This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const threeSum = (nums) => { | |
| let len = nums.length; | |
| if(len < 3) return []; | |
| nums.sort(function(a,b){ | |
| return a-b; | |
| }) | |
| if(nums[0] > 0 || nums[0] + nums[1] + nums[2] > 0) return []; | |
| if(len === 3) { | |
| if(nums[0] + nums[1] + nums[2] === 0) return [nums]; | |
| else return []; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const threeSum = (nums) => { | |
| let len = nums.length; | |
| if(len < 3) return []; | |
| nums.sort(function(a,b){ | |
| return a-b; | |
| }) | |
| if(nums[0] > 0 || nums[0] + nums[1] + nums[2] > 0) return []; | |
| if(len === 3) { | |
| if(nums[0] + nums[1] + nums[2] === 0) return [nums]; | |
| else return []; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const minPathSum = (grid) => { | |
| let [m, n] = [grid.length, grid[0].length]; | |
| if(m+n === 0) return 0; | |
| let result = [grid[0][0]]; | |
| for (let j = 1; j < n; j++) { // initialize | |
| result[j] = result[j-1] + grid[0][j]; | |
| } | |
| for(let i=1; i<m; i++){ | |
| for(let j=0; j<n; j++){ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const maxArea = function(height) { | |
| let max = 0, | |
| left = 0, | |
| right = height.length - 1; | |
| let term, thisArea, width; | |
| while (left < right) { | |
| width = right - left; | |
| if (height[left] < height[right]) { | |
| thisArea = height[left] * width; | |
| left++; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const trap = (number) => { | |
| let len = number.length; | |
| if(!len) return 0; | |
| let result = 0; | |
| let left = 0, right = len - 1; | |
| let maxLeft = 0, maxRight = 0; | |
| while(left<=right){ | |
| if(number[left] <= number[right]){ | |
| if(number[left] >=maxLeft) maxLeft = number[left]; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const trap = (number) => { | |
| let len = number.length; | |
| if(!len) return 0; | |
| let result = 0; | |
| let maxLeft = []; | |
| maxLeft[0] = number[0]; | |
| let maxRight = []; | |
| maxRight[len-1] = number[len-1]; | |
| for(let i=1; i<len; i++) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const maximumProduct = function(nums) { | |
| let len = nums.length; | |
| let maxProduct = 1; | |
| if(len===0) return 0; | |
| if(len<=3) { | |
| for(var i=0; i<len; i++){ | |
| maxProduct *= nums[i]; | |
| } | |
| return maxProduct; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const maximumProduct = (nums) => { | |
| let len = nums.length; | |
| let maxProduct = 1; | |
| if(len===0) return 0; | |
| if(len<=3) { | |
| for(let i=0; i<len; i++){ | |
| maxProduct *= nums[i]; | |
| } | |
| return maxProduct; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const integerBreak = (n) => { | |
| if(n<=3) return n-1; | |
| if(n%3===0) return Math.pow(3, n/3); | |
| if(n%3===1) return 4*Math.pow(3, (n-4)/3); | |
| return 2*Math.pow(3,parseInt(n/3)); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const missingNumber = (nums) => { | |
| let len = nums.length; | |
| if(len===0) return 0; | |
| result = 0; | |
| for (let i=0; i<len; i++) { | |
| result += nums[i] - i; | |
| } | |
| return len - result; | |
| }; |