diff --git "a/lcof/\351\235\242\350\257\225\351\242\23003. \346\225\260\347\273\204\344\270\255\351\207\215\345\244\215\347\232\204\346\225\260\345\255\227/README.md" "b/lcof/\351\235\242\350\257\225\351\242\23003. \346\225\260\347\273\204\344\270\255\351\207\215\345\244\215\347\232\204\346\225\260\345\255\227/README.md" index 0d71e2df57da4..a68d88d107885 100644 --- "a/lcof/\351\235\242\350\257\225\351\242\23003. \346\225\260\347\273\204\344\270\255\351\207\215\345\244\215\347\232\204\346\225\260\345\255\227/README.md" +++ "b/lcof/\351\235\242\350\257\225\351\242\23003. \346\225\260\347\273\204\344\270\255\351\207\215\345\244\215\347\232\204\346\225\260\345\255\227/README.md" @@ -59,6 +59,21 @@ class Solution { } ``` +### JavaScript +```js +/** + * @param {number[]} nums + * @return {number} + */ +var findRepeatNumber = function(nums) { + let m = {} + for(let num of nums) { + if(m[num]) return num + m[num] = 1 + } +}; +``` + ### ... ``` diff --git "a/lcof/\351\235\242\350\257\225\351\242\23003. \346\225\260\347\273\204\344\270\255\351\207\215\345\244\215\347\232\204\346\225\260\345\255\227/Solution.js" "b/lcof/\351\235\242\350\257\225\351\242\23003. \346\225\260\347\273\204\344\270\255\351\207\215\345\244\215\347\232\204\346\225\260\345\255\227/Solution.js" new file mode 100644 index 0000000000000..a09fb63a5e24c --- /dev/null +++ "b/lcof/\351\235\242\350\257\225\351\242\23003. \346\225\260\347\273\204\344\270\255\351\207\215\345\244\215\347\232\204\346\225\260\345\255\227/Solution.js" @@ -0,0 +1,11 @@ +/** + * @param {number[]} nums + * @return {number} + */ +var findRepeatNumber = function(nums) { + let m = {} + for(let num of nums) { + if(m[num]) return num + m[num] = 1 + } +}; \ No newline at end of file diff --git "a/lcof/\351\235\242\350\257\225\351\242\23004. \344\272\214\347\273\264\346\225\260\347\273\204\344\270\255\347\232\204\346\237\245\346\211\276/README.md" "b/lcof/\351\235\242\350\257\225\351\242\23004. \344\272\214\347\273\264\346\225\260\347\273\204\344\270\255\347\232\204\346\237\245\346\211\276/README.md" index 2b9afd882f16b..9a0f257c988bb 100644 --- "a/lcof/\351\235\242\350\257\225\351\242\23004. \344\272\214\347\273\264\346\225\260\347\273\204\344\270\255\347\232\204\346\237\245\346\211\276/README.md" +++ "b/lcof/\351\235\242\350\257\225\351\242\23004. \344\272\214\347\273\264\346\225\260\347\273\204\344\270\255\347\232\204\346\237\245\346\211\276/README.md" @@ -73,6 +73,31 @@ class Solution { } ``` +### JavaScript +```js +/** + * @param {number[][]} matrix + * @param {number} target + * @return {boolean} + */ +var findNumberIn2DArray = function(matrix, target) { + let row = matrix.length + let col = matrix[0].length + function dfs(i,j) { + if(i < 0 || j >= col) { + return false + } + if(matrix[i][j] === target) return true + else if(matrix[i][j] > target) { + return dfs(i-1,j) + } else { + return dfs(i,j+1) + } + } + return dfs(row-1,0) +}; +``` + ### ... ``` diff --git "a/lcof/\351\235\242\350\257\225\351\242\23004. \344\272\214\347\273\264\346\225\260\347\273\204\344\270\255\347\232\204\346\237\245\346\211\276/Solution.js" "b/lcof/\351\235\242\350\257\225\351\242\23004. \344\272\214\347\273\264\346\225\260\347\273\204\344\270\255\347\232\204\346\237\245\346\211\276/Solution.js" new file mode 100644 index 0000000000000..a24d211a46bb4 --- /dev/null +++ "b/lcof/\351\235\242\350\257\225\351\242\23004. \344\272\214\347\273\264\346\225\260\347\273\204\344\270\255\347\232\204\346\237\245\346\211\276/Solution.js" @@ -0,0 +1,21 @@ +/** + * @param {number[][]} matrix + * @param {number} target + * @return {boolean} + */ +var findNumberIn2DArray = function(matrix, target) { + let row = matrix.length + let col = matrix[0].length + function dfs(i,j) { + if(i < 0 || j >= col) { + return false + } + if(matrix[i][j] === target) return true + else if(matrix[i][j] > target) { + return dfs(i-1,j) + } else { + return dfs(i,j+1) + } + } + return dfs(row-1,0) +}; \ No newline at end of file diff --git "a/lcof/\351\235\242\350\257\225\351\242\23005. \346\233\277\346\215\242\347\251\272\346\240\274/README.md" "b/lcof/\351\235\242\350\257\225\351\242\23005. \346\233\277\346\215\242\347\251\272\346\240\274/README.md" index 6341ae8aabb14..0ca500a25cd00 100644 --- "a/lcof/\351\235\242\350\257\225\351\242\23005. \346\233\277\346\215\242\347\251\272\346\240\274/README.md" +++ "b/lcof/\351\235\242\350\257\225\351\242\23005. \346\233\277\346\215\242\347\251\272\346\240\274/README.md" @@ -33,6 +33,17 @@ class Solution { } ``` +### JavaScript +```js +/** + * @param {string} s + * @return {string} + */ +var replaceSpace = function(s) { + return s.split(' ').join('%20') +}; +``` + ### ... ``` diff --git "a/lcof/\351\235\242\350\257\225\351\242\23005. \346\233\277\346\215\242\347\251\272\346\240\274/Solution.js" "b/lcof/\351\235\242\350\257\225\351\242\23005. \346\233\277\346\215\242\347\251\272\346\240\274/Solution.js" new file mode 100644 index 0000000000000..7408d235562c0 --- /dev/null +++ "b/lcof/\351\235\242\350\257\225\351\242\23005. \346\233\277\346\215\242\347\251\272\346\240\274/Solution.js" @@ -0,0 +1,7 @@ +/** + * @param {string} s + * @return {string} + */ +var replaceSpace = function(s) { + return s.split(' ').join('%20') +}; \ No newline at end of file diff --git "a/lcof/\351\235\242\350\257\225\351\242\23006. \344\273\216\345\260\276\345\210\260\345\244\264\346\211\223\345\215\260\351\223\276\350\241\250/README.md" "b/lcof/\351\235\242\350\257\225\351\242\23006. \344\273\216\345\260\276\345\210\260\345\244\264\346\211\223\345\215\260\351\223\276\350\241\250/README.md" index 26d1eedb474e0..3e4942c7c78c8 100644 --- "a/lcof/\351\235\242\350\257\225\351\242\23006. \344\273\216\345\260\276\345\210\260\345\244\264\346\211\223\345\215\260\351\223\276\350\241\250/README.md" +++ "b/lcof/\351\235\242\350\257\225\351\242\23006. \344\273\216\345\260\276\345\210\260\345\244\264\346\211\223\345\215\260\351\223\276\350\241\250/README.md" @@ -60,6 +60,11 @@ class Solution { } ``` +### JavaScript +```js + +``` + ### ... ``` diff --git "a/lcof/\351\235\242\350\257\225\351\242\23006. \344\273\216\345\260\276\345\210\260\345\244\264\346\211\223\345\215\260\351\223\276\350\241\250/Solution.js" "b/lcof/\351\235\242\350\257\225\351\242\23006. \344\273\216\345\260\276\345\210\260\345\244\264\346\211\223\345\215\260\351\223\276\350\241\250/Solution.js" new file mode 100644 index 0000000000000..2f0547e64aafa --- /dev/null +++ "b/lcof/\351\235\242\350\257\225\351\242\23006. \344\273\216\345\260\276\345\210\260\345\244\264\346\211\223\345\215\260\351\223\276\350\241\250/Solution.js" @@ -0,0 +1,20 @@ +/** + * Definition for singly-linked list. + * function ListNode(val) { + * this.val = val; + * this.next = null; + * } + */ +/** + * @param {ListNode} head + * @return {number[]} + */ +var reversePrint = function(head) { + let node = head + let res = [] + while(node) { + res.unshift(node.val) + node = node.next + } + return res +}; \ No newline at end of file diff --git "a/lcof/\351\235\242\350\257\225\351\242\23007. \351\207\215\345\273\272\344\272\214\345\217\211\346\240\221/README.md" "b/lcof/\351\235\242\350\257\225\351\242\23007. \351\207\215\345\273\272\344\272\214\345\217\211\346\240\221/README.md" index e7c900f097185..b298a761d0364 100644 --- "a/lcof/\351\235\242\350\257\225\351\242\23007. \351\207\215\345\273\272\344\272\214\345\217\211\346\240\221/README.md" +++ "b/lcof/\351\235\242\350\257\225\351\242\23007. \351\207\215\345\273\272\344\272\214\345\217\211\346\240\221/README.md" @@ -99,6 +99,43 @@ class Solution { } ``` +### JavaScript +```js +/** + * Definition for a binary tree node. + * function TreeNode(val) { + * this.val = val; + * this.left = this.right = null; + * } + */ +/** + * @param {number[]} preorder + * @param {number[]} inorder + * @return {TreeNode} + */ +var buildTree = function(preorder, inorder) { + if(!preorder || !preorder.length) return null + let preIdx = 0 + let inMap = {} + for(let i=0;i end) { + return null + } + let preVal = preorder[preIdx] + preIdx++ + let inIdx = inMap[preVal] + let node = new TreeNode(preVal) + node.left = func(start, inIdx - 1) + node.right = func(inIdx + 1, end) + return node + } + return func(0, preorder.length - 1) +}; +``` + ### ... ``` diff --git "a/lcof/\351\235\242\350\257\225\351\242\23007. \351\207\215\345\273\272\344\272\214\345\217\211\346\240\221/Solution.js" "b/lcof/\351\235\242\350\257\225\351\242\23007. \351\207\215\345\273\272\344\272\214\345\217\211\346\240\221/Solution.js" new file mode 100644 index 0000000000000..3b574635c8087 --- /dev/null +++ "b/lcof/\351\235\242\350\257\225\351\242\23007. \351\207\215\345\273\272\344\272\214\345\217\211\346\240\221/Solution.js" @@ -0,0 +1,33 @@ +/** + * Definition for a binary tree node. + * function TreeNode(val) { + * this.val = val; + * this.left = this.right = null; + * } + */ +/** + * @param {number[]} preorder + * @param {number[]} inorder + * @return {TreeNode} + */ +var buildTree = function(preorder, inorder) { + if(!preorder || !preorder.length) return null + let preIdx = 0 + let inMap = {} + for(let i=0;i end) { + return null + } + let preVal = preorder[preIdx] + preIdx++ + let inIdx = inMap[preVal] + let node = new TreeNode(preVal) + node.left = func(start, inIdx - 1) + node.right = func(inIdx + 1, end) + return node + } + return func(0, preorder.length - 1) +}; \ No newline at end of file diff --git "a/lcof/\351\235\242\350\257\225\351\242\23009. \347\224\250\344\270\244\344\270\252\346\240\210\345\256\236\347\216\260\351\230\237\345\210\227/README.md" "b/lcof/\351\235\242\350\257\225\351\242\23009. \347\224\250\344\270\244\344\270\252\346\240\210\345\256\236\347\216\260\351\230\237\345\210\227/README.md" index 9c26fa62c1041..3faf1c67687ed 100644 --- "a/lcof/\351\235\242\350\257\225\351\242\23009. \347\224\250\344\270\244\344\270\252\346\240\210\345\256\236\347\216\260\351\230\237\345\210\227/README.md" +++ "b/lcof/\351\235\242\350\257\225\351\242\23009. \347\224\250\344\270\244\344\270\252\346\240\210\345\256\236\347\216\260\351\230\237\345\210\227/README.md" @@ -92,6 +92,38 @@ class CQueue { */ ``` +### JavaScript +```js +var CQueue = function() { + this.data = [] + this.helper = [] +}; +/** + * @param {number} value + * @return {void} + */ +CQueue.prototype.appendTail = function(value) { + this.data.push(value) +}; +/** + * @return {number} + */ +CQueue.prototype.deleteHead = function() { + if(this.data.length) { + while(this.data.length > 1) { + this.helper.push(this.data.pop()) + } + let res = this.data.pop() + while(this.helper.length) { + this.data.push(this.helper.pop()) + } + return res + } else { + return -1 + } +}; +``` + ### ... ``` diff --git "a/lcof/\351\235\242\350\257\225\351\242\23009. \347\224\250\344\270\244\344\270\252\346\240\210\345\256\236\347\216\260\351\230\237\345\210\227/Solution.js" "b/lcof/\351\235\242\350\257\225\351\242\23009. \347\224\250\344\270\244\344\270\252\346\240\210\345\256\236\347\216\260\351\230\237\345\210\227/Solution.js" new file mode 100644 index 0000000000000..1472cd7f86239 --- /dev/null +++ "b/lcof/\351\235\242\350\257\225\351\242\23009. \347\224\250\344\270\244\344\270\252\346\240\210\345\256\236\347\216\260\351\230\237\345\210\227/Solution.js" @@ -0,0 +1,28 @@ +var CQueue = function() { + this.data = [] + this.helper = [] +}; +/** + * @param {number} value + * @return {void} + */ +CQueue.prototype.appendTail = function(value) { + this.data.push(value) +}; +/** + * @return {number} + */ +CQueue.prototype.deleteHead = function() { + if(this.data.length) { + while(this.data.length > 1) { + this.helper.push(this.data.pop()) + } + let res = this.data.pop() + while(this.helper.length) { + this.data.push(this.helper.pop()) + } + return res + } else { + return -1 + } +}; \ No newline at end of file diff --git "a/lcof/\351\235\242\350\257\225\351\242\23010- I. \346\226\220\346\263\242\351\202\243\345\245\221\346\225\260\345\210\227/README.md" "b/lcof/\351\235\242\350\257\225\351\242\23010- I. \346\226\220\346\263\242\351\202\243\345\245\221\346\225\260\345\210\227/README.md" index decd389557e84..8c60b767d8b63 100644 --- "a/lcof/\351\235\242\350\257\225\351\242\23010- I. \346\226\220\346\263\242\351\202\243\345\245\221\346\225\260\345\210\227/README.md" +++ "b/lcof/\351\235\242\350\257\225\351\242\23010- I. \346\226\220\346\263\242\351\202\243\345\245\221\346\225\260\345\210\227/README.md" @@ -58,6 +58,25 @@ class Solution { } ``` +### JavaScript +```js +/** + * @param {number} n + * @return {number} + */ +var fib = function(n) { + if(!n) return 0 + let pre = 0 + let cur = 1 + for(let i=2;i<=n;i++) { + let c = (pre + cur)%(1e9+7) + pre = cur + cur = c + } + return cur +}; +``` + ### ... ``` diff --git "a/lcof/\351\235\242\350\257\225\351\242\23010- I. \346\226\220\346\263\242\351\202\243\345\245\221\346\225\260\345\210\227/Solution.js" "b/lcof/\351\235\242\350\257\225\351\242\23010- I. \346\226\220\346\263\242\351\202\243\345\245\221\346\225\260\345\210\227/Solution.js" new file mode 100644 index 0000000000000..0b7822ed4593a --- /dev/null +++ "b/lcof/\351\235\242\350\257\225\351\242\23010- I. \346\226\220\346\263\242\351\202\243\345\245\221\346\225\260\345\210\227/Solution.js" @@ -0,0 +1,15 @@ +/** + * @param {number} n + * @return {number} + */ +var fib = function(n) { + if(!n) return 0 + let pre = 0 + let cur = 1 + for(let i=2;i<=n;i++) { + let c = (pre + cur)%(1e9+7) + pre = cur + cur = c + } + return cur +}; \ No newline at end of file diff --git "a/lcof/\351\235\242\350\257\225\351\242\23010- II. \351\235\222\350\233\231\350\267\263\345\217\260\351\230\266\351\227\256\351\242\230/README.md" "b/lcof/\351\235\242\350\257\225\351\242\23010- II. \351\235\222\350\233\231\350\267\263\345\217\260\351\230\266\351\227\256\351\242\230/README.md" index 21358e3129f94..2489bb0d452b5 100644 --- "a/lcof/\351\235\242\350\257\225\351\242\23010- II. \351\235\222\350\233\231\350\267\263\345\217\260\351\230\266\351\227\256\351\242\230/README.md" +++ "b/lcof/\351\235\242\350\257\225\351\242\23010- II. \351\235\222\350\233\231\350\267\263\345\217\260\351\230\266\351\227\256\351\242\230/README.md" @@ -53,6 +53,25 @@ class Solution { } ``` +### JavaScript +```js +/** + * @param {number} n + * @return {number} + */ +var numWays = function(n) { + if(!n) return 1 + let pre = 1 + let cur = 1 + for(let i=2;i<=n;i++) { + let c = (pre + cur) % (1e9 + 7) + pre = cur + cur = c + } + return cur +}; +``` + ### ... ``` diff --git "a/lcof/\351\235\242\350\257\225\351\242\23010- II. \351\235\222\350\233\231\350\267\263\345\217\260\351\230\266\351\227\256\351\242\230/Solution.js" "b/lcof/\351\235\242\350\257\225\351\242\23010- II. \351\235\222\350\233\231\350\267\263\345\217\260\351\230\266\351\227\256\351\242\230/Solution.js" new file mode 100644 index 0000000000000..4d1b07d839035 --- /dev/null +++ "b/lcof/\351\235\242\350\257\225\351\242\23010- II. \351\235\222\350\233\231\350\267\263\345\217\260\351\230\266\351\227\256\351\242\230/Solution.js" @@ -0,0 +1,15 @@ +/** + * @param {number} n + * @return {number} + */ +var numWays = function(n) { + if(!n) return 1 + let pre = 1 + let cur = 1 + for(let i=2;i<=n;i++) { + let c = (pre + cur) % (1e9 + 7) + pre = cur + cur = c + } + return cur +}; \ No newline at end of file diff --git "a/lcof/\351\235\242\350\257\225\351\242\23011. \346\227\213\350\275\254\346\225\260\347\273\204\347\232\204\346\234\200\345\260\217\346\225\260\345\255\227/README.md" "b/lcof/\351\235\242\350\257\225\351\242\23011. \346\227\213\350\275\254\346\225\260\347\273\204\347\232\204\346\234\200\345\260\217\346\225\260\345\255\227/README.md" index b81254cd431e7..27ee5d2059d5d 100644 --- "a/lcof/\351\235\242\350\257\225\351\242\23011. \346\227\213\350\275\254\346\225\260\347\273\204\347\232\204\346\234\200\345\260\217\346\225\260\345\255\227/README.md" +++ "b/lcof/\351\235\242\350\257\225\351\242\23011. \346\227\213\350\275\254\346\225\260\347\273\204\347\232\204\346\234\200\345\260\217\346\225\260\345\255\227/README.md" @@ -72,6 +72,30 @@ class Solution { } ``` +### JavaScript +```js +/** + * @param {number[]} numbers + * @return {number} + */ +var minArray = function(numbers) { + // return Math.min(...numbers) + let left = 0 + let right = numbers.length-1 + while(left < right) { + let mid = left + ~~((right - left)/2) + if(numbers[mid] > numbers[right]) { + left = mid + 1 + } else if(numbers[mid] === numbers[right]) { + right-- + } else { + right = mid + } + } + return numbers[left] +}; +``` + ### ... ``` diff --git "a/lcof/\351\235\242\350\257\225\351\242\23011. \346\227\213\350\275\254\346\225\260\347\273\204\347\232\204\346\234\200\345\260\217\346\225\260\345\255\227/Solution.js" "b/lcof/\351\235\242\350\257\225\351\242\23011. \346\227\213\350\275\254\346\225\260\347\273\204\347\232\204\346\234\200\345\260\217\346\225\260\345\255\227/Solution.js" new file mode 100644 index 0000000000000..8065ce8b2efc8 --- /dev/null +++ "b/lcof/\351\235\242\350\257\225\351\242\23011. \346\227\213\350\275\254\346\225\260\347\273\204\347\232\204\346\234\200\345\260\217\346\225\260\345\255\227/Solution.js" @@ -0,0 +1,20 @@ +/** + * @param {number[]} numbers + * @return {number} + */ +var minArray = function(numbers) { + // return Math.min(...numbers) + let left = 0 + let right = numbers.length-1 + while(left < right) { + let mid = left + ~~((right - left)/2) + if(numbers[mid] > numbers[right]) { + left = mid + 1 + } else if(numbers[mid] === numbers[right]) { + right-- + } else { + right = mid + } + } + return numbers[left] +}; \ No newline at end of file diff --git "a/lcof/\351\235\242\350\257\225\351\242\23012. \347\237\251\351\230\265\344\270\255\347\232\204\350\267\257\345\276\204/README.md" "b/lcof/\351\235\242\350\257\225\351\242\23012. \347\237\251\351\230\265\344\270\255\347\232\204\350\267\257\345\276\204/README.md" index 20bcc45bd5b22..0e7d5cbb6db71 100644 --- "a/lcof/\351\235\242\350\257\225\351\242\23012. \347\237\251\351\230\265\344\270\255\347\232\204\350\267\257\345\276\204/README.md" +++ "b/lcof/\351\235\242\350\257\225\351\242\23012. \347\237\251\351\230\265\344\270\255\347\232\204\350\267\257\345\276\204/README.md" @@ -97,6 +97,47 @@ class Solution { } ``` +### JavaScript +```js +/** + * @param {character[][]} board + * @param {string} word + * @return {boolean} + */ +var exist = function(board, word) { + let row = board.length + let col = board[0].length + let res = false + let isRead = [...new Array(row)].map(()=>Array(col).fill(0)) + for(let i=0;i= row || j >= col || res || isRead[i][j] || board[i][j] !== word[0]) { + return + } + isRead[i][j] = 1 + word = word.substring(1) + if(word.length) { + dfs(i-1,j,word) + dfs(i+1,j,word) + dfs(i,j-1,word) + dfs(i,j+1,word) + } else { + res = true + return + } + isRead[i][j] = 0 + } + return res +}; +``` + ### ... ``` diff --git "a/lcof/\351\235\242\350\257\225\351\242\23012. \347\237\251\351\230\265\344\270\255\347\232\204\350\267\257\345\276\204/Solution.js" "b/lcof/\351\235\242\350\257\225\351\242\23012. \347\237\251\351\230\265\344\270\255\347\232\204\350\267\257\345\276\204/Solution.js" new file mode 100644 index 0000000000000..0be1068eb59b3 --- /dev/null +++ "b/lcof/\351\235\242\350\257\225\351\242\23012. \347\237\251\351\230\265\344\270\255\347\232\204\350\267\257\345\276\204/Solution.js" @@ -0,0 +1,37 @@ +/** + * @param {character[][]} board + * @param {string} word + * @return {boolean} + */ +var exist = function(board, word) { + let row = board.length + let col = board[0].length + let res = false + let isRead = [...new Array(row)].map(()=>Array(col).fill(0)) + for(let i=0;i= row || j >= col || res || isRead[i][j] || board[i][j] !== word[0]) { + return + } + isRead[i][j] = 1 + word = word.substring(1) + if(word.length) { + dfs(i-1,j,word) + dfs(i+1,j,word) + dfs(i,j-1,word) + dfs(i,j+1,word) + } else { + res = true + return + } + isRead[i][j] = 0 + } + return res +}; \ No newline at end of file diff --git "a/lcof/\351\235\242\350\257\225\351\242\23013. \346\234\272\345\231\250\344\272\272\347\232\204\350\277\220\345\212\250\350\214\203\345\233\264/README.md" "b/lcof/\351\235\242\350\257\225\351\242\23013. \346\234\272\345\231\250\344\272\272\347\232\204\350\277\220\345\212\250\350\214\203\345\233\264/README.md" index 46fa9b5a10f65..b35918721c3c6 100644 --- "a/lcof/\351\235\242\350\257\225\351\242\23013. \346\234\272\345\231\250\344\272\272\347\232\204\350\277\220\345\212\250\350\214\203\345\233\264/README.md" +++ "b/lcof/\351\235\242\350\257\225\351\242\23013. \346\234\272\345\231\250\344\272\272\347\232\204\350\277\220\345\212\250\350\214\203\345\233\264/README.md" @@ -91,6 +91,42 @@ class Solution { } ``` +### JavaScript +```js +/** + * @param {number} m + * @param {number} n + * @param {number} k + * @return {number} + */ +var movingCount = function(m, n, k) { + let res = 0 + let isRead = [...new Array(m)].map(()=>Array(n).fill(0)) + let moving = [[0,-1],[0,1],[1,0],[-1,0]] + let queue = [[0,0]] + isRead[0][0] = 1 + while(queue.length) { + let [x,y] = queue.shift() + for(let [dx,dy] of moving) { + let X = x + dx + let Y = y + dy + if(X >= 0 && Y >= 0 && X < m && Y < n && !isRead[X][Y] && isValid(X,Y)) { + queue.push([X,Y]) + isRead[X][Y] = 1 + } + } + res++ + } + function isValid(x,y) { + let r = 0 + r += x.toString().split('').reduce((acc,cur)=>acc+ +cur,0) + y.toString().split('').reduce((acc,cur)=>acc+ +cur,0) + if(r <= k) return true + else return false + } + return res +}; +``` + ### ... ``` diff --git "a/lcof/\351\235\242\350\257\225\351\242\23013. \346\234\272\345\231\250\344\272\272\347\232\204\350\277\220\345\212\250\350\214\203\345\233\264/Solution.js" "b/lcof/\351\235\242\350\257\225\351\242\23013. \346\234\272\345\231\250\344\272\272\347\232\204\350\277\220\345\212\250\350\214\203\345\233\264/Solution.js" new file mode 100644 index 0000000000000..e3e2f110a43e7 --- /dev/null +++ "b/lcof/\351\235\242\350\257\225\351\242\23013. \346\234\272\345\231\250\344\272\272\347\232\204\350\277\220\345\212\250\350\214\203\345\233\264/Solution.js" @@ -0,0 +1,32 @@ +/** + * @param {number} m + * @param {number} n + * @param {number} k + * @return {number} + */ +var movingCount = function(m, n, k) { + let res = 0 + let isRead = [...new Array(m)].map(()=>Array(n).fill(0)) + let moving = [[0,-1],[0,1],[1,0],[-1,0]] + let queue = [[0,0]] + isRead[0][0] = 1 + while(queue.length) { + let [x,y] = queue.shift() + for(let [dx,dy] of moving) { + let X = x + dx + let Y = y + dy + if(X >= 0 && Y >= 0 && X < m && Y < n && !isRead[X][Y] && isValid(X,Y)) { + queue.push([X,Y]) + isRead[X][Y] = 1 + } + } + res++ + } + function isValid(x,y) { + let r = 0 + r += x.toString().split('').reduce((acc,cur)=>acc+ +cur,0) + y.toString().split('').reduce((acc,cur)=>acc+ +cur,0) + if(r <= k) return true + else return false + } + return res +}; \ No newline at end of file diff --git "a/lcof/\351\235\242\350\257\225\351\242\23014- I. \345\211\252\347\273\263\345\255\220/README.md" "b/lcof/\351\235\242\350\257\225\351\242\23014- I. \345\211\252\347\273\263\345\255\220/README.md" index c38533e199cea..a7dff72fa4414 100644 --- "a/lcof/\351\235\242\350\257\225\351\242\23014- I. \345\211\252\347\273\263\345\255\220/README.md" +++ "b/lcof/\351\235\242\350\257\225\351\242\23014- I. \345\211\252\347\273\263\345\255\220/README.md" @@ -54,6 +54,34 @@ class Solution { } ``` +### JavaScript +```js +/** + * @param {number} n + * @return {number} + */ +var cuttingRope = function(n) { + // 数学方法 + if(n <= 3) return n-1 + let a = ~~(n / 3) + let b = n % 3 + if(b === 1) { + return 3**(a-1) * 2 * 2 + } + if(b === 0) return 3**a + return 3**a * b + // dp 方法 + // let dp = new Array(n+1).fill(0) + // dp[0] = 1 + // for(let i=1;i>>= 1 + } + return cnt +}; +``` + ### ... ``` diff --git "a/lcof/\351\235\242\350\257\225\351\242\23015. \344\272\214\350\277\233\345\210\266\344\270\2551\347\232\204\344\270\252\346\225\260/Solution.js" "b/lcof/\351\235\242\350\257\225\351\242\23015. \344\272\214\350\277\233\345\210\266\344\270\2551\347\232\204\344\270\252\346\225\260/Solution.js" new file mode 100644 index 0000000000000..430e3f32be185 --- /dev/null +++ "b/lcof/\351\235\242\350\257\225\351\242\23015. \344\272\214\350\277\233\345\210\266\344\270\2551\347\232\204\344\270\252\346\225\260/Solution.js" @@ -0,0 +1,12 @@ +/** + * @param {number} n - a positive integer + * @return {number} + */ +var hammingWeight = function(n) { + let cnt = 0 + while(n) { + cnt += n & 1 + n >>>= 1 + } + return cnt +}; \ No newline at end of file diff --git "a/lcof/\351\235\242\350\257\225\351\242\23016. \346\225\260\345\200\274\347\232\204\346\225\264\346\225\260\346\254\241\346\226\271/README.md" "b/lcof/\351\235\242\350\257\225\351\242\23016. \346\225\260\345\200\274\347\232\204\346\225\264\346\225\260\346\254\241\346\226\271/README.md" index 91e01b032d978..de8db124d9db6 100644 --- "a/lcof/\351\235\242\350\257\225\351\242\23016. \346\225\260\345\200\274\347\232\204\346\225\264\346\225\260\346\254\241\346\226\271/README.md" +++ "b/lcof/\351\235\242\350\257\225\351\242\23016. \346\225\260\345\200\274\347\232\204\346\225\264\346\225\260\346\254\241\346\226\271/README.md" @@ -64,6 +64,32 @@ class Solution { } ``` +### JavaScript +```js +/** + * @param {number} x + * @param {number} n + * @return {number} + */ +var myPow = function(x, n) { + let r = 1 + let tmp = x + let tag = 0 + if(n < 0) { + tag = 1 + n = -n + } + while(n) { + if(n & 1) { + r *= tmp + } + tmp *= tmp + n >>>= 1 + } + return tag ? 1/r : r +}; +``` + ### ... ``` diff --git "a/lcof/\351\235\242\350\257\225\351\242\23016. \346\225\260\345\200\274\347\232\204\346\225\264\346\225\260\346\254\241\346\226\271/Solution.js" "b/lcof/\351\235\242\350\257\225\351\242\23016. \346\225\260\345\200\274\347\232\204\346\225\264\346\225\260\346\254\241\346\226\271/Solution.js" new file mode 100644 index 0000000000000..546c5946ad56e --- /dev/null +++ "b/lcof/\351\235\242\350\257\225\351\242\23016. \346\225\260\345\200\274\347\232\204\346\225\264\346\225\260\346\254\241\346\226\271/Solution.js" @@ -0,0 +1,22 @@ +/** + * @param {number} x + * @param {number} n + * @return {number} + */ +var myPow = function(x, n) { + let r = 1 + let tmp = x + let tag = 0 + if(n < 0) { + tag = 1 + n = -n + } + while(n) { + if(n & 1) { + r *= tmp + } + tmp *= tmp + n >>>= 1 + } + return tag ? 1/r : r +}; \ No newline at end of file diff --git "a/lcof/\351\235\242\350\257\225\351\242\23017. \346\211\223\345\215\260\344\273\2161\345\210\260\346\234\200\345\244\247\347\232\204n\344\275\215\346\225\260/README.md" "b/lcof/\351\235\242\350\257\225\351\242\23017. \346\211\223\345\215\260\344\273\2161\345\210\260\346\234\200\345\244\247\347\232\204n\344\275\215\346\225\260/README.md" index 9eb79858f47bd..7c22ac8b708f9 100644 --- "a/lcof/\351\235\242\350\257\225\351\242\23017. \346\211\223\345\215\260\344\273\2161\345\210\260\346\234\200\345\244\247\347\232\204n\344\275\215\346\225\260/README.md" +++ "b/lcof/\351\235\242\350\257\225\351\242\23017. \346\211\223\345\215\260\344\273\2161\345\210\260\346\234\200\345\244\247\347\232\204n\344\275\215\346\225\260/README.md" @@ -37,6 +37,21 @@ class Solution { } ``` +### JavaScript +```js +/** + * @param {number} n + * @return {number[]} + */ +var printNumbers = function(n) { + let res = [] + for(let i=1;i<10**n;i++) { + res.push(i) + } + return res +}; +``` + ### ... ``` diff --git "a/lcof/\351\235\242\350\257\225\351\242\23017. \346\211\223\345\215\260\344\273\2161\345\210\260\346\234\200\345\244\247\347\232\204n\344\275\215\346\225\260/Solution.js" "b/lcof/\351\235\242\350\257\225\351\242\23017. \346\211\223\345\215\260\344\273\2161\345\210\260\346\234\200\345\244\247\347\232\204n\344\275\215\346\225\260/Solution.js" new file mode 100644 index 0000000000000..7a7c367793b7c --- /dev/null +++ "b/lcof/\351\235\242\350\257\225\351\242\23017. \346\211\223\345\215\260\344\273\2161\345\210\260\346\234\200\345\244\247\347\232\204n\344\275\215\346\225\260/Solution.js" @@ -0,0 +1,11 @@ +/** + * @param {number} n + * @return {number[]} + */ +var printNumbers = function(n) { + let res = [] + for(let i=1;i<10**n;i++) { + res.push(i) + } + return res +}; \ No newline at end of file diff --git "a/lcof/\351\235\242\350\257\225\351\242\23018. \345\210\240\351\231\244\351\223\276\350\241\250\347\232\204\350\212\202\347\202\271/README.md" "b/lcof/\351\235\242\350\257\225\351\242\23018. \345\210\240\351\231\244\351\223\276\350\241\250\347\232\204\350\212\202\347\202\271/README.md" index 27486f919f2ae..3828d503763d9 100644 --- "a/lcof/\351\235\242\350\257\225\351\242\23018. \345\210\240\351\231\244\351\223\276\350\241\250\347\232\204\350\212\202\347\202\271/README.md" +++ "b/lcof/\351\235\242\350\257\225\351\242\23018. \345\210\240\351\231\244\351\223\276\350\241\250\347\232\204\350\212\202\347\202\271/README.md" @@ -82,6 +82,38 @@ class Solution { } ``` +### JavaScript +```js +/** + * Definition for singly-linked list. + * function ListNode(val) { + * this.val = val; + * this.next = null; + * } + */ +/** + * @param {ListNode} head + * @param {number} val + * @return {ListNode} + */ +var deleteNode = function(head, val) { + let node = head + if(node.val === val) { + node = node.next + head = node + } else { + while(node.next) { + if(node.next.val === val) { + node.next = node.next.next + break + } + node = node.next + } + } + return head +}; +``` + ### ... ``` diff --git "a/lcof/\351\235\242\350\257\225\351\242\23018. \345\210\240\351\231\244\351\223\276\350\241\250\347\232\204\350\212\202\347\202\271/Solution.js" "b/lcof/\351\235\242\350\257\225\351\242\23018. \345\210\240\351\231\244\351\223\276\350\241\250\347\232\204\350\212\202\347\202\271/Solution.js" new file mode 100644 index 0000000000000..a240bcaeefacf --- /dev/null +++ "b/lcof/\351\235\242\350\257\225\351\242\23018. \345\210\240\351\231\244\351\223\276\350\241\250\347\232\204\350\212\202\347\202\271/Solution.js" @@ -0,0 +1,28 @@ +/** + * Definition for singly-linked list. + * function ListNode(val) { + * this.val = val; + * this.next = null; + * } + */ +/** + * @param {ListNode} head + * @param {number} val + * @return {ListNode} + */ +var deleteNode = function(head, val) { + let node = head + if(node.val === val) { + node = node.next + head = node + } else { + while(node.next) { + if(node.next.val === val) { + node.next = node.next.next + break + } + node = node.next + } + } + return head +}; \ No newline at end of file diff --git "a/lcof/\351\235\242\350\257\225\351\242\23019. \346\255\243\345\210\231\350\241\250\350\276\276\345\274\217\345\214\271\351\205\215/README.md" "b/lcof/\351\235\242\350\257\225\351\242\23019. \346\255\243\345\210\231\350\241\250\350\276\276\345\274\217\345\214\271\351\205\215/README.md" index 60e7e922ce83d..02db5432f03f8 100644 --- "a/lcof/\351\235\242\350\257\225\351\242\23019. \346\255\243\345\210\231\350\241\250\350\276\276\345\274\217\345\214\271\351\205\215/README.md" +++ "b/lcof/\351\235\242\350\257\225\351\242\23019. \346\255\243\345\210\231\350\241\250\350\276\276\345\274\217\345\214\271\351\205\215/README.md" @@ -130,6 +130,33 @@ class Solution { } ``` +### JavaScript +```js +/** + * @param {string} s + * @param {string} p + * @return {boolean} + */ +var isMatch = function(s, p) { + // 回溯大法好 + let memo = {} + function recursive(i,j) { + if(memo[[i,j]] !== undefined) return memo[[i,j]] + if(j === p.length) return i === s.length + let tmp = i < s.length && (s[i] === p[j] || p[j] === '.') + let ans = false + if(p[j+1] === '*') { + ans = recursive(i,j+2) || tmp && recursive(i+1,j) + } else { + ans = tmp && recursive(i+1,j+1) + } + memo[[i,j]] = ans + return ans + } + return recursive(0,0) +}; +``` + ### ... ``` diff --git "a/lcof/\351\235\242\350\257\225\351\242\23019. \346\255\243\345\210\231\350\241\250\350\276\276\345\274\217\345\214\271\351\205\215/Solution.js" "b/lcof/\351\235\242\350\257\225\351\242\23019. \346\255\243\345\210\231\350\241\250\350\276\276\345\274\217\345\214\271\351\205\215/Solution.js" new file mode 100644 index 0000000000000..734eab92de1c4 --- /dev/null +++ "b/lcof/\351\235\242\350\257\225\351\242\23019. \346\255\243\345\210\231\350\241\250\350\276\276\345\274\217\345\214\271\351\205\215/Solution.js" @@ -0,0 +1,23 @@ +/** + * @param {string} s + * @param {string} p + * @return {boolean} + */ +var isMatch = function(s, p) { + // 回溯大法好 + let memo = {} + function recursive(i,j) { + if(memo[[i,j]] !== undefined) return memo[[i,j]] + if(j === p.length) return i === s.length + let tmp = i < s.length && (s[i] === p[j] || p[j] === '.') + let ans = false + if(p[j+1] === '*') { + ans = recursive(i,j+2) || tmp && recursive(i+1,j) + } else { + ans = tmp && recursive(i+1,j+1) + } + memo[[i,j]] = ans + return ans + } + return recursive(0,0) +}; \ No newline at end of file diff --git "a/lcof/\351\235\242\350\257\225\351\242\23020. \350\241\250\347\244\272\346\225\260\345\200\274\347\232\204\345\255\227\347\254\246\344\270\262/README.md" "b/lcof/\351\235\242\350\257\225\351\242\23020. \350\241\250\347\244\272\346\225\260\345\200\274\347\232\204\345\255\227\347\254\246\344\270\262/README.md" index e73745fb764b5..4d0725554f8cc 100644 --- "a/lcof/\351\235\242\350\257\225\351\242\23020. \350\241\250\347\244\272\346\225\260\345\200\274\347\232\204\345\255\227\347\254\246\344\270\262/README.md" +++ "b/lcof/\351\235\242\350\257\225\351\242\23020. \350\241\250\347\244\272\346\225\260\345\200\274\347\232\204\345\255\227\347\254\246\344\270\262/README.md" @@ -84,6 +84,17 @@ class Solution { } ``` +### JavaScript +```js +/** + * @param {string} s + * @return {boolean} + */ +var isNumber = function(s) { + return s !== ' ' && !isNaN(+s) +}; +``` + ### ... ``` diff --git "a/lcof/\351\235\242\350\257\225\351\242\23020. \350\241\250\347\244\272\346\225\260\345\200\274\347\232\204\345\255\227\347\254\246\344\270\262/Solution.js" "b/lcof/\351\235\242\350\257\225\351\242\23020. \350\241\250\347\244\272\346\225\260\345\200\274\347\232\204\345\255\227\347\254\246\344\270\262/Solution.js" new file mode 100644 index 0000000000000..1aa939067f7b4 --- /dev/null +++ "b/lcof/\351\235\242\350\257\225\351\242\23020. \350\241\250\347\244\272\346\225\260\345\200\274\347\232\204\345\255\227\347\254\246\344\270\262/Solution.js" @@ -0,0 +1,7 @@ +/** + * @param {string} s + * @return {boolean} + */ +var isNumber = function(s) { + return s !== ' ' && !isNaN(+s) +}; \ No newline at end of file diff --git "a/lcof/\351\235\242\350\257\225\351\242\23021. \350\260\203\346\225\264\346\225\260\347\273\204\351\241\272\345\272\217\344\275\277\345\245\207\346\225\260\344\275\215\344\272\216\345\201\266\346\225\260\345\211\215\351\235\242/README.md" "b/lcof/\351\235\242\350\257\225\351\242\23021. \350\260\203\346\225\264\346\225\260\347\273\204\351\241\272\345\272\217\344\275\277\345\245\207\346\225\260\344\275\215\344\272\216\345\201\266\346\225\260\345\211\215\351\235\242/README.md" index be24386e28993..6d5e283342a5c 100644 --- "a/lcof/\351\235\242\350\257\225\351\242\23021. \350\260\203\346\225\264\346\225\260\347\273\204\351\241\272\345\272\217\344\275\277\345\245\207\346\225\260\344\275\215\344\272\216\345\201\266\346\225\260\345\211\215\351\235\242/README.md" +++ "b/lcof/\351\235\242\350\257\225\351\242\23021. \350\260\203\346\225\264\346\225\260\347\273\204\351\241\272\345\272\217\344\275\277\345\245\207\346\225\260\344\275\215\344\272\216\345\201\266\346\225\260\345\211\215\351\235\242/README.md" @@ -52,6 +52,30 @@ class Solution { } ``` +### JavaScript +```js +/** + * @param {number[]} nums + * @return {number[]} + */ +var exchange = function(nums) { + let left = 0 + let right = nums.length-1 + while(left < right) { + let c = nums[left] + nums[left] = nums[right] + nums[right] = c + while(nums[left]%2) { + left++ + } + while(nums[right]%2===0) { + right-- + } + } + return nums +}; +``` + ### ... ``` diff --git "a/lcof/\351\235\242\350\257\225\351\242\23021. \350\260\203\346\225\264\346\225\260\347\273\204\351\241\272\345\272\217\344\275\277\345\245\207\346\225\260\344\275\215\344\272\216\345\201\266\346\225\260\345\211\215\351\235\242/Solution.js" "b/lcof/\351\235\242\350\257\225\351\242\23021. \350\260\203\346\225\264\346\225\260\347\273\204\351\241\272\345\272\217\344\275\277\345\245\207\346\225\260\344\275\215\344\272\216\345\201\266\346\225\260\345\211\215\351\235\242/Solution.js" new file mode 100644 index 0000000000000..041d5d3865c22 --- /dev/null +++ "b/lcof/\351\235\242\350\257\225\351\242\23021. \350\260\203\346\225\264\346\225\260\347\273\204\351\241\272\345\272\217\344\275\277\345\245\207\346\225\260\344\275\215\344\272\216\345\201\266\346\225\260\345\211\215\351\235\242/Solution.js" @@ -0,0 +1,20 @@ +/** + * @param {number[]} nums + * @return {number[]} + */ +var exchange = function(nums) { + let left = 0 + let right = nums.length-1 + while(left < right) { + let c = nums[left] + nums[left] = nums[right] + nums[right] = c + while(nums[left]%2) { + left++ + } + while(nums[right]%2===0) { + right-- + } + } + return nums +}; \ No newline at end of file diff --git "a/lcof/\351\235\242\350\257\225\351\242\23022. \351\223\276\350\241\250\344\270\255\345\200\222\346\225\260\347\254\254k\344\270\252\350\212\202\347\202\271/README.md" "b/lcof/\351\235\242\350\257\225\351\242\23022. \351\223\276\350\241\250\344\270\255\345\200\222\346\225\260\347\254\254k\344\270\252\350\212\202\347\202\271/README.md" index f169c24bd1361..d92099a9d1abb 100644 --- "a/lcof/\351\235\242\350\257\225\351\242\23022. \351\223\276\350\241\250\344\270\255\345\200\222\346\225\260\347\254\254k\344\270\252\350\212\202\347\202\271/README.md" +++ "b/lcof/\351\235\242\350\257\225\351\242\23022. \351\223\276\350\241\250\344\270\255\345\200\222\346\225\260\347\254\254k\344\270\252\350\212\202\347\202\271/README.md" @@ -62,6 +62,40 @@ class Solution { } ``` +### JavaScript +```js +/** + * @param {ListNode} head + * @param {number} k + * @return {ListNode} + */ +var getKthFromEnd = function(head, k) { + // 递归 + // let cnt = 1 + // function func(node) { + // if(!node || !node.next) return node + // let newNode = func(node.next) + // if(cnt === k) return newNode + // else cnt++ + // return node + // } + // return func(head) + + // 快慢指针 + let slow = head + let fast = head + while(k) { + fast = fast.next + k-- + } + while(fast) { + slow = slow.next + fast = fast.next + } + return slow +}; +``` + ### ... ``` diff --git "a/lcof/\351\235\242\350\257\225\351\242\23022. \351\223\276\350\241\250\344\270\255\345\200\222\346\225\260\347\254\254k\344\270\252\350\212\202\347\202\271/Solution.js" "b/lcof/\351\235\242\350\257\225\351\242\23022. \351\223\276\350\241\250\344\270\255\345\200\222\346\225\260\347\254\254k\344\270\252\350\212\202\347\202\271/Solution.js" new file mode 100644 index 0000000000000..4cda330163a80 --- /dev/null +++ "b/lcof/\351\235\242\350\257\225\351\242\23022. \351\223\276\350\241\250\344\270\255\345\200\222\346\225\260\347\254\254k\344\270\252\350\212\202\347\202\271/Solution.js" @@ -0,0 +1,30 @@ +/** + * @param {ListNode} head + * @param {number} k + * @return {ListNode} + */ +var getKthFromEnd = function(head, k) { + // 递归 + // let cnt = 1 + // function func(node) { + // if(!node || !node.next) return node + // let newNode = func(node.next) + // if(cnt === k) return newNode + // else cnt++ + // return node + // } + // return func(head) + + // 快慢指针 + let slow = head + let fast = head + while(k) { + fast = fast.next + k-- + } + while(fast) { + slow = slow.next + fast = fast.next + } + return slow +}; \ No newline at end of file diff --git "a/lcof/\351\235\242\350\257\225\351\242\23024. \345\217\215\350\275\254\351\223\276\350\241\250/README.md" "b/lcof/\351\235\242\350\257\225\351\242\23024. \345\217\215\350\275\254\351\223\276\350\241\250/README.md" index 58ed69b302752..7b4c8f6af1482 100644 --- "a/lcof/\351\235\242\350\257\225\351\242\23024. \345\217\215\350\275\254\351\223\276\350\241\250/README.md" +++ "b/lcof/\351\235\242\350\257\225\351\242\23024. \345\217\215\350\275\254\351\223\276\350\241\250/README.md" @@ -64,6 +64,32 @@ class Solution { } ``` +### JavaScript +```js +/** + * Definition for singly-linked list. + * function ListNode(val) { + * this.val = val; + * this.next = null; + * } + */ +/** + * @param {ListNode} head + * @return {ListNode} + */ +var reverseList = function(head) { + let node = head + let pre = null + while(node) { + let cur = node + node = cur.next + cur.next = pre + pre = cur + } + return pre +}; +``` + ### ... ``` diff --git "a/lcof/\351\235\242\350\257\225\351\242\23024. \345\217\215\350\275\254\351\223\276\350\241\250/Solution.js" "b/lcof/\351\235\242\350\257\225\351\242\23024. \345\217\215\350\275\254\351\223\276\350\241\250/Solution.js" new file mode 100644 index 0000000000000..174b7ed781f80 --- /dev/null +++ "b/lcof/\351\235\242\350\257\225\351\242\23024. \345\217\215\350\275\254\351\223\276\350\241\250/Solution.js" @@ -0,0 +1,22 @@ +/** + * Definition for singly-linked list. + * function ListNode(val) { + * this.val = val; + * this.next = null; + * } + */ +/** + * @param {ListNode} head + * @return {ListNode} + */ +var reverseList = function(head) { + let node = head + let pre = null + while(node) { + let cur = node + node = cur.next + cur.next = pre + pre = cur + } + return pre +}; \ No newline at end of file diff --git "a/lcof/\351\235\242\350\257\225\351\242\23025. \345\220\210\345\271\266\344\270\244\344\270\252\346\216\222\345\272\217\347\232\204\351\223\276\350\241\250/README.md" "b/lcof/\351\235\242\350\257\225\351\242\23025. \345\220\210\345\271\266\344\270\244\344\270\252\346\216\222\345\272\217\347\232\204\351\223\276\350\241\250/README.md" index ea55a17bb0119..ac92c67e7b64d 100644 --- "a/lcof/\351\235\242\350\257\225\351\242\23025. \345\220\210\345\271\266\344\270\244\344\270\252\346\216\222\345\272\217\347\232\204\351\223\276\350\241\250/README.md" +++ "b/lcof/\351\235\242\350\257\225\351\242\23025. \345\220\210\345\271\266\344\270\244\344\270\252\346\216\222\345\272\217\347\232\204\351\223\276\350\241\250/README.md" @@ -85,6 +85,57 @@ class Solution { } ``` +### JavaScript +```js +/** + * Definition for singly-linked list. + * function ListNode(val) { + * this.val = val; + * this.next = null; + * } + */ +/** + * @param {ListNode} l1 + * @param {ListNode} l2 + * @return {ListNode} + */ +var mergeTwoLists = function(l1, l2) { + // 法一 - 递归 + if (!l1) return l2 + if (!l2) return l1 + if (l1.val < l2.val) { + l1.next = mergeTwoLists(l1.next, l2) + return l1 + } else { + l2.next = mergeTwoLists(l2.next, l1) + return l2 + } + // 法二 - 遍历 + // if(!l1 || !l2) return l1 ? l1 : l2 + // let a = l1 + // let b = l2 + // let res = l1 + // if(a.val > b.val) { + // let c = a + // a = b + // b = c + // res = l2 + // } + // while(a && b) { + // while(a.next && a.next.val < b.val) { + // a = a.next + // } + // let tmp = a.next + // let rec = b.next + // a.next = b + // a.next.next = tmp + // a = a.next + // b = rec + // } + // return res +}; +``` + ### ... ``` diff --git "a/lcof/\351\235\242\350\257\225\351\242\23025. \345\220\210\345\271\266\344\270\244\344\270\252\346\216\222\345\272\217\347\232\204\351\223\276\350\241\250/Solution.js" "b/lcof/\351\235\242\350\257\225\351\242\23025. \345\220\210\345\271\266\344\270\244\344\270\252\346\216\222\345\272\217\347\232\204\351\223\276\350\241\250/Solution.js" new file mode 100644 index 0000000000000..ddfb6a5343c94 --- /dev/null +++ "b/lcof/\351\235\242\350\257\225\351\242\23025. \345\220\210\345\271\266\344\270\244\344\270\252\346\216\222\345\272\217\347\232\204\351\223\276\350\241\250/Solution.js" @@ -0,0 +1,47 @@ +/** + * Definition for singly-linked list. + * function ListNode(val) { + * this.val = val; + * this.next = null; + * } + */ +/** + * @param {ListNode} l1 + * @param {ListNode} l2 + * @return {ListNode} + */ +var mergeTwoLists = function(l1, l2) { + // 法一 - 递归 + if (!l1) return l2 + if (!l2) return l1 + if (l1.val < l2.val) { + l1.next = mergeTwoLists(l1.next, l2) + return l1 + } else { + l2.next = mergeTwoLists(l2.next, l1) + return l2 + } + // 法二 - 遍历 + // if(!l1 || !l2) return l1 ? l1 : l2 + // let a = l1 + // let b = l2 + // let res = l1 + // if(a.val > b.val) { + // let c = a + // a = b + // b = c + // res = l2 + // } + // while(a && b) { + // while(a.next && a.next.val < b.val) { + // a = a.next + // } + // let tmp = a.next + // let rec = b.next + // a.next = b + // a.next.next = tmp + // a = a.next + // b = rec + // } + // return res +}; \ No newline at end of file diff --git "a/lcof/\351\235\242\350\257\225\351\242\23026. \346\240\221\347\232\204\345\255\220\347\273\223\346\236\204/README.md" "b/lcof/\351\235\242\350\257\225\351\242\23026. \346\240\221\347\232\204\345\255\220\347\273\223\346\236\204/README.md" index 52cccc072e0d2..2f95dddfd8661 100644 --- "a/lcof/\351\235\242\350\257\225\351\242\23026. \346\240\221\347\232\204\345\255\220\347\273\223\346\236\204/README.md" +++ "b/lcof/\351\235\242\350\257\225\351\242\23026. \346\240\221\347\232\204\345\255\220\347\273\223\346\236\204/README.md" @@ -118,6 +118,49 @@ class Solution { } ``` +### JavaScript +```js +/** + * Definition for a binary tree node. + * function TreeNode(val) { + * this.val = val; + * this.left = this.right = null; + * } + */ +/** + * @param {TreeNode} A + * @param {TreeNode} B + * @return {boolean} + */ +var isSubStructure = function(A, B) { + if(!B || !A) return false + let res + function dfs(A,B,bool) { + if(!A || !B) { + if(B) { + return false + } else { + return true + } + } + if(A.val === B.val) { + let left = dfs(A.left,B.left,true) + let right = dfs(A.right,B.right,true) + if(left && right) return true + else return false + } else { + if(bool) return false + else { + let left = dfs(A.left,B,false) + let right = dfs(A.right,B,false) + return left || right + } + } + } + return dfs(A,B,false) || false +}; +``` + ### ... ``` diff --git "a/lcof/\351\235\242\350\257\225\351\242\23026. \346\240\221\347\232\204\345\255\220\347\273\223\346\236\204/Solution.js" "b/lcof/\351\235\242\350\257\225\351\242\23026. \346\240\221\347\232\204\345\255\220\347\273\223\346\236\204/Solution.js" new file mode 100644 index 0000000000000..14497eff83d11 --- /dev/null +++ "b/lcof/\351\235\242\350\257\225\351\242\23026. \346\240\221\347\232\204\345\255\220\347\273\223\346\236\204/Solution.js" @@ -0,0 +1,39 @@ +/** + * Definition for a binary tree node. + * function TreeNode(val) { + * this.val = val; + * this.left = this.right = null; + * } + */ +/** + * @param {TreeNode} A + * @param {TreeNode} B + * @return {boolean} + */ +var isSubStructure = function(A, B) { + if(!B || !A) return false + let res + function dfs(A,B,bool) { + if(!A || !B) { + if(B) { + return false + } else { + return true + } + } + if(A.val === B.val) { + let left = dfs(A.left,B.left,true) + let right = dfs(A.right,B.right,true) + if(left && right) return true + else return false + } else { + if(bool) return false + else { + let left = dfs(A.left,B,false) + let right = dfs(A.right,B,false) + return left || right + } + } + } + return dfs(A,B,false) || false +}; \ No newline at end of file diff --git "a/lcof/\351\235\242\350\257\225\351\242\23027. \344\272\214\345\217\211\346\240\221\347\232\204\351\225\234\345\203\217/README.md" "b/lcof/\351\235\242\350\257\225\351\242\23027. \344\272\214\345\217\211\346\240\221\347\232\204\351\225\234\345\203\217/README.md" index 80259897b2931..782810822f68b 100644 --- "a/lcof/\351\235\242\350\257\225\351\242\23027. \344\272\214\345\217\211\346\240\221\347\232\204\351\225\234\345\203\217/README.md" +++ "b/lcof/\351\235\242\350\257\225\351\242\23027. \344\272\214\345\217\211\346\240\221\347\232\204\351\225\234\345\203\217/README.md" @@ -81,6 +81,32 @@ class Solution { } ``` +### JavaScript +```js +/** + * Definition for a binary tree node. + * function TreeNode(val) { + * this.val = val; + * this.left = this.right = null; + * } + */ +/** + * @param {TreeNode} root + * @return {TreeNode} + */ +var mirrorTree = function(root) { + function dfs(node) { + if(!node) return null + let left = dfs(node.left) + let right = dfs(node.right) + node.left = right + node.right = left + return node + } + return dfs(root) +}; +``` + ### ... ``` diff --git "a/lcof/\351\235\242\350\257\225\351\242\23027. \344\272\214\345\217\211\346\240\221\347\232\204\351\225\234\345\203\217/Solution.js" "b/lcof/\351\235\242\350\257\225\351\242\23027. \344\272\214\345\217\211\346\240\221\347\232\204\351\225\234\345\203\217/Solution.js" new file mode 100644 index 0000000000000..658dce7b33f14 --- /dev/null +++ "b/lcof/\351\235\242\350\257\225\351\242\23027. \344\272\214\345\217\211\346\240\221\347\232\204\351\225\234\345\203\217/Solution.js" @@ -0,0 +1,22 @@ +/** + * Definition for a binary tree node. + * function TreeNode(val) { + * this.val = val; + * this.left = this.right = null; + * } + */ +/** + * @param {TreeNode} root + * @return {TreeNode} + */ +var mirrorTree = function(root) { + function dfs(node) { + if(!node) return null + let left = dfs(node.left) + let right = dfs(node.right) + node.left = right + node.right = left + return node + } + return dfs(root) +}; \ No newline at end of file diff --git "a/lcof/\351\235\242\350\257\225\351\242\23028. \345\257\271\347\247\260\347\232\204\344\272\214\345\217\211\346\240\221/README.md" "b/lcof/\351\235\242\350\257\225\351\242\23028. \345\257\271\347\247\260\347\232\204\344\272\214\345\217\211\346\240\221/README.md" index 1c94c8b8c9582..16aa91f3eccfd 100644 --- "a/lcof/\351\235\242\350\257\225\351\242\23028. \345\257\271\347\247\260\347\232\204\344\272\214\345\217\211\346\240\221/README.md" +++ "b/lcof/\351\235\242\350\257\225\351\242\23028. \345\257\271\347\247\260\347\232\204\344\272\214\345\217\211\346\240\221/README.md" @@ -96,6 +96,30 @@ class Solution { } ``` +### JavaScript +```js +/** + * Definition for a binary tree node. + * function TreeNode(val) { + * this.val = val; + * this.left = this.right = null; + * } + */ +/** + * @param {TreeNode} root + * @return {boolean} + */ +var isSymmetric = function(root) { + function dfs(a,b) { + if(!a && !b) return true + if(!a || !b) return false + return a.val === b.val && dfs(a.left,b.right) && dfs(a.right,b.left) + } + if(!root) return true + return dfs(root.left,root.right) +}; +``` + ### ... ``` diff --git "a/lcof/\351\235\242\350\257\225\351\242\23028. \345\257\271\347\247\260\347\232\204\344\272\214\345\217\211\346\240\221/Solution.js" "b/lcof/\351\235\242\350\257\225\351\242\23028. \345\257\271\347\247\260\347\232\204\344\272\214\345\217\211\346\240\221/Solution.js" new file mode 100644 index 0000000000000..34de87681578e --- /dev/null +++ "b/lcof/\351\235\242\350\257\225\351\242\23028. \345\257\271\347\247\260\347\232\204\344\272\214\345\217\211\346\240\221/Solution.js" @@ -0,0 +1,20 @@ +/** + * Definition for a binary tree node. + * function TreeNode(val) { + * this.val = val; + * this.left = this.right = null; + * } + */ +/** + * @param {TreeNode} root + * @return {boolean} + */ +var isSymmetric = function(root) { + function dfs(a,b) { + if(!a && !b) return true + if(!a || !b) return false + return a.val === b.val && dfs(a.left,b.right) && dfs(a.right,b.left) + } + if(!root) return true + return dfs(root.left,root.right) +}; \ No newline at end of file diff --git "a/lcof/\351\235\242\350\257\225\351\242\23029. \351\241\272\346\227\266\351\222\210\346\211\223\345\215\260\347\237\251\351\230\265/README.md" "b/lcof/\351\235\242\350\257\225\351\242\23029. \351\241\272\346\227\266\351\222\210\346\211\223\345\215\260\347\237\251\351\230\265/README.md" index 7cf7a94b6a394..93867e7d48224 100644 --- "a/lcof/\351\235\242\350\257\225\351\242\23029. \351\241\272\346\227\266\351\222\210\346\211\223\345\215\260\347\237\251\351\230\265/README.md" +++ "b/lcof/\351\235\242\350\257\225\351\242\23029. \351\241\272\346\227\266\351\222\210\346\211\223\345\215\260\347\237\251\351\230\265/README.md" @@ -97,6 +97,44 @@ class Solution { } ``` +### JavaScript +```js +/** + * @param {number[][]} matrix + * @return {number[]} + */ +var spiralOrder = function(matrix) { + if(!matrix || !matrix.length) return [] + let row = matrix.length + let col = matrix[0].length + let res = [] + let moves = { + 'right': [0,1], + 'down': [1,0], + 'left': [0,-1], + 'up': [-1,0] + } + let k = 0 + function dfs(i,j,dir) { + if(i < 0 || j < 0 || i >= row || j >= col || res.length === row * col) { + return + } + res.push(matrix[i][j]) + switch(dir) { + case 'right': if(j === col-1 - k) dir = 'down';break + case 'down': if(i === row-1 - k) dir = 'left';break + case 'left': if(j === k) { dir = 'up';k++; } break + case 'up': if(i === k) dir = 'right';break + } + let x = i+moves[dir][0] + let y = j+moves[dir][1] + dfs(x,y,dir) + } + dfs(0,0,'right') + return res +}; +``` + ### ... ``` diff --git "a/lcof/\351\235\242\350\257\225\351\242\23029. \351\241\272\346\227\266\351\222\210\346\211\223\345\215\260\347\237\251\351\230\265/Solution.js" "b/lcof/\351\235\242\350\257\225\351\242\23029. \351\241\272\346\227\266\351\222\210\346\211\223\345\215\260\347\237\251\351\230\265/Solution.js" new file mode 100644 index 0000000000000..21b86cd33ef64 --- /dev/null +++ "b/lcof/\351\235\242\350\257\225\351\242\23029. \351\241\272\346\227\266\351\222\210\346\211\223\345\215\260\347\237\251\351\230\265/Solution.js" @@ -0,0 +1,34 @@ +/** + * @param {number[][]} matrix + * @return {number[]} + */ +var spiralOrder = function(matrix) { + if(!matrix || !matrix.length) return [] + let row = matrix.length + let col = matrix[0].length + let res = [] + let moves = { + 'right': [0,1], + 'down': [1,0], + 'left': [0,-1], + 'up': [-1,0] + } + let k = 0 + function dfs(i,j,dir) { + if(i < 0 || j < 0 || i >= row || j >= col || res.length === row * col) { + return + } + res.push(matrix[i][j]) + switch(dir) { + case 'right': if(j === col-1 - k) dir = 'down';break + case 'down': if(i === row-1 - k) dir = 'left';break + case 'left': if(j === k) { dir = 'up';k++; } break + case 'up': if(i === k) dir = 'right';break + } + let x = i+moves[dir][0] + let y = j+moves[dir][1] + dfs(x,y,dir) + } + dfs(0,0,'right') + return res +}; \ No newline at end of file diff --git "a/lcof/\351\235\242\350\257\225\351\242\23030. \345\214\205\345\220\253min\345\207\275\346\225\260\347\232\204\346\240\210/README.md" "b/lcof/\351\235\242\350\257\225\351\242\23030. \345\214\205\345\220\253min\345\207\275\346\225\260\347\232\204\346\240\210/README.md" index 9de68382215a4..c670a40141906 100644 --- "a/lcof/\351\235\242\350\257\225\351\242\23030. \345\214\205\345\220\253min\345\207\275\346\225\260\347\232\204\346\240\210/README.md" +++ "b/lcof/\351\235\242\350\257\225\351\242\23030. \345\214\205\345\220\253min\345\207\275\346\225\260\347\232\204\346\240\210/README.md" @@ -96,6 +96,60 @@ class MinStack { */ ``` +### JavaScript +```js +/** + * initialize your data structure here. + */ +var MinStack = function() { + this.stack = [] + this.minStack = [] +}; + +/** + * @param {number} x + * @return {void} + */ +MinStack.prototype.push = function(x) { + this.stack.unshift(x) + if(!this.minStack.length || this.minStack[0] >= x) { + this.minStack.unshift(x) + } +}; + +/** + * @return {void} + */ +MinStack.prototype.pop = function() { + if(this.stack.shift() === this.minStack[0]) { + this.minStack.shift() + } +}; + +/** + * @return {number} + */ +MinStack.prototype.top = function() { + return this.stack[0] +}; + +/** + * @return {number} + */ +MinStack.prototype.min = function() { + return this.minStack[0] +}; + +/** + * Your MinStack object will be instantiated and called as such: + * var obj = new MinStack() + * obj.push(x) + * obj.pop() + * var param_3 = obj.top() + * var param_4 = obj.min() + */ +``` + ### ... ``` diff --git "a/lcof/\351\235\242\350\257\225\351\242\23030. \345\214\205\345\220\253min\345\207\275\346\225\260\347\232\204\346\240\210/Solution.js" "b/lcof/\351\235\242\350\257\225\351\242\23030. \345\214\205\345\220\253min\345\207\275\346\225\260\347\232\204\346\240\210/Solution.js" new file mode 100644 index 0000000000000..4593816272b44 --- /dev/null +++ "b/lcof/\351\235\242\350\257\225\351\242\23030. \345\214\205\345\220\253min\345\207\275\346\225\260\347\232\204\346\240\210/Solution.js" @@ -0,0 +1,50 @@ +/** + * initialize your data structure here. + */ +var MinStack = function() { + this.stack = [] + this.minStack = [] +}; + +/** + * @param {number} x + * @return {void} + */ +MinStack.prototype.push = function(x) { + this.stack.unshift(x) + if(!this.minStack.length || this.minStack[0] >= x) { + this.minStack.unshift(x) + } +}; + +/** + * @return {void} + */ +MinStack.prototype.pop = function() { + if(this.stack.shift() === this.minStack[0]) { + this.minStack.shift() + } +}; + +/** + * @return {number} + */ +MinStack.prototype.top = function() { + return this.stack[0] +}; + +/** + * @return {number} + */ +MinStack.prototype.min = function() { + return this.minStack[0] +}; + +/** + * Your MinStack object will be instantiated and called as such: + * var obj = new MinStack() + * obj.push(x) + * obj.pop() + * var param_3 = obj.top() + * var param_4 = obj.min() + */ \ No newline at end of file diff --git "a/lcof/\351\235\242\350\257\225\351\242\23031. \346\240\210\347\232\204\345\216\213\345\205\245\343\200\201\345\274\271\345\207\272\345\272\217\345\210\227/README.md" "b/lcof/\351\235\242\350\257\225\351\242\23031. \346\240\210\347\232\204\345\216\213\345\205\245\343\200\201\345\274\271\345\207\272\345\272\217\345\210\227/README.md" index 91358cda531c6..c138b03cab8e0 100644 --- "a/lcof/\351\235\242\350\257\225\351\242\23031. \346\240\210\347\232\204\345\216\213\345\205\245\343\200\201\345\274\271\345\207\272\345\272\217\345\210\227/README.md" +++ "b/lcof/\351\235\242\350\257\225\351\242\23031. \346\240\210\347\232\204\345\216\213\345\205\245\343\200\201\345\274\271\345\207\272\345\272\217\345\210\227/README.md" @@ -72,6 +72,35 @@ class Solution { } ``` +### JavaScript +```js +/** + * @param {number[]} pushed + * @param {number[]} popped + * @return {boolean} + */ +var validateStackSequences = function(pushed, popped) { + let stack = [] + while(pushed.length && popped.length) { + if(pushed[0] === popped[0]) { + pushed.shift() + popped.shift() + } else if(popped[0] === stack[0]) { + stack.shift() + popped.shift() + } else { + stack.unshift(pushed.shift()) + } + } + while(stack.length) { + if(stack[0] !== popped[0]) return false + stack.shift() + popped.shift() + } + return true +}; +``` + ### ... ``` diff --git "a/lcof/\351\235\242\350\257\225\351\242\23031. \346\240\210\347\232\204\345\216\213\345\205\245\343\200\201\345\274\271\345\207\272\345\272\217\345\210\227/Solution.js" "b/lcof/\351\235\242\350\257\225\351\242\23031. \346\240\210\347\232\204\345\216\213\345\205\245\343\200\201\345\274\271\345\207\272\345\272\217\345\210\227/Solution.js" new file mode 100644 index 0000000000000..fe9857e320dfa --- /dev/null +++ "b/lcof/\351\235\242\350\257\225\351\242\23031. \346\240\210\347\232\204\345\216\213\345\205\245\343\200\201\345\274\271\345\207\272\345\272\217\345\210\227/Solution.js" @@ -0,0 +1,25 @@ +/** + * @param {number[]} pushed + * @param {number[]} popped + * @return {boolean} + */ +var validateStackSequences = function(pushed, popped) { + let stack = [] + while(pushed.length && popped.length) { + if(pushed[0] === popped[0]) { + pushed.shift() + popped.shift() + } else if(popped[0] === stack[0]) { + stack.shift() + popped.shift() + } else { + stack.unshift(pushed.shift()) + } + } + while(stack.length) { + if(stack[0] !== popped[0]) return false + stack.shift() + popped.shift() + } + return true +}; \ No newline at end of file diff --git "a/lcof/\351\235\242\350\257\225\351\242\23032 - I. \344\273\216\344\270\212\345\210\260\344\270\213\346\211\223\345\215\260\344\272\214\345\217\211\346\240\221/README.md" "b/lcof/\351\235\242\350\257\225\351\242\23032 - I. \344\273\216\344\270\212\345\210\260\344\270\213\346\211\223\345\215\260\344\272\214\345\217\211\346\240\221/README.md" index ae9b9739db2e6..34d8a256239fd 100644 --- "a/lcof/\351\235\242\350\257\225\351\242\23032 - I. \344\273\216\344\270\212\345\210\260\344\270\213\346\211\223\345\215\260\344\272\214\345\217\211\346\240\221/README.md" +++ "b/lcof/\351\235\242\350\257\225\351\242\23032 - I. \344\273\216\344\270\212\345\210\260\344\270\213\346\211\223\345\215\260\344\272\214\345\217\211\346\240\221/README.md" @@ -95,6 +95,33 @@ class Solution { } ``` +### JavaScript +```js +/** + * Definition for a binary tree node. + * function TreeNode(val) { + * this.val = val; + * this.left = this.right = null; + * } + */ +/** + * @param {TreeNode} root + * @return {number[]} + */ +var levelOrder = function(root) { + if(!root) return [] + let queue = [root] + let res = [] + while(queue.length) { + let node = queue.shift() + if(!node) continue + res.push(node.val) + queue.push(node.left,node.right) + } + return res +}; +``` + ### ... ``` diff --git "a/lcof/\351\235\242\350\257\225\351\242\23032 - I. \344\273\216\344\270\212\345\210\260\344\270\213\346\211\223\345\215\260\344\272\214\345\217\211\346\240\221/Solution.js" "b/lcof/\351\235\242\350\257\225\351\242\23032 - I. \344\273\216\344\270\212\345\210\260\344\270\213\346\211\223\345\215\260\344\272\214\345\217\211\346\240\221/Solution.js" new file mode 100644 index 0000000000000..be2973fdb59c6 --- /dev/null +++ "b/lcof/\351\235\242\350\257\225\351\242\23032 - I. \344\273\216\344\270\212\345\210\260\344\270\213\346\211\223\345\215\260\344\272\214\345\217\211\346\240\221/Solution.js" @@ -0,0 +1,23 @@ +/** + * Definition for a binary tree node. + * function TreeNode(val) { + * this.val = val; + * this.left = this.right = null; + * } + */ +/** + * @param {TreeNode} root + * @return {number[]} + */ +var levelOrder = function(root) { + if(!root) return [] + let queue = [root] + let res = [] + while(queue.length) { + let node = queue.shift() + if(!node) continue + res.push(node.val) + queue.push(node.left,node.right) + } + return res +}; \ No newline at end of file diff --git "a/lcof/\351\235\242\350\257\225\351\242\23032 - II. \344\273\216\344\270\212\345\210\260\344\270\213\346\211\223\345\215\260\344\272\214\345\217\211\346\240\221 II/README.md" "b/lcof/\351\235\242\350\257\225\351\242\23032 - II. \344\273\216\344\270\212\345\210\260\344\270\213\346\211\223\345\215\260\344\272\214\345\217\211\346\240\221 II/README.md" index a6d178161a353..6fd2d22a5057e 100644 --- "a/lcof/\351\235\242\350\257\225\351\242\23032 - II. \344\273\216\344\270\212\345\210\260\344\270\213\346\211\223\345\215\260\344\272\214\345\217\211\346\240\221 II/README.md" +++ "b/lcof/\351\235\242\350\257\225\351\242\23032 - II. \344\273\216\344\270\212\345\210\260\344\270\213\346\211\223\345\215\260\344\272\214\345\217\211\346\240\221 II/README.md" @@ -109,6 +109,39 @@ class Solution { } ``` +### JavaScript +```js +/** + * Definition for a binary tree node. + * function TreeNode(val) { + * this.val = val; + * this.left = this.right = null; + * } + */ +/** + * @param {TreeNode} root + * @return {number[][]} + */ +var levelOrder = function(root) { + if(!root) return [] + let queue = [root] + let res = [] + let depth = 0 + while(queue.length) { + let len = queue.length + for(let i=0;i= 0) { + arr[i].left = arr[i-1] + } else { + arr[i].left = arr[len-1] + } + } + return res +}; +``` + ### ... ``` diff --git "a/lcof/\351\235\242\350\257\225\351\242\23036. \344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\216\345\217\214\345\220\221\351\223\276\350\241\250/Solution.js" "b/lcof/\351\235\242\350\257\225\351\242\23036. \344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\216\345\217\214\345\220\221\351\223\276\350\241\250/Solution.js" new file mode 100644 index 0000000000000..fb321a85a25a0 --- /dev/null +++ "b/lcof/\351\235\242\350\257\225\351\242\23036. \344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\344\270\216\345\217\214\345\220\221\351\223\276\350\241\250/Solution.js" @@ -0,0 +1,38 @@ +/** + * // Definition for a Node. + * function Node(val, left, right) { + * this.val = val; + * this.left = left; + * this.right = right; + * }; + */ +/** + * @param {Node} root + * @return {Node} + */ +var treeToDoublyList = function(root) { + if(!root) return null + function dfs(node) { + if(!node) return null + dfs(node.left) + arr.push(node) + dfs(node.right) + } + let arr = [] + dfs(root) + let len = arr.length + let res = arr[0] + for(let i=0;i= 0) { + arr[i].left = arr[i-1] + } else { + arr[i].left = arr[len-1] + } + } + return res +}; \ No newline at end of file diff --git "a/lcof/\351\235\242\350\257\225\351\242\23037. \345\272\217\345\210\227\345\214\226\344\272\214\345\217\211\346\240\221/README.md" "b/lcof/\351\235\242\350\257\225\351\242\23037. \345\272\217\345\210\227\345\214\226\344\272\214\345\217\211\346\240\221/README.md" index 2835648fc6956..9e5585dbef46a 100644 --- "a/lcof/\351\235\242\350\257\225\351\242\23037. \345\272\217\345\210\227\345\214\226\344\272\214\345\217\211\346\240\221/README.md" +++ "b/lcof/\351\235\242\350\257\225\351\242\23037. \345\272\217\345\210\227\345\214\226\344\272\214\345\217\211\346\240\221/README.md" @@ -159,6 +159,72 @@ public class Codec { // codec.deserialize(codec.serialize(root)); ``` +### JavaScript +```js +/** + * Definition for a binary tree node. + * function TreeNode(val) { + * this.val = val; + * this.left = this.right = null; + * } + */ + +/** + * Encodes a tree to a single string. + * + * @param {TreeNode} root + * @return {string} + */ +var serialize = function(root) { + if(!root) return '[]' + let queue = [root] + let res = '' + while(queue.length) { + let node = queue.shift() + if(node) { + res += node.val + ',' + queue.push(node.left) + queue.push(node.right) + } else { + res += 'null' + ',' + } + } + return `[${res.substring(0,res.length-1)}]` +}; + +/** + * Decodes your encoded data to tree. + * + * @param {string} data + * @return {TreeNode} + */ +var deserialize = function(data) { + if(!data || data.length <= 2) return null + let arr = data.substring(1,data.length-1).split(',') + let root = new TreeNode(arr.shift()) + let queue = [root] + while(queue.length) { + let node = queue.shift() + let leftVal = arr.shift() + if(leftVal !== 'null') { + node.left = new TreeNode(leftVal) + queue.push(node.left) + } + let rightVal = arr.shift() + if(rightVal !== 'null') { + node.right = new TreeNode(rightVal) + queue.push(node.right) + } + } + return root +}; + +/** + * Your functions will be called as such: + * deserialize(serialize(root)); + */ +``` + ### ... ``` diff --git "a/lcof/\351\235\242\350\257\225\351\242\23037. \345\272\217\345\210\227\345\214\226\344\272\214\345\217\211\346\240\221/Solution.js" "b/lcof/\351\235\242\350\257\225\351\242\23037. \345\272\217\345\210\227\345\214\226\344\272\214\345\217\211\346\240\221/Solution.js" new file mode 100644 index 0000000000000..c20e0c3b3001b --- /dev/null +++ "b/lcof/\351\235\242\350\257\225\351\242\23037. \345\272\217\345\210\227\345\214\226\344\272\214\345\217\211\346\240\221/Solution.js" @@ -0,0 +1,62 @@ +/** + * Definition for a binary tree node. + * function TreeNode(val) { + * this.val = val; + * this.left = this.right = null; + * } + */ + +/** + * Encodes a tree to a single string. + * + * @param {TreeNode} root + * @return {string} + */ +var serialize = function(root) { + if(!root) return '[]' + let queue = [root] + let res = '' + while(queue.length) { + let node = queue.shift() + if(node) { + res += node.val + ',' + queue.push(node.left) + queue.push(node.right) + } else { + res += 'null' + ',' + } + } + return `[${res.substring(0,res.length-1)}]` +}; + +/** + * Decodes your encoded data to tree. + * + * @param {string} data + * @return {TreeNode} + */ +var deserialize = function(data) { + if(!data || data.length <= 2) return null + let arr = data.substring(1,data.length-1).split(',') + let root = new TreeNode(arr.shift()) + let queue = [root] + while(queue.length) { + let node = queue.shift() + let leftVal = arr.shift() + if(leftVal !== 'null') { + node.left = new TreeNode(leftVal) + queue.push(node.left) + } + let rightVal = arr.shift() + if(rightVal !== 'null') { + node.right = new TreeNode(rightVal) + queue.push(node.right) + } + } + return root +}; + +/** + * Your functions will be called as such: + * deserialize(serialize(root)); + */ \ No newline at end of file diff --git "a/lcof/\351\235\242\350\257\225\351\242\23038. \345\255\227\347\254\246\344\270\262\347\232\204\346\216\222\345\210\227/README.md" "b/lcof/\351\235\242\350\257\225\351\242\23038. \345\255\227\347\254\246\344\270\262\347\232\204\346\216\222\345\210\227/README.md" index 8b423d27aa5b0..3260719421505 100644 --- "a/lcof/\351\235\242\350\257\225\351\242\23038. \345\255\227\347\254\246\344\270\262\347\232\204\346\216\222\345\210\227/README.md" +++ "b/lcof/\351\235\242\350\257\225\351\242\23038. \345\255\227\347\254\246\344\270\262\347\232\204\346\216\222\345\210\227/README.md" @@ -84,6 +84,32 @@ class Solution { } ``` +### JavaScript +```js +/** + * @param {string} s + * @return {string[]} + */ +var permutation = function(s) { + let len = s.length + let res = new Set() + function dfs(str, isRead) { + if(str.length === len) { + res.add(str) + return + } + for(let i=0;ia-b)[~~(nums.length/2)] +}; +``` + ### ... ``` diff --git "a/lcof/\351\235\242\350\257\225\351\242\23039. \346\225\260\347\273\204\344\270\255\345\207\272\347\216\260\346\254\241\346\225\260\350\266\205\350\277\207\344\270\200\345\215\212\347\232\204\346\225\260\345\255\227/Solution.js" "b/lcof/\351\235\242\350\257\225\351\242\23039. \346\225\260\347\273\204\344\270\255\345\207\272\347\216\260\346\254\241\346\225\260\350\266\205\350\277\207\344\270\200\345\215\212\347\232\204\346\225\260\345\255\227/Solution.js" new file mode 100644 index 0000000000000..e36ecfcc4512c --- /dev/null +++ "b/lcof/\351\235\242\350\257\225\351\242\23039. \346\225\260\347\273\204\344\270\255\345\207\272\347\216\260\346\254\241\346\225\260\350\266\205\350\277\207\344\270\200\345\215\212\347\232\204\346\225\260\345\255\227/Solution.js" @@ -0,0 +1,19 @@ +/** + * @param {number[]} nums + * @return {number} + */ +var majorityElement = function(nums) { + let cnt = 0 + let mode = -1 + for(let num of nums) { + if(!cnt) { + mode = num + cnt++ + } else { + if(mode === num) cnt++ + else cnt-- + } + } + return mode + // return nums.sort((a,b)=>a-b)[~~(nums.length/2)] +}; \ No newline at end of file diff --git "a/lcof/\351\235\242\350\257\225\351\242\23040. \346\234\200\345\260\217\347\232\204k\344\270\252\346\225\260/README.md" "b/lcof/\351\235\242\350\257\225\351\242\23040. \346\234\200\345\260\217\347\232\204k\344\270\252\346\225\260/README.md" index ef1ba56f895de..e939113d287fd 100644 --- "a/lcof/\351\235\242\350\257\225\351\242\23040. \346\234\200\345\260\217\347\232\204k\344\270\252\346\225\260/README.md" +++ "b/lcof/\351\235\242\350\257\225\351\242\23040. \346\234\200\345\260\217\347\232\204k\344\270\252\346\225\260/README.md" @@ -64,6 +64,48 @@ class Solution { } ``` +### JavaScript +```js +/** + * @param {number[]} arr + * @param {number} k + * @return {number[]} + */ +var getLeastNumbers = function(arr, k) { + // 排序 + // return arr.sort((a,b)=>a-b).slice(0,k) + // ========================================== + // 快排思想 + let left = 0 + let right = arr.length-1 + while(left < right) { + let i = partition(left, right) + if(i <= k) { + left = i+1 + } + if(i >= k){ + right = i-1 + } + } + function partition(left, right) { + let pivot = arr[left] + while(left < right) { + while(left < right && arr[right] >= pivot) { + right-- + } + arr[left] = arr[right] + while(left < right && arr[left] <= pivot) { + left++ + } + arr[right] = arr[left] + } + arr[left] = pivot + return left + } + return arr.slice(0, k) +}; +``` + ### ... ``` diff --git "a/lcof/\351\235\242\350\257\225\351\242\23040. \346\234\200\345\260\217\347\232\204k\344\270\252\346\225\260/Solution.js" "b/lcof/\351\235\242\350\257\225\351\242\23040. \346\234\200\345\260\217\347\232\204k\344\270\252\346\225\260/Solution.js" new file mode 100644 index 0000000000000..a38c36f60fc41 --- /dev/null +++ "b/lcof/\351\235\242\350\257\225\351\242\23040. \346\234\200\345\260\217\347\232\204k\344\270\252\346\225\260/Solution.js" @@ -0,0 +1,38 @@ +/** + * @param {number[]} arr + * @param {number} k + * @return {number[]} + */ +var getLeastNumbers = function(arr, k) { + // 排序 + // return arr.sort((a,b)=>a-b).slice(0,k) + // ========================================== + // 快排思想 + let left = 0 + let right = arr.length-1 + while(left < right) { + let i = partition(left, right) + if(i <= k) { + left = i+1 + } + if(i >= k){ + right = i-1 + } + } + function partition(left, right) { + let pivot = arr[left] + while(left < right) { + while(left < right && arr[right] >= pivot) { + right-- + } + arr[left] = arr[right] + while(left < right && arr[left] <= pivot) { + left++ + } + arr[right] = arr[left] + } + arr[left] = pivot + return left + } + return arr.slice(0, k) +}; \ No newline at end of file diff --git "a/lcof/\351\235\242\350\257\225\351\242\23041. \346\225\260\346\215\256\346\265\201\344\270\255\347\232\204\344\270\255\344\275\215\346\225\260/README.md" "b/lcof/\351\235\242\350\257\225\351\242\23041. \346\225\260\346\215\256\346\265\201\344\270\255\347\232\204\344\270\255\344\275\215\346\225\260/README.md" index 5a1b791e46b07..4256994a6ba2d 100644 --- "a/lcof/\351\235\242\350\257\225\351\242\23041. \346\225\260\346\215\256\346\265\201\344\270\255\347\232\204\344\270\255\344\275\215\346\225\260/README.md" +++ "b/lcof/\351\235\242\350\257\225\351\242\23041. \346\225\260\346\215\256\346\265\201\344\270\255\347\232\204\344\270\255\344\275\215\346\225\260/README.md" @@ -118,6 +118,44 @@ class MedianFinder { */ ``` +### JavaScript +```js +/** + * initialize your data structure here. + */ +var MedianFinder = function() { + this.val = [] +}; + +/** + * @param {number} num + * @return {void} + */ +MedianFinder.prototype.addNum = function(num) { + let left = 0; + let right = this.val.length; + while (left < right) { + let mid = left + ~~((right - left) / 2); + if (num > this.val[mid]) { + left = mid + 1; + } else { + right = mid; + } + } + this.val.splice(left, 0, num); +}; + +/** + * @return {number} + */ +MedianFinder.prototype.findMedian = function() { + let mid = ~~(this.val.length / 2); + return this.val.length % 2 + ? this.val[mid] + : (this.val[mid - 1] + this.val[mid]) / 2; +}; +``` + ### ... ``` diff --git "a/lcof/\351\235\242\350\257\225\351\242\23041. \346\225\260\346\215\256\346\265\201\344\270\255\347\232\204\344\270\255\344\275\215\346\225\260/Solution.js" "b/lcof/\351\235\242\350\257\225\351\242\23041. \346\225\260\346\215\256\346\265\201\344\270\255\347\232\204\344\270\255\344\275\215\346\225\260/Solution.js" new file mode 100644 index 0000000000000..82e4b9d14af4a --- /dev/null +++ "b/lcof/\351\235\242\350\257\225\351\242\23041. \346\225\260\346\215\256\346\265\201\344\270\255\347\232\204\344\270\255\344\275\215\346\225\260/Solution.js" @@ -0,0 +1,34 @@ +/** + * initialize your data structure here. + */ +var MedianFinder = function() { + this.val = [] +}; + +/** + * @param {number} num + * @return {void} + */ +MedianFinder.prototype.addNum = function(num) { + let left = 0; + let right = this.val.length; + while (left < right) { + let mid = left + ~~((right - left) / 2); + if (num > this.val[mid]) { + left = mid + 1; + } else { + right = mid; + } + } + this.val.splice(left, 0, num); +}; + +/** + * @return {number} + */ +MedianFinder.prototype.findMedian = function() { + let mid = ~~(this.val.length / 2); + return this.val.length % 2 + ? this.val[mid] + : (this.val[mid - 1] + this.val[mid]) / 2; +}; \ No newline at end of file diff --git "a/lcof/\351\235\242\350\257\225\351\242\23042. \350\277\236\347\273\255\345\255\220\346\225\260\347\273\204\347\232\204\346\234\200\345\244\247\345\222\214/README.md" "b/lcof/\351\235\242\350\257\225\351\242\23042. \350\277\236\347\273\255\345\255\220\346\225\260\347\273\204\347\232\204\346\234\200\345\244\247\345\222\214/README.md" index fc5ac20bf87ef..6f6c4bb08886b 100644 --- "a/lcof/\351\235\242\350\257\225\351\242\23042. \350\277\236\347\273\255\345\255\220\346\225\260\347\273\204\347\232\204\346\234\200\345\244\247\345\222\214/README.md" +++ "b/lcof/\351\235\242\350\257\225\351\242\23042. \350\277\236\347\273\255\345\255\220\346\225\260\347\273\204\347\232\204\346\234\200\345\244\247\345\222\214/README.md" @@ -45,6 +45,24 @@ class Solution { } ``` +### JavaScript +```js +/** + * @param {number[]} nums + * @return {number} + */ +var maxSubArray = function(nums) { + if(!nums || !nums.length) return null + let len = nums.length + let dp = new Array(len) + dp[0] = nums[0] + for(let i=1;i { + let s1 = a + '' + b + let s2 = b + '' + a + if(s1 < s2) { + return -1 + } else return 1 + }) + return nums.join('') +}; +``` + ### ... ``` diff --git "a/lcof/\351\235\242\350\257\225\351\242\23045. \346\212\212\346\225\260\347\273\204\346\216\222\346\210\220\346\234\200\345\260\217\347\232\204\346\225\260/Solution.js" "b/lcof/\351\235\242\350\257\225\351\242\23045. \346\212\212\346\225\260\347\273\204\346\216\222\346\210\220\346\234\200\345\260\217\347\232\204\346\225\260/Solution.js" new file mode 100644 index 0000000000000..d40170dfb8233 --- /dev/null +++ "b/lcof/\351\235\242\350\257\225\351\242\23045. \346\212\212\346\225\260\347\273\204\346\216\222\346\210\220\346\234\200\345\260\217\347\232\204\346\225\260/Solution.js" @@ -0,0 +1,14 @@ +/** + * @param {number[]} nums + * @return {string} + */ +var minNumber = function(nums) { + nums.sort((a,b) => { + let s1 = a + '' + b + let s2 = b + '' + a + if(s1 < s2) { + return -1 + } else return 1 + }) + return nums.join('') +}; \ No newline at end of file diff --git "a/lcof/\351\235\242\350\257\225\351\242\23046. \346\212\212\346\225\260\345\255\227\347\277\273\350\257\221\346\210\220\345\255\227\347\254\246\344\270\262/README.md" "b/lcof/\351\235\242\350\257\225\351\242\23046. \346\212\212\346\225\260\345\255\227\347\277\273\350\257\221\346\210\220\345\255\227\347\254\246\344\270\262/README.md" index afcb091965dff..e05f426f754a8 100644 --- "a/lcof/\351\235\242\350\257\225\351\242\23046. \346\212\212\346\225\260\345\255\227\347\277\273\350\257\221\346\210\220\345\255\227\347\254\246\344\270\262/README.md" +++ "b/lcof/\351\235\242\350\257\225\351\242\23046. \346\212\212\346\225\260\345\255\227\347\277\273\350\257\221\346\210\220\345\255\227\347\254\246\344\270\262/README.md" @@ -54,6 +54,31 @@ class Solution { } ``` +### JavaScript +```js +/** + * @param {number} num + * @return {number} + */ +var translateNum = function(num) { + let res = 0 + num = num.toString() + function dfs(i) { + if(i >= num.length) { + res++ + return + } + dfs(i+1) + let tmp = +(num[i] + num[i+1]) + if(num[i] !== '0' && tmp >= 0 && tmp < 26 ) { + dfs(i+2) + } + } + dfs(0) + return res +}; +``` + ### ... ``` diff --git "a/lcof/\351\235\242\350\257\225\351\242\23046. \346\212\212\346\225\260\345\255\227\347\277\273\350\257\221\346\210\220\345\255\227\347\254\246\344\270\262/Solution.js" "b/lcof/\351\235\242\350\257\225\351\242\23046. \346\212\212\346\225\260\345\255\227\347\277\273\350\257\221\346\210\220\345\255\227\347\254\246\344\270\262/Solution.js" new file mode 100644 index 0000000000000..189758ba46639 --- /dev/null +++ "b/lcof/\351\235\242\350\257\225\351\242\23046. \346\212\212\346\225\260\345\255\227\347\277\273\350\257\221\346\210\220\345\255\227\347\254\246\344\270\262/Solution.js" @@ -0,0 +1,21 @@ +/** + * @param {number} num + * @return {number} + */ +var translateNum = function(num) { + let res = 0 + num = num.toString() + function dfs(i) { + if(i >= num.length) { + res++ + return + } + dfs(i+1) + let tmp = +(num[i] + num[i+1]) + if(num[i] !== '0' && tmp >= 0 && tmp < 26 ) { + dfs(i+2) + } + } + dfs(0) + return res +}; \ No newline at end of file diff --git "a/lcof/\351\235\242\350\257\225\351\242\23047. \347\244\274\347\211\251\347\232\204\346\234\200\345\244\247\344\273\267\345\200\274/README.md" "b/lcof/\351\235\242\350\257\225\351\242\23047. \347\244\274\347\211\251\347\232\204\346\234\200\345\244\247\344\273\267\345\200\274/README.md" index cf1e170435d86..7fa0879ce0424 100644 --- "a/lcof/\351\235\242\350\257\225\351\242\23047. \347\244\274\347\211\251\347\232\204\346\234\200\345\244\247\344\273\267\345\200\274/README.md" +++ "b/lcof/\351\235\242\350\257\225\351\242\23047. \347\244\274\347\211\251\347\232\204\346\234\200\345\244\247\344\273\267\345\200\274/README.md" @@ -63,6 +63,25 @@ class Solution { } ``` +### JavaScript +```js +/** + * @param {number[][]} grid + * @return {number} + */ +var maxValue = function(grid) { + let row = grid.length + let col = grid[0].length + let dp = [...new Array(row+1)].map(() => Array(col+1).fill(0)) + for(let i=1;i<=row;i++) { + for(let j=1;j<=col;j++) { + dp[i][j] = Math.max(dp[i-1][j], dp[i][j-1]) + grid[i-1][j-1] + } + } + return dp[row][col] +}; +``` + ### ... ``` diff --git "a/lcof/\351\235\242\350\257\225\351\242\23047. \347\244\274\347\211\251\347\232\204\346\234\200\345\244\247\344\273\267\345\200\274/Solution.js" "b/lcof/\351\235\242\350\257\225\351\242\23047. \347\244\274\347\211\251\347\232\204\346\234\200\345\244\247\344\273\267\345\200\274/Solution.js" new file mode 100644 index 0000000000000..be0890f266179 --- /dev/null +++ "b/lcof/\351\235\242\350\257\225\351\242\23047. \347\244\274\347\211\251\347\232\204\346\234\200\345\244\247\344\273\267\345\200\274/Solution.js" @@ -0,0 +1,15 @@ +/** + * @param {number[][]} grid + * @return {number} + */ +var maxValue = function(grid) { + let row = grid.length + let col = grid[0].length + let dp = [...new Array(row+1)].map(() => Array(col+1).fill(0)) + for(let i=1;i<=row;i++) { + for(let j=1;j<=col;j++) { + dp[i][j] = Math.max(dp[i-1][j], dp[i][j-1]) + grid[i-1][j-1] + } + } + return dp[row][col] +}; \ No newline at end of file diff --git "a/lcof/\351\235\242\350\257\225\351\242\23048. \346\234\200\351\225\277\344\270\215\345\220\253\351\207\215\345\244\215\345\255\227\347\254\246\347\232\204\345\255\220\345\255\227\347\254\246\344\270\262/README.md" "b/lcof/\351\235\242\350\257\225\351\242\23048. \346\234\200\351\225\277\344\270\215\345\220\253\351\207\215\345\244\215\345\255\227\347\254\246\347\232\204\345\255\220\345\255\227\347\254\246\344\270\262/README.md" index 21a65122ca048..27300efdf7b7b 100644 --- "a/lcof/\351\235\242\350\257\225\351\242\23048. \346\234\200\351\225\277\344\270\215\345\220\253\351\207\215\345\244\215\345\255\227\347\254\246\347\232\204\345\255\220\345\255\227\347\254\246\344\270\262/README.md" +++ "b/lcof/\351\235\242\350\257\225\351\242\23048. \346\234\200\351\225\277\344\270\215\345\220\253\351\207\215\345\244\215\345\255\227\347\254\246\347\232\204\345\255\220\345\255\227\347\254\246\344\270\262/README.md" @@ -88,6 +88,35 @@ class Solution { } ``` +### JavaScript +```js +/** + * @param {string} s + * @return {number} + */ +var lengthOfLongestSubstring = function(s) { + let left = 0 + let right = 0 + let res = 0 + let len = s.length + let rec = {} + while(right < len) { + let tmp = '*' + while(right < len) { + tmp = s[right] + if(!rec[tmp]) rec[tmp] = 0 + rec[tmp]++ + if(rec[tmp] > 1) break + right++ + } + res = Math.max(res, right - left) + while(rec[tmp] > 1) rec[s[left++]]-- + right++ + } + return res +}; +``` + ### ... ``` diff --git "a/lcof/\351\235\242\350\257\225\351\242\23048. \346\234\200\351\225\277\344\270\215\345\220\253\351\207\215\345\244\215\345\255\227\347\254\246\347\232\204\345\255\220\345\255\227\347\254\246\344\270\262/Solution.js" "b/lcof/\351\235\242\350\257\225\351\242\23048. \346\234\200\351\225\277\344\270\215\345\220\253\351\207\215\345\244\215\345\255\227\347\254\246\347\232\204\345\255\220\345\255\227\347\254\246\344\270\262/Solution.js" new file mode 100644 index 0000000000000..680c338960bd4 --- /dev/null +++ "b/lcof/\351\235\242\350\257\225\351\242\23048. \346\234\200\351\225\277\344\270\215\345\220\253\351\207\215\345\244\215\345\255\227\347\254\246\347\232\204\345\255\220\345\255\227\347\254\246\344\270\262/Solution.js" @@ -0,0 +1,25 @@ +/** + * @param {string} s + * @return {number} + */ +var lengthOfLongestSubstring = function(s) { + let left = 0 + let right = 0 + let res = 0 + let len = s.length + let rec = {} + while(right < len) { + let tmp = '*' + while(right < len) { + tmp = s[right] + if(!rec[tmp]) rec[tmp] = 0 + rec[tmp]++ + if(rec[tmp] > 1) break + right++ + } + res = Math.max(res, right - left) + while(rec[tmp] > 1) rec[s[left++]]-- + right++ + } + return res +}; \ No newline at end of file diff --git "a/lcof/\351\235\242\350\257\225\351\242\23049. \344\270\221\346\225\260/README.md" "b/lcof/\351\235\242\350\257\225\351\242\23049. \344\270\221\346\225\260/README.md" index 7fbfdd86d85ef..c7f65f2c247c0 100644 --- "a/lcof/\351\235\242\350\257\225\351\242\23049. \344\270\221\346\225\260/README.md" +++ "b/lcof/\351\235\242\350\257\225\351\242\23049. \344\270\221\346\225\260/README.md" @@ -76,6 +76,33 @@ class Solution { } ``` +### JavaScript +```js +/** + * @param {number} n + * @return {number} + */ +var nthUglyNumber = function(n) { + let res = [1]; + //三指针 + let a = 0;//2 + let b = 0;//3 + let c = 0;//5 + let min = 0; + for(let i=1;i target) { + right = mid + } else { + left = mid + right = mid + break + } + } + while(nums[left] === target) { + res++ + left-- + } + while(nums[++right] === target) { + res++ + } + return res + +}; +``` + ### ... ``` diff --git "a/lcof/\351\235\242\350\257\225\351\242\23053 - I. \345\234\250\346\216\222\345\272\217\346\225\260\347\273\204\344\270\255\346\237\245\346\211\276\346\225\260\345\255\227 I/Solution.js" "b/lcof/\351\235\242\350\257\225\351\242\23053 - I. \345\234\250\346\216\222\345\272\217\346\225\260\347\273\204\344\270\255\346\237\245\346\211\276\346\225\260\345\255\227 I/Solution.js" new file mode 100644 index 0000000000000..2e5b814db3d75 --- /dev/null +++ "b/lcof/\351\235\242\350\257\225\351\242\23053 - I. \345\234\250\346\216\222\345\272\217\346\225\260\347\273\204\344\270\255\346\237\245\346\211\276\346\225\260\345\255\227 I/Solution.js" @@ -0,0 +1,32 @@ +/** + * @param {number[]} nums + * @param {number} target + * @return {number} + */ +var search = function(nums, target) { + if(!nums || !nums.length) return 0 + let left = 0 + let right = nums.length - 1 + let res = 0 + while(left < right) { + let mid = left + ~~((right-left)/2) + if(nums[mid] < target) { + left = mid + 1 + } else if(nums[mid] > target) { + right = mid + } else { + left = mid + right = mid + break + } + } + while(nums[left] === target) { + res++ + left-- + } + while(nums[++right] === target) { + res++ + } + return res + +}; \ No newline at end of file diff --git "a/lcof/\351\235\242\350\257\225\351\242\23053 - II. 0\357\275\236n-1\344\270\255\347\274\272\345\244\261\347\232\204\346\225\260\345\255\227/README.md" "b/lcof/\351\235\242\350\257\225\351\242\23053 - II. 0\357\275\236n-1\344\270\255\347\274\272\345\244\261\347\232\204\346\225\260\345\255\227/README.md" index b226749528798..85d2589622554 100644 --- "a/lcof/\351\235\242\350\257\225\351\242\23053 - II. 0\357\275\236n-1\344\270\255\347\274\272\345\244\261\347\232\204\346\225\260\345\255\227/README.md" +++ "b/lcof/\351\235\242\350\257\225\351\242\23053 - II. 0\357\275\236n-1\344\270\255\347\274\272\345\244\261\347\232\204\346\225\260\345\255\227/README.md" @@ -66,6 +66,28 @@ class Solution { } ``` +### JavaScript +```js +/** + * @param {number[]} nums + * @return {number} + */ +var missingNumber = function(nums) { + if(!nums || !nums.length) return 0 + let left = 0 + let right = nums.length-1 + while(left < right) { + let mid = left + ~~((right - left)/2) + if(nums[mid] !== mid) { + right = mid + } else { + left = mid + 1 + } + } + return nums[left] === left ? nums.length : left +}; +``` + ### ... ``` diff --git "a/lcof/\351\235\242\350\257\225\351\242\23053 - II. 0\357\275\236n-1\344\270\255\347\274\272\345\244\261\347\232\204\346\225\260\345\255\227/Solution.js" "b/lcof/\351\235\242\350\257\225\351\242\23053 - II. 0\357\275\236n-1\344\270\255\347\274\272\345\244\261\347\232\204\346\225\260\345\255\227/Solution.js" new file mode 100644 index 0000000000000..08d2554a7f3dd --- /dev/null +++ "b/lcof/\351\235\242\350\257\225\351\242\23053 - II. 0\357\275\236n-1\344\270\255\347\274\272\345\244\261\347\232\204\346\225\260\345\255\227/Solution.js" @@ -0,0 +1,18 @@ +/** + * @param {number[]} nums + * @return {number} + */ +var missingNumber = function(nums) { + if(!nums || !nums.length) return 0 + let left = 0 + let right = nums.length-1 + while(left < right) { + let mid = left + ~~((right - left)/2) + if(nums[mid] !== mid) { + right = mid + } else { + left = mid + 1 + } + } + return nums[left] === left ? nums.length : left +}; \ No newline at end of file diff --git "a/lcof/\351\235\242\350\257\225\351\242\23054. \344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\347\232\204\347\254\254k\345\244\247\350\212\202\347\202\271/README.md" "b/lcof/\351\235\242\350\257\225\351\242\23054. \344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\347\232\204\347\254\254k\345\244\247\350\212\202\347\202\271/README.md" index 1028cd673090f..d59bc104ff81f 100644 --- "a/lcof/\351\235\242\350\257\225\351\242\23054. \344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\347\232\204\347\254\254k\345\244\247\350\212\202\347\202\271/README.md" +++ "b/lcof/\351\235\242\350\257\225\351\242\23054. \344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\347\232\204\347\254\254k\345\244\247\350\212\202\347\202\271/README.md" @@ -97,6 +97,34 @@ class Solution { } ``` +### JavaScript +```js +/** + * Definition for a binary tree node. + * function TreeNode(val) { + * this.val = val; + * this.left = this.right = null; + * } + */ +/** + * @param {TreeNode} root + * @param {number} k + * @return {number} + */ +var kthLargest = function(root, k) { + let res + let t = 0 + function traversal(node) { + if(!node) return + traversal(node.right) + if(++t === k) res = node.val + traversal(node.left) + } + traversal(root) + return res +}; +``` + ### ... ``` diff --git "a/lcof/\351\235\242\350\257\225\351\242\23054. \344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\347\232\204\347\254\254k\345\244\247\350\212\202\347\202\271/Solution.js" "b/lcof/\351\235\242\350\257\225\351\242\23054. \344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\347\232\204\347\254\254k\345\244\247\350\212\202\347\202\271/Solution.js" new file mode 100644 index 0000000000000..7a36bfe0f8682 --- /dev/null +++ "b/lcof/\351\235\242\350\257\225\351\242\23054. \344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\347\232\204\347\254\254k\345\244\247\350\212\202\347\202\271/Solution.js" @@ -0,0 +1,24 @@ +/** + * Definition for a binary tree node. + * function TreeNode(val) { + * this.val = val; + * this.left = this.right = null; + * } + */ +/** + * @param {TreeNode} root + * @param {number} k + * @return {number} + */ +var kthLargest = function(root, k) { + let res + let t = 0 + function traversal(node) { + if(!node) return + traversal(node.right) + if(++t === k) res = node.val + traversal(node.left) + } + traversal(root) + return res +}; \ No newline at end of file diff --git "a/lcof/\351\235\242\350\257\225\351\242\23055 - I. \344\272\214\345\217\211\346\240\221\347\232\204\346\267\261\345\272\246/README.md" "b/lcof/\351\235\242\350\257\225\351\242\23055 - I. \344\272\214\345\217\211\346\240\221\347\232\204\346\267\261\345\272\246/README.md" index 3084c2616d633..7fb32ce86750a 100644 --- "a/lcof/\351\235\242\350\257\225\351\242\23055 - I. \344\272\214\345\217\211\346\240\221\347\232\204\346\267\261\345\272\246/README.md" +++ "b/lcof/\351\235\242\350\257\225\351\242\23055 - I. \344\272\214\345\217\211\346\240\221\347\232\204\346\267\261\345\272\246/README.md" @@ -59,6 +59,34 @@ class Solution { } ``` +### JavaScript +```js +/** + * Definition for a binary tree node. + * function TreeNode(val) { + * this.val = val; + * this.left = this.right = null; + * } + */ +/** + * @param {TreeNode} root + * @return {number} + */ +var maxDepth = function(root) { + let res = 0 + function traversal(node, depth) { + if(!node){ + res = Math.max(res, depth) + return + } + traversal(node.left,depth+1) + traversal(node.right,depth+1) + } + traversal(root,0) + return res +}; +``` + ### ... ``` diff --git "a/lcof/\351\235\242\350\257\225\351\242\23055 - I. \344\272\214\345\217\211\346\240\221\347\232\204\346\267\261\345\272\246/Solution.js" "b/lcof/\351\235\242\350\257\225\351\242\23055 - I. \344\272\214\345\217\211\346\240\221\347\232\204\346\267\261\345\272\246/Solution.js" new file mode 100644 index 0000000000000..998c527f08bae --- /dev/null +++ "b/lcof/\351\235\242\350\257\225\351\242\23055 - I. \344\272\214\345\217\211\346\240\221\347\232\204\346\267\261\345\272\246/Solution.js" @@ -0,0 +1,24 @@ +/** + * Definition for a binary tree node. + * function TreeNode(val) { + * this.val = val; + * this.left = this.right = null; + * } + */ +/** + * @param {TreeNode} root + * @return {number} + */ +var maxDepth = function(root) { + let res = 0 + function traversal(node, depth) { + if(!node){ + res = Math.max(res, depth) + return + } + traversal(node.left,depth+1) + traversal(node.right,depth+1) + } + traversal(root,0) + return res +}; \ No newline at end of file diff --git "a/lcof/\351\235\242\350\257\225\351\242\23055 - II. \345\271\263\350\241\241\344\272\214\345\217\211\346\240\221/README.md" "b/lcof/\351\235\242\350\257\225\351\242\23055 - II. \345\271\263\350\241\241\344\272\214\345\217\211\346\240\221/README.md" index 5c6d8f5d7756d..85b951d94bcec 100644 --- "a/lcof/\351\235\242\350\257\225\351\242\23055 - II. \345\271\263\350\241\241\344\272\214\345\217\211\346\240\221/README.md" +++ "b/lcof/\351\235\242\350\257\225\351\242\23055 - II. \345\271\263\350\241\241\344\272\214\345\217\211\346\240\221/README.md" @@ -87,6 +87,32 @@ class Solution { } ``` +### JavaScript +```js +/** + * Definition for a binary tree node. + * function TreeNode(val) { + * this.val = val; + * this.left = this.right = null; + * } + */ +/** + * @param {TreeNode} root + * @return {boolean} + */ +var isBalanced = function(root) { + if(!root) return true + if(!isBalanced(root.left) || !isBalanced(root.right)) return false + if(Math.abs(getDepth(root.left)-getDepth(root.right)) > 1) return false + return true +}; + +function getDepth(node) { + if(!node) return 0 + return Math.max(getDepth(node.left),getDepth(node.right)) + 1 +} +``` + ### ... ``` diff --git "a/lcof/\351\235\242\350\257\225\351\242\23055 - II. \345\271\263\350\241\241\344\272\214\345\217\211\346\240\221/Solution.js" "b/lcof/\351\235\242\350\257\225\351\242\23055 - II. \345\271\263\350\241\241\344\272\214\345\217\211\346\240\221/Solution.js" new file mode 100644 index 0000000000000..abf20759e1956 --- /dev/null +++ "b/lcof/\351\235\242\350\257\225\351\242\23055 - II. \345\271\263\350\241\241\344\272\214\345\217\211\346\240\221/Solution.js" @@ -0,0 +1,22 @@ +/** + * Definition for a binary tree node. + * function TreeNode(val) { + * this.val = val; + * this.left = this.right = null; + * } + */ +/** + * @param {TreeNode} root + * @return {boolean} + */ +var isBalanced = function(root) { + if(!root) return true + if(!isBalanced(root.left) || !isBalanced(root.right)) return false + if(Math.abs(getDepth(root.left)-getDepth(root.right)) > 1) return false + return true +}; + +function getDepth(node) { + if(!node) return 0 + return Math.max(getDepth(node.left),getDepth(node.right)) + 1 +} \ No newline at end of file diff --git "a/lcof/\351\235\242\350\257\225\351\242\23056 - I. \346\225\260\347\273\204\344\270\255\346\225\260\345\255\227\345\207\272\347\216\260\347\232\204\346\254\241\346\225\260/README.md" "b/lcof/\351\235\242\350\257\225\351\242\23056 - I. \346\225\260\347\273\204\344\270\255\346\225\260\345\255\227\345\207\272\347\216\260\347\232\204\346\254\241\346\225\260/README.md" index e7472959072ac..1fa5d61f04203 100644 --- "a/lcof/\351\235\242\350\257\225\351\242\23056 - I. \346\225\260\347\273\204\344\270\255\346\225\260\345\255\227\345\207\272\347\216\260\347\232\204\346\254\241\346\225\260/README.md" +++ "b/lcof/\351\235\242\350\257\225\351\242\23056 - I. \346\225\260\347\273\204\344\270\255\346\225\260\345\255\227\345\207\272\347\216\260\347\232\204\346\254\241\346\225\260/README.md" @@ -76,6 +76,34 @@ class Solution { } ``` +### JavaScript +```js +/** + * @param {number[]} nums + * @return {number[]} + */ +var singleNumbers = function(nums) { + let xor = 0 + let bit = 1 + let res = [0,0] + for(let num of nums) { + xor ^= num + } + while((xor & 1) === 0) { + xor >>= 1 + bit <<= 1 + } + for(let num of nums) { + if((num & bit) === 0) { + res[0] ^= num + } else { + res[1] ^= num + } + } + return res +}; +``` + ### ... ``` diff --git "a/lcof/\351\235\242\350\257\225\351\242\23056 - I. \346\225\260\347\273\204\344\270\255\346\225\260\345\255\227\345\207\272\347\216\260\347\232\204\346\254\241\346\225\260/Solution.js" "b/lcof/\351\235\242\350\257\225\351\242\23056 - I. \346\225\260\347\273\204\344\270\255\346\225\260\345\255\227\345\207\272\347\216\260\347\232\204\346\254\241\346\225\260/Solution.js" new file mode 100644 index 0000000000000..f978b6f6fa9bf --- /dev/null +++ "b/lcof/\351\235\242\350\257\225\351\242\23056 - I. \346\225\260\347\273\204\344\270\255\346\225\260\345\255\227\345\207\272\347\216\260\347\232\204\346\254\241\346\225\260/Solution.js" @@ -0,0 +1,24 @@ +/** + * @param {number[]} nums + * @return {number[]} + */ +var singleNumbers = function(nums) { + let xor = 0 + let bit = 1 + let res = [0,0] + for(let num of nums) { + xor ^= num + } + while((xor & 1) === 0) { + xor >>= 1 + bit <<= 1 + } + for(let num of nums) { + if((num & bit) === 0) { + res[0] ^= num + } else { + res[1] ^= num + } + } + return res +}; \ No newline at end of file diff --git "a/lcof/\351\235\242\350\257\225\351\242\23056 - II. \346\225\260\347\273\204\344\270\255\346\225\260\345\255\227\345\207\272\347\216\260\347\232\204\346\254\241\346\225\260 II/README.md" "b/lcof/\351\235\242\350\257\225\351\242\23056 - II. \346\225\260\347\273\204\344\270\255\346\225\260\345\255\227\345\207\272\347\216\260\347\232\204\346\254\241\346\225\260 II/README.md" index 9052f7aa0a6be..f557b4be6b407 100644 --- "a/lcof/\351\235\242\350\257\225\351\242\23056 - II. \346\225\260\347\273\204\344\270\255\346\225\260\345\255\227\345\207\272\347\216\260\347\232\204\346\254\241\346\225\260 II/README.md" +++ "b/lcof/\351\235\242\350\257\225\351\242\23056 - II. \346\225\260\347\273\204\344\270\255\346\225\260\345\255\227\345\207\272\347\216\260\347\232\204\346\254\241\346\225\260 II/README.md" @@ -62,6 +62,23 @@ class Solution { } ``` +### JavaScript +```js +/** + * @param {number[]} nums + * @return {number} + */ +var singleNumber = function(nums) { + let a = 0 + let b = 0 + for(let num of nums) { + a = (a ^ num) & ~b + b = (b ^ num) & ~a + } + return a +}; +``` + ### ... ``` diff --git "a/lcof/\351\235\242\350\257\225\351\242\23056 - II. \346\225\260\347\273\204\344\270\255\346\225\260\345\255\227\345\207\272\347\216\260\347\232\204\346\254\241\346\225\260 II/Solution.js" "b/lcof/\351\235\242\350\257\225\351\242\23056 - II. \346\225\260\347\273\204\344\270\255\346\225\260\345\255\227\345\207\272\347\216\260\347\232\204\346\254\241\346\225\260 II/Solution.js" new file mode 100644 index 0000000000000..54ae19d5437e5 --- /dev/null +++ "b/lcof/\351\235\242\350\257\225\351\242\23056 - II. \346\225\260\347\273\204\344\270\255\346\225\260\345\255\227\345\207\272\347\216\260\347\232\204\346\254\241\346\225\260 II/Solution.js" @@ -0,0 +1,13 @@ +/** + * @param {number[]} nums + * @return {number} + */ +var singleNumber = function(nums) { + let a = 0 + let b = 0 + for(let num of nums) { + a = (a ^ num) & ~b + b = (b ^ num) & ~a + } + return a +}; \ No newline at end of file diff --git "a/lcof/\351\235\242\350\257\225\351\242\23057 - II. \345\222\214\344\270\272s\347\232\204\350\277\236\347\273\255\346\255\243\346\225\260\345\272\217\345\210\227/README.md" "b/lcof/\351\235\242\350\257\225\351\242\23057 - II. \345\222\214\344\270\272s\347\232\204\350\277\236\347\273\255\346\255\243\346\225\260\345\272\217\345\210\227/README.md" index fc6aeaf3fd873..d30afc86399b5 100644 --- "a/lcof/\351\235\242\350\257\225\351\242\23057 - II. \345\222\214\344\270\272s\347\232\204\350\277\236\347\273\255\346\255\243\346\225\260\345\272\217\345\210\227/README.md" +++ "b/lcof/\351\235\242\350\257\225\351\242\23057 - II. \345\222\214\344\270\272s\347\232\204\350\277\236\347\273\255\346\255\243\346\225\260\345\272\217\345\210\227/README.md" @@ -74,6 +74,38 @@ class Solution { } ``` +### JavaScript +```js +/** + * @param {number} target + * @return {number[][]} + */ +var findContinuousSequence = function(target) { + let res = [] + let window = [] + let i = 1 + let sum = 0 + while(1) { + if(sum < target) { + window.push(i) + sum += i + i++ + } else if(sum > target) { + let a = window.shift() + if(window.length < 2) break + sum -= a + } else { + res.push([...window]) + window.push(i) + sum += i + i++ + if(window.length === 2) break + } + } + return res +}; +``` + ### ... ``` diff --git "a/lcof/\351\235\242\350\257\225\351\242\23057 - II. \345\222\214\344\270\272s\347\232\204\350\277\236\347\273\255\346\255\243\346\225\260\345\272\217\345\210\227/Solution.js" "b/lcof/\351\235\242\350\257\225\351\242\23057 - II. \345\222\214\344\270\272s\347\232\204\350\277\236\347\273\255\346\255\243\346\225\260\345\272\217\345\210\227/Solution.js" new file mode 100644 index 0000000000000..10047a40453e4 --- /dev/null +++ "b/lcof/\351\235\242\350\257\225\351\242\23057 - II. \345\222\214\344\270\272s\347\232\204\350\277\236\347\273\255\346\255\243\346\225\260\345\272\217\345\210\227/Solution.js" @@ -0,0 +1,28 @@ +/** + * @param {number} target + * @return {number[][]} + */ +var findContinuousSequence = function(target) { + let res = [] + let window = [] + let i = 1 + let sum = 0 + while(1) { + if(sum < target) { + window.push(i) + sum += i + i++ + } else if(sum > target) { + let a = window.shift() + if(window.length < 2) break + sum -= a + } else { + res.push([...window]) + window.push(i) + sum += i + i++ + if(window.length === 2) break + } + } + return res +}; \ No newline at end of file diff --git "a/lcof/\351\235\242\350\257\225\351\242\23057. \345\222\214\344\270\272s\347\232\204\344\270\244\344\270\252\346\225\260\345\255\227/README.md" "b/lcof/\351\235\242\350\257\225\351\242\23057. \345\222\214\344\270\272s\347\232\204\344\270\244\344\270\252\346\225\260\345\255\227/README.md" index 7aa8054141a3c..a0d9ed9d117f6 100644 --- "a/lcof/\351\235\242\350\257\225\351\242\23057. \345\222\214\344\270\272s\347\232\204\344\270\244\344\270\252\346\225\260\345\255\227/README.md" +++ "b/lcof/\351\235\242\350\257\225\351\242\23057. \345\222\214\344\270\272s\347\232\204\344\270\244\344\270\252\346\225\260\345\255\227/README.md" @@ -60,6 +60,30 @@ class Solution { } ``` +### JavaScript +```js +/** + * @param {number[]} nums + * @param {number} target + * @return {number[]} + */ +var twoSum = function(nums, target) { + let left = 0 + let right = nums.length - 1 + while(left < right) { + let sum = nums[left] + nums[right] + if(sum === target) { + return [nums[left],nums[right]] + } else if(sum > target) { + right-- + } else { + left++ + } + } + +}; +``` + ### ... ``` diff --git "a/lcof/\351\235\242\350\257\225\351\242\23057. \345\222\214\344\270\272s\347\232\204\344\270\244\344\270\252\346\225\260\345\255\227/Solution.js" "b/lcof/\351\235\242\350\257\225\351\242\23057. \345\222\214\344\270\272s\347\232\204\344\270\244\344\270\252\346\225\260\345\255\227/Solution.js" new file mode 100644 index 0000000000000..5c1f4f6bff38e --- /dev/null +++ "b/lcof/\351\235\242\350\257\225\351\242\23057. \345\222\214\344\270\272s\347\232\204\344\270\244\344\270\252\346\225\260\345\255\227/Solution.js" @@ -0,0 +1,20 @@ +/** + * @param {number[]} nums + * @param {number} target + * @return {number[]} + */ +var twoSum = function(nums, target) { + let left = 0 + let right = nums.length - 1 + while(left < right) { + let sum = nums[left] + nums[right] + if(sum === target) { + return [nums[left],nums[right]] + } else if(sum > target) { + right-- + } else { + left++ + } + } + +}; \ No newline at end of file diff --git "a/lcof/\351\235\242\350\257\225\351\242\23058 - I. \347\277\273\350\275\254\345\215\225\350\257\215\351\241\272\345\272\217/README.md" "b/lcof/\351\235\242\350\257\225\351\242\23058 - I. \347\277\273\350\275\254\345\215\225\350\257\215\351\241\272\345\272\217/README.md" index 6ecded32d4cc2..fe199971d18f4 100644 --- "a/lcof/\351\235\242\350\257\225\351\242\23058 - I. \347\277\273\350\275\254\345\215\225\350\257\215\351\241\272\345\272\217/README.md" +++ "b/lcof/\351\235\242\350\257\225\351\242\23058 - I. \347\277\273\350\275\254\345\215\225\350\257\215\351\241\272\345\272\217/README.md" @@ -70,6 +70,17 @@ class Solution { } ``` +### JavaScript +```js +/** + * @param {string} s + * @return {string} + */ +var reverseWords = function(s) { + return s.split(' ').reduce((acc,cur)=>cur !== '' ? acc.concat(cur) : acc,[]).reverse().join(' ') +}; +``` + ### ... ``` diff --git "a/lcof/\351\235\242\350\257\225\351\242\23058 - I. \347\277\273\350\275\254\345\215\225\350\257\215\351\241\272\345\272\217/Solution.js" "b/lcof/\351\235\242\350\257\225\351\242\23058 - I. \347\277\273\350\275\254\345\215\225\350\257\215\351\241\272\345\272\217/Solution.js" new file mode 100644 index 0000000000000..97c9f7b79c8cf --- /dev/null +++ "b/lcof/\351\235\242\350\257\225\351\242\23058 - I. \347\277\273\350\275\254\345\215\225\350\257\215\351\241\272\345\272\217/Solution.js" @@ -0,0 +1,7 @@ +/** + * @param {string} s + * @return {string} + */ +var reverseWords = function(s) { + return s.split(' ').reduce((acc,cur)=>cur !== '' ? acc.concat(cur) : acc,[]).reverse().join(' ') +}; \ No newline at end of file diff --git "a/lcof/\351\235\242\350\257\225\351\242\23058 - II. \345\267\246\346\227\213\350\275\254\345\255\227\347\254\246\344\270\262/README.md" "b/lcof/\351\235\242\350\257\225\351\242\23058 - II. \345\267\246\346\227\213\350\275\254\345\255\227\347\254\246\344\270\262/README.md" index ff130d1b4babf..5309f42e389fe 100644 --- "a/lcof/\351\235\242\350\257\225\351\242\23058 - II. \345\267\246\346\227\213\350\275\254\345\255\227\347\254\246\344\270\262/README.md" +++ "b/lcof/\351\235\242\350\257\225\351\242\23058 - II. \345\267\246\346\227\213\350\275\254\345\255\227\347\254\246\344\270\262/README.md" @@ -41,6 +41,18 @@ class Solution { } ``` +### JavaScript +```js +/** + * @param {string} s + * @param {number} n + * @return {string} + */ +var reverseLeftWords = function(s, n) { + return s.substring(n) + s.substr(0,n) +}; +``` + ### ... ``` diff --git "a/lcof/\351\235\242\350\257\225\351\242\23058 - II. \345\267\246\346\227\213\350\275\254\345\255\227\347\254\246\344\270\262/Solution.js" "b/lcof/\351\235\242\350\257\225\351\242\23058 - II. \345\267\246\346\227\213\350\275\254\345\255\227\347\254\246\344\270\262/Solution.js" new file mode 100644 index 0000000000000..f281cdf5dd656 --- /dev/null +++ "b/lcof/\351\235\242\350\257\225\351\242\23058 - II. \345\267\246\346\227\213\350\275\254\345\255\227\347\254\246\344\270\262/Solution.js" @@ -0,0 +1,8 @@ +/** + * @param {string} s + * @param {number} n + * @return {string} + */ +var reverseLeftWords = function(s, n) { + return s.substring(n) + s.substr(0,n) +}; \ No newline at end of file diff --git "a/lcof/\351\235\242\350\257\225\351\242\23059 - I. \346\273\221\345\212\250\347\252\227\345\217\243\347\232\204\346\234\200\345\244\247\345\200\274/README.md" "b/lcof/\351\235\242\350\257\225\351\242\23059 - I. \346\273\221\345\212\250\347\252\227\345\217\243\347\232\204\346\234\200\345\244\247\345\200\274/README.md" index 0aeac76a00a24..3517b6f499810 100644 --- "a/lcof/\351\235\242\350\257\225\351\242\23059 - I. \346\273\221\345\212\250\347\252\227\345\217\243\347\232\204\346\234\200\345\244\247\345\200\274/README.md" +++ "b/lcof/\351\235\242\350\257\225\351\242\23059 - I. \346\273\221\345\212\250\347\252\227\345\217\243\347\232\204\346\234\200\345\244\247\345\200\274/README.md" @@ -80,6 +80,39 @@ class Solution { } ``` +### JavaScript +```js +/** + * @param {number[]} nums + * @param {number} k + * @return {number[]} + */ +var maxSlidingWindow = function(nums, k) { + if(!nums.length || !k) return [] + if(k === 1) return nums + let res = [] + let tmpMax = -Infinity + let len = nums.length + let window = [] + for(let i=0;i tmpMax) { + tmpMax = nums[i] + } else if(tmpMax === a) { + tmpMax = Math.max(...window) + } + res.push(tmpMax) + } + return res +}; +``` + ### ... ``` diff --git "a/lcof/\351\235\242\350\257\225\351\242\23059 - I. \346\273\221\345\212\250\347\252\227\345\217\243\347\232\204\346\234\200\345\244\247\345\200\274/Solution.js" "b/lcof/\351\235\242\350\257\225\351\242\23059 - I. \346\273\221\345\212\250\347\252\227\345\217\243\347\232\204\346\234\200\345\244\247\345\200\274/Solution.js" new file mode 100644 index 0000000000000..ee423b6aab269 --- /dev/null +++ "b/lcof/\351\235\242\350\257\225\351\242\23059 - I. \346\273\221\345\212\250\347\252\227\345\217\243\347\232\204\346\234\200\345\244\247\345\200\274/Solution.js" @@ -0,0 +1,29 @@ +/** + * @param {number[]} nums + * @param {number} k + * @return {number[]} + */ +var maxSlidingWindow = function(nums, k) { + if(!nums.length || !k) return [] + if(k === 1) return nums + let res = [] + let tmpMax = -Infinity + let len = nums.length + let window = [] + for(let i=0;i tmpMax) { + tmpMax = nums[i] + } else if(tmpMax === a) { + tmpMax = Math.max(...window) + } + res.push(tmpMax) + } + return res +}; \ No newline at end of file diff --git "a/lcof/\351\235\242\350\257\225\351\242\23059 - II. \351\230\237\345\210\227\347\232\204\346\234\200\345\244\247\345\200\274/README.md" "b/lcof/\351\235\242\350\257\225\351\242\23059 - II. \351\230\237\345\210\227\347\232\204\346\234\200\345\244\247\345\200\274/README.md" new file mode 100644 index 0000000000000..ac81fadb4b303 --- /dev/null +++ "b/lcof/\351\235\242\350\257\225\351\242\23059 - II. \351\230\237\345\210\227\347\232\204\346\234\200\345\244\247\345\200\274/README.md" @@ -0,0 +1,102 @@ +# [面试题59 - II. 队列的最大值](https://leetcode-cn.com/problems/dui-lie-de-zui-da-zhi-lcof/) + +## 题目描述 + +请定义一个队列并实现函数 `max_value` 得到队列里的最大值,要求函数`max_value`、`push_back` 和 `pop_front` 的**均摊**时间复杂度都是O(1)。 + +若队列为空,`pop_front` 和 `max_value` 需要返回 -1 + +**示例 1:** + +``` +输入: +["MaxQueue","push_back","push_back","max_value","pop_front","max_value"] +[[],[1],[2],[],[],[]] +输出: [null,null,null,2,1,2] +``` + +**示例 2:** + +``` +输入: +["MaxQueue","pop_front","max_value"] +[[],[],[]] +输出: [null,-1,-1] +``` + +**限制:** +- `1 <= push_back,pop_front,max_value的总操作数 <= 10000` +- `1 <= value <= 10^5` + +## 解法 + + + +### Python3 + +```python + +``` + +### Java + +```java + +``` + +### JavaScript +```js +var MaxQueue = function() { + this.queue = [] + this.maxValue = -Infinity + this.maxIdx = -1 +}; + +/** + * @return {number} + */ +MaxQueue.prototype.max_value = function() { + if(!this.queue.length) return -1 + return this.maxValue +}; + +/** + * @param {number} value + * @return {void} + */ +MaxQueue.prototype.push_back = function(value) { + this.queue.push(value) + if(value >= this.maxValue) { + this.maxIdx = this.queue.length-1 + this.maxValue = value + } +}; + +/** + * @return {number} + */ +MaxQueue.prototype.pop_front = function() { + if(!this.queue.length) return -1 + let a = this.queue.shift() + this.maxIdx-- + if(this.maxIdx < 0) { + let tmp = -Infinity + let id = -1 + for(let i=0;i tmp) { + tmp = this.queue[i] + id = i + } + } + this.maxIdx = id + this.maxValue = tmp + } + return a +}; +``` + + +### ... +``` + +``` \ No newline at end of file diff --git "a/lcof/\351\235\242\350\257\225\351\242\23059 - II. \351\230\237\345\210\227\347\232\204\346\234\200\345\244\247\345\200\274/Solution.js" "b/lcof/\351\235\242\350\257\225\351\242\23059 - II. \351\230\237\345\210\227\347\232\204\346\234\200\345\244\247\345\200\274/Solution.js" new file mode 100644 index 0000000000000..953dd6196945c --- /dev/null +++ "b/lcof/\351\235\242\350\257\225\351\242\23059 - II. \351\230\237\345\210\227\347\232\204\346\234\200\345\244\247\345\200\274/Solution.js" @@ -0,0 +1,47 @@ +var MaxQueue = function() { + this.queue = [] + this.maxValue = -Infinity + this.maxIdx = -1 +}; + +/** + * @return {number} + */ +MaxQueue.prototype.max_value = function() { + if(!this.queue.length) return -1 + return this.maxValue +}; + +/** + * @param {number} value + * @return {void} + */ +MaxQueue.prototype.push_back = function(value) { + this.queue.push(value) + if(value >= this.maxValue) { + this.maxIdx = this.queue.length-1 + this.maxValue = value + } +}; + +/** + * @return {number} + */ +MaxQueue.prototype.pop_front = function() { + if(!this.queue.length) return -1 + let a = this.queue.shift() + this.maxIdx-- + if(this.maxIdx < 0) { + let tmp = -Infinity + let id = -1 + for(let i=0;i tmp) { + tmp = this.queue[i] + id = i + } + } + this.maxIdx = id + this.maxValue = tmp + } + return a +}; \ No newline at end of file diff --git "a/lcof/\351\235\242\350\257\225\351\242\23060. n\344\270\252\351\252\260\345\255\220\347\232\204\347\202\271\346\225\260/README.md" "b/lcof/\351\235\242\350\257\225\351\242\23060. n\344\270\252\351\252\260\345\255\220\347\232\204\347\202\271\346\225\260/README.md" index 0516485183d8d..74f8896c1bb0f 100644 --- "a/lcof/\351\235\242\350\257\225\351\242\23060. n\344\270\252\351\252\260\345\255\220\347\232\204\347\202\271\346\225\260/README.md" +++ "b/lcof/\351\235\242\350\257\225\351\242\23060. n\344\270\252\351\252\260\345\255\220\347\232\204\347\202\271\346\225\260/README.md" @@ -96,6 +96,30 @@ class Solution { } ``` +### JavaScript +```js +/** + * @param {number} n + * @return {number[]} + */ +var twoSum = function(n) { + function backtrack(sum,time) { + if(time === n) { + res[sum]++ + return + } + for(let i=1;i<=6;i++) { + backtrack(sum+i,time+1) + } + } + let len = n*6 + let t = 6**n + let res = new Array(len+1).fill(0) + backtrack(0,0) + return res.slice(n).map(e=> e/t) +}; +``` + ### ... ``` diff --git "a/lcof/\351\235\242\350\257\225\351\242\23060. n\344\270\252\351\252\260\345\255\220\347\232\204\347\202\271\346\225\260/Solution.js" "b/lcof/\351\235\242\350\257\225\351\242\23060. n\344\270\252\351\252\260\345\255\220\347\232\204\347\202\271\346\225\260/Solution.js" new file mode 100644 index 0000000000000..7c923bcd542b2 --- /dev/null +++ "b/lcof/\351\235\242\350\257\225\351\242\23060. n\344\270\252\351\252\260\345\255\220\347\232\204\347\202\271\346\225\260/Solution.js" @@ -0,0 +1,20 @@ +/** + * @param {number} n + * @return {number[]} + */ +var twoSum = function(n) { + function backtrack(sum,time) { + if(time === n) { + res[sum]++ + return + } + for(let i=1;i<=6;i++) { + backtrack(sum+i,time+1) + } + } + let len = n*6 + let t = 6**n + let res = new Array(len+1).fill(0) + backtrack(0,0) + return res.slice(n).map(e=> e/t) +}; \ No newline at end of file diff --git "a/lcof/\351\235\242\350\257\225\351\242\23061. \346\211\221\345\205\213\347\211\214\344\270\255\347\232\204\351\241\272\345\255\220/README.md" "b/lcof/\351\235\242\350\257\225\351\242\23061. \346\211\221\345\205\213\347\211\214\344\270\255\347\232\204\351\241\272\345\255\220/README.md" index e7764fbc4a6d1..055afb74ebdf6 100644 --- "a/lcof/\351\235\242\350\257\225\351\242\23061. \346\211\221\345\205\213\347\211\214\344\270\255\347\232\204\351\241\272\345\255\220/README.md" +++ "b/lcof/\351\235\242\350\257\225\351\242\23061. \346\211\221\345\205\213\347\211\214\344\270\255\347\232\204\351\241\272\345\255\220/README.md" @@ -75,6 +75,33 @@ class Solution { } ``` +### JavaScript +```js +/** + * @param {number[]} nums + * @return {boolean} + */ +var isStraight = function(nums) { + let zeroCnt = 0 + nums.sort((a,b)=>a-b) + for(let i=0;i= nums[i+1] - zeroCnt - 1) { + zeroCnt-- + } else { + return false + } + } + if(zeroCnt < 0) return false + } + return true +}; +``` + ### ... ``` diff --git "a/lcof/\351\235\242\350\257\225\351\242\23061. \346\211\221\345\205\213\347\211\214\344\270\255\347\232\204\351\241\272\345\255\220/Solution.js" "b/lcof/\351\235\242\350\257\225\351\242\23061. \346\211\221\345\205\213\347\211\214\344\270\255\347\232\204\351\241\272\345\255\220/Solution.js" new file mode 100644 index 0000000000000..537fcd3807ecc --- /dev/null +++ "b/lcof/\351\235\242\350\257\225\351\242\23061. \346\211\221\345\205\213\347\211\214\344\270\255\347\232\204\351\241\272\345\255\220/Solution.js" @@ -0,0 +1,23 @@ +/** + * @param {number[]} nums + * @return {boolean} + */ +var isStraight = function(nums) { + let zeroCnt = 0 + nums.sort((a,b)=>a-b) + for(let i=0;i= nums[i+1] - zeroCnt - 1) { + zeroCnt-- + } else { + return false + } + } + if(zeroCnt < 0) return false + } + return true +}; \ No newline at end of file diff --git "a/lcof/\351\235\242\350\257\225\351\242\23062. \345\234\206\345\234\210\344\270\255\346\234\200\345\220\216\345\211\251\344\270\213\347\232\204\346\225\260\345\255\227/README.md" "b/lcof/\351\235\242\350\257\225\351\242\23062. \345\234\206\345\234\210\344\270\255\346\234\200\345\220\216\345\211\251\344\270\213\347\232\204\346\225\260\345\255\227/README.md" index 5d4870c1d715d..a723b117a9608 100644 --- "a/lcof/\351\235\242\350\257\225\351\242\23062. \345\234\206\345\234\210\344\270\255\346\234\200\345\220\216\345\211\251\344\270\213\347\232\204\346\225\260\345\255\227/README.md" +++ "b/lcof/\351\235\242\350\257\225\351\242\23062. \345\234\206\345\234\210\344\270\255\346\234\200\345\220\216\345\211\251\344\270\213\347\232\204\346\225\260\345\255\227/README.md" @@ -80,6 +80,23 @@ class Solution { } ``` +### JavaScript +```js +/** + * @param {number} n + * @param {number} m + * @return {number} + */ +var lastRemaining = function(n, m) { + // 约瑟夫环 + let res = 0 + for(let i=1;i<=n;i++) { + res = (res + m) % i + } + return res +}; +``` + ### ... ``` diff --git "a/lcof/\351\235\242\350\257\225\351\242\23062. \345\234\206\345\234\210\344\270\255\346\234\200\345\220\216\345\211\251\344\270\213\347\232\204\346\225\260\345\255\227/Solution.js" "b/lcof/\351\235\242\350\257\225\351\242\23062. \345\234\206\345\234\210\344\270\255\346\234\200\345\220\216\345\211\251\344\270\213\347\232\204\346\225\260\345\255\227/Solution.js" new file mode 100644 index 0000000000000..293807d8adcbb --- /dev/null +++ "b/lcof/\351\235\242\350\257\225\351\242\23062. \345\234\206\345\234\210\344\270\255\346\234\200\345\220\216\345\211\251\344\270\213\347\232\204\346\225\260\345\255\227/Solution.js" @@ -0,0 +1,13 @@ +/** + * @param {number} n + * @param {number} m + * @return {number} + */ +var lastRemaining = function(n, m) { + // 约瑟夫环 + let res = 0 + for(let i=1;i<=n;i++) { + res = (res + m) % i + } + return res +}; \ No newline at end of file diff --git "a/lcof/\351\235\242\350\257\225\351\242\23063. \350\202\241\347\245\250\347\232\204\346\234\200\345\244\247\345\210\251\346\266\246/README.md" "b/lcof/\351\235\242\350\257\225\351\242\23063. \350\202\241\347\245\250\347\232\204\346\234\200\345\244\247\345\210\251\346\266\246/README.md" index 8eef4dd3bd472..b726c64beba21 100644 --- "a/lcof/\351\235\242\350\257\225\351\242\23063. \350\202\241\347\245\250\347\232\204\346\234\200\345\244\247\345\210\251\346\266\246/README.md" +++ "b/lcof/\351\235\242\350\257\225\351\242\23063. \350\202\241\347\245\250\347\232\204\346\234\200\345\244\247\345\210\251\346\266\246/README.md" @@ -58,6 +58,23 @@ class Solution { } ``` +### JavaScript +```js +/** + * @param {number[]} prices + * @return {number} + */ +var maxProfit = function(prices) { + let a = 0 + let b = Infinity + for(let p of prices) { + a = Math.max(a, p - b) + b = Math.min(b, p) + } + return a +}; +``` + ### ... ``` diff --git "a/lcof/\351\235\242\350\257\225\351\242\23063. \350\202\241\347\245\250\347\232\204\346\234\200\345\244\247\345\210\251\346\266\246/Solution.js" "b/lcof/\351\235\242\350\257\225\351\242\23063. \350\202\241\347\245\250\347\232\204\346\234\200\345\244\247\345\210\251\346\266\246/Solution.js" new file mode 100644 index 0000000000000..ccbb22d26615e --- /dev/null +++ "b/lcof/\351\235\242\350\257\225\351\242\23063. \350\202\241\347\245\250\347\232\204\346\234\200\345\244\247\345\210\251\346\266\246/Solution.js" @@ -0,0 +1,13 @@ +/** + * @param {number[]} prices + * @return {number} + */ +var maxProfit = function(prices) { + let a = 0 + let b = Infinity + for(let p of prices) { + a = Math.max(a, p - b) + b = Math.min(b, p) + } + return a +}; \ No newline at end of file diff --git "a/lcof/\351\235\242\350\257\225\351\242\23064. \346\261\2021+2+\342\200\246+n/README.md" "b/lcof/\351\235\242\350\257\225\351\242\23064. \346\261\2021+2+\342\200\246+n/README.md" index 87d59e2511cdc..5afb98553d65b 100644 --- "a/lcof/\351\235\242\350\257\225\351\242\23064. \346\261\2021+2+\342\200\246+n/README.md" +++ "b/lcof/\351\235\242\350\257\225\351\242\23064. \346\261\2021+2+\342\200\246+n/README.md" @@ -42,6 +42,17 @@ class Solution { } ``` +### JavaScript +```js +/** + * @param {number} n + * @return {number} + */ +var sumNums = function(n) { + return (n**2 + n) >> 1 +}; +``` + ### ... ``` diff --git "a/lcof/\351\235\242\350\257\225\351\242\23064. \346\261\2021+2+\342\200\246+n/Solution.js" "b/lcof/\351\235\242\350\257\225\351\242\23064. \346\261\2021+2+\342\200\246+n/Solution.js" new file mode 100644 index 0000000000000..e8809a3fed041 --- /dev/null +++ "b/lcof/\351\235\242\350\257\225\351\242\23064. \346\261\2021+2+\342\200\246+n/Solution.js" @@ -0,0 +1,7 @@ +/** + * @param {number} n + * @return {number} + */ +var sumNums = function(n) { + return (n**2 + n) >> 1 +}; \ No newline at end of file diff --git "a/lcof/\351\235\242\350\257\225\351\242\23065. \344\270\215\347\224\250\345\212\240\345\207\217\344\271\230\351\231\244\345\201\232\345\212\240\346\263\225/README.md" "b/lcof/\351\235\242\350\257\225\351\242\23065. \344\270\215\347\224\250\345\212\240\345\207\217\344\271\230\351\231\244\345\201\232\345\212\240\346\263\225/README.md" index 02c0acb99f37e..1b1d70442809e 100644 --- "a/lcof/\351\235\242\350\257\225\351\242\23065. \344\270\215\347\224\250\345\212\240\345\207\217\344\271\230\351\231\244\345\201\232\345\212\240\346\263\225/README.md" +++ "b/lcof/\351\235\242\350\257\225\351\242\23065. \344\270\215\347\224\250\345\212\240\345\207\217\344\271\230\351\231\244\345\201\232\345\212\240\346\263\225/README.md" @@ -56,6 +56,22 @@ class Solution { } ``` +### JavaScript +```js +/** + * @param {number} a + * @param {number} b + * @return {number} + */ +var add = function(a, b) { + if(a === 0) return b + return add((a&b) << 1, a^b) +}; +// (a & b) << 1 是 进位和 +// a ^ b 是不进位和 +// 两者相加得结果,由于本题禁止 + 号,所以递归 +``` + ### ... ``` diff --git "a/lcof/\351\235\242\350\257\225\351\242\23065. \344\270\215\347\224\250\345\212\240\345\207\217\344\271\230\351\231\244\345\201\232\345\212\240\346\263\225/Solution.js" "b/lcof/\351\235\242\350\257\225\351\242\23065. \344\270\215\347\224\250\345\212\240\345\207\217\344\271\230\351\231\244\345\201\232\345\212\240\346\263\225/Solution.js" new file mode 100644 index 0000000000000..020dd80397a86 --- /dev/null +++ "b/lcof/\351\235\242\350\257\225\351\242\23065. \344\270\215\347\224\250\345\212\240\345\207\217\344\271\230\351\231\244\345\201\232\345\212\240\346\263\225/Solution.js" @@ -0,0 +1,12 @@ +/** + * @param {number} a + * @param {number} b + * @return {number} + */ +var add = function(a, b) { + if(a === 0) return b + return add((a&b) << 1, a^b) +}; +// (a & b) << 1 是 进位和 +// a ^ b 是不进位和 +// 两者相加得结果,由于本题禁止 + 号,所以递归 \ No newline at end of file diff --git "a/lcof/\351\235\242\350\257\225\351\242\23066. \346\236\204\345\273\272\344\271\230\347\247\257\346\225\260\347\273\204/README.md" "b/lcof/\351\235\242\350\257\225\351\242\23066. \346\236\204\345\273\272\344\271\230\347\247\257\346\225\260\347\273\204/README.md" index 1bc1c15a28e02..4313fe5c4375f 100644 --- "a/lcof/\351\235\242\350\257\225\351\242\23066. \346\236\204\345\273\272\344\271\230\347\247\257\346\225\260\347\273\204/README.md" +++ "b/lcof/\351\235\242\350\257\225\351\242\23066. \346\236\204\345\273\272\344\271\230\347\247\257\346\225\260\347\273\204/README.md" @@ -70,6 +70,28 @@ class Solution { } ``` +### JavaScript +```js +/** + * @param {number[]} a + * @return {number[]} + */ +var constructArr = function(a) { + let pre = new Array(a.length+1).fill(1) + pre[0] = 1 + let res = new Array(a.length).fill(1) + for(let i = 1;i <= a.length;i++) { + pre[i] = a[i-1] * pre[i-1] + } + let cur = 1 + for(let i = a.length - 1;i >= 0;i--) { + res[i] = pre[i] * cur + cur *= a[i] + } + return res +}; +``` + ### ... ``` diff --git "a/lcof/\351\235\242\350\257\225\351\242\23066. \346\236\204\345\273\272\344\271\230\347\247\257\346\225\260\347\273\204/Solution.js" "b/lcof/\351\235\242\350\257\225\351\242\23066. \346\236\204\345\273\272\344\271\230\347\247\257\346\225\260\347\273\204/Solution.js" new file mode 100644 index 0000000000000..764d9d407c4f6 --- /dev/null +++ "b/lcof/\351\235\242\350\257\225\351\242\23066. \346\236\204\345\273\272\344\271\230\347\247\257\346\225\260\347\273\204/Solution.js" @@ -0,0 +1,18 @@ +/** + * @param {number[]} a + * @return {number[]} + */ +var constructArr = function(a) { + let pre = new Array(a.length+1).fill(1) + pre[0] = 1 + let res = new Array(a.length).fill(1) + for(let i = 1;i <= a.length;i++) { + pre[i] = a[i-1] * pre[i-1] + } + let cur = 1 + for(let i = a.length - 1;i >= 0;i--) { + res[i] = pre[i] * cur + cur *= a[i] + } + return res +}; \ No newline at end of file diff --git "a/lcof/\351\235\242\350\257\225\351\242\23067. \346\212\212\345\255\227\347\254\246\344\270\262\350\275\254\346\215\242\346\210\220\346\225\264\346\225\260/README.md" "b/lcof/\351\235\242\350\257\225\351\242\23067. \346\212\212\345\255\227\347\254\246\344\270\262\350\275\254\346\215\242\346\210\220\346\225\264\346\225\260/README.md" index 8727352074914..435a11408a41c 100644 --- "a/lcof/\351\235\242\350\257\225\351\242\23067. \346\212\212\345\255\227\347\254\246\344\270\262\350\275\254\346\215\242\346\210\220\346\225\264\346\225\260/README.md" +++ "b/lcof/\351\235\242\350\257\225\351\242\23067. \346\212\212\345\255\227\347\254\246\344\270\262\350\275\254\346\215\242\346\210\220\346\225\264\346\225\260/README.md" @@ -119,6 +119,35 @@ class Solution { } ``` +### JavaScript +```js +/** + * @param {string} str + * @return {number} + */ +var strToInt = function(str) { + let res = '' + let l = 1 + for(let i=0;i 2147483647) return 2147483647 + if(res < -2147483648) return -2147483648 + return res +}; +``` + ### ... ``` diff --git "a/lcof/\351\235\242\350\257\225\351\242\23067. \346\212\212\345\255\227\347\254\246\344\270\262\350\275\254\346\215\242\346\210\220\346\225\264\346\225\260/Solution.js" "b/lcof/\351\235\242\350\257\225\351\242\23067. \346\212\212\345\255\227\347\254\246\344\270\262\350\275\254\346\215\242\346\210\220\346\225\264\346\225\260/Solution.js" new file mode 100644 index 0000000000000..dea43b5ec53d7 --- /dev/null +++ "b/lcof/\351\235\242\350\257\225\351\242\23067. \346\212\212\345\255\227\347\254\246\344\270\262\350\275\254\346\215\242\346\210\220\346\225\264\346\225\260/Solution.js" @@ -0,0 +1,25 @@ +/** + * @param {string} str + * @return {number} + */ +var strToInt = function(str) { + let res = '' + let l = 1 + for(let i=0;i 2147483647) return 2147483647 + if(res < -2147483648) return -2147483648 + return res +}; \ No newline at end of file diff --git "a/lcof/\351\235\242\350\257\225\351\242\23068 - I. \344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\347\232\204\346\234\200\350\277\221\345\205\254\345\205\261\347\245\226\345\205\210/README.md" "b/lcof/\351\235\242\350\257\225\351\242\23068 - I. \344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\347\232\204\346\234\200\350\277\221\345\205\254\345\205\261\347\245\226\345\205\210/README.md" index 142e8be87a99e..ad7d171d53b55 100644 --- "a/lcof/\351\235\242\350\257\225\351\242\23068 - I. \344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\347\232\204\346\234\200\350\277\221\345\205\254\345\205\261\347\245\226\345\205\210/README.md" +++ "b/lcof/\351\235\242\350\257\225\351\242\23068 - I. \344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\347\232\204\346\234\200\350\277\221\345\205\254\345\205\261\347\245\226\345\205\210/README.md" @@ -91,6 +91,33 @@ class Solution { } ``` +### JavaScript +```js +/** + * Definition for a binary tree node. + * function TreeNode(val) { + * this.val = val; + * this.left = this.right = null; + * } + */ +/** + * @param {TreeNode} root + * @param {TreeNode} p + * @param {TreeNode} q + * @return {TreeNode} + */ +var lowestCommonAncestor = function(root, p, q) { + // 递归 + if(!root) return null + if(root.val < p.val && root.val < q.val) { + return lowestCommonAncestor(root.right, p,q) + } else if(root.val > p.val && root.val > q.val) { + return lowestCommonAncestor(root.left, p,q) + } + return root +}; +``` + ### ... ``` diff --git "a/lcof/\351\235\242\350\257\225\351\242\23068 - I. \344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\347\232\204\346\234\200\350\277\221\345\205\254\345\205\261\347\245\226\345\205\210/Solution.js" "b/lcof/\351\235\242\350\257\225\351\242\23068 - I. \344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\347\232\204\346\234\200\350\277\221\345\205\254\345\205\261\347\245\226\345\205\210/Solution.js" new file mode 100644 index 0000000000000..aa30bd7cb9469 --- /dev/null +++ "b/lcof/\351\235\242\350\257\225\351\242\23068 - I. \344\272\214\345\217\211\346\220\234\347\264\242\346\240\221\347\232\204\346\234\200\350\277\221\345\205\254\345\205\261\347\245\226\345\205\210/Solution.js" @@ -0,0 +1,23 @@ +/** + * Definition for a binary tree node. + * function TreeNode(val) { + * this.val = val; + * this.left = this.right = null; + * } + */ +/** + * @param {TreeNode} root + * @param {TreeNode} p + * @param {TreeNode} q + * @return {TreeNode} + */ +var lowestCommonAncestor = function(root, p, q) { + // 递归 + if(!root) return null + if(root.val < p.val && root.val < q.val) { + return lowestCommonAncestor(root.right, p,q) + } else if(root.val > p.val && root.val > q.val) { + return lowestCommonAncestor(root.left, p,q) + } + return root +}; \ No newline at end of file diff --git "a/lcof/\351\235\242\350\257\225\351\242\23068 - II. \344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\350\277\221\345\205\254\345\205\261\347\245\226\345\205\210/README.md" "b/lcof/\351\235\242\350\257\225\351\242\23068 - II. \344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\350\277\221\345\205\254\345\205\261\347\245\226\345\205\210/README.md" index 5a484a0de2c47..e80dfa073577f 100644 --- "a/lcof/\351\235\242\350\257\225\351\242\23068 - II. \344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\350\277\221\345\205\254\345\205\261\347\245\226\345\205\210/README.md" +++ "b/lcof/\351\235\242\350\257\225\351\242\23068 - II. \344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\350\277\221\345\205\254\345\205\261\347\245\226\345\205\210/README.md" @@ -80,6 +80,33 @@ class Solution { } ``` +### JavaScript +```js +/** + * Definition for a binary tree node. + * function TreeNode(val) { + * this.val = val; + * this.left = this.right = null; + * } + */ +/** + * @param {TreeNode} root + * @param {TreeNode} p + * @param {TreeNode} q + * @return {TreeNode} + */ +var lowestCommonAncestor = function(root, p, q) { + if(!root) return null + if(root === p || root === q) return root + let left = lowestCommonAncestor(root.left,p,q) + let right = lowestCommonAncestor(root.right,p,q) + if(left && right) return root + if(left) return left + if(right) return right + return null +}; +``` + ### ... ``` diff --git "a/lcof/\351\235\242\350\257\225\351\242\23068 - II. \344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\350\277\221\345\205\254\345\205\261\347\245\226\345\205\210/Solution.js" "b/lcof/\351\235\242\350\257\225\351\242\23068 - II. \344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\350\277\221\345\205\254\345\205\261\347\245\226\345\205\210/Solution.js" new file mode 100644 index 0000000000000..03e3aa2aa956b --- /dev/null +++ "b/lcof/\351\235\242\350\257\225\351\242\23068 - II. \344\272\214\345\217\211\346\240\221\347\232\204\346\234\200\350\277\221\345\205\254\345\205\261\347\245\226\345\205\210/Solution.js" @@ -0,0 +1,23 @@ +/** + * Definition for a binary tree node. + * function TreeNode(val) { + * this.val = val; + * this.left = this.right = null; + * } + */ +/** + * @param {TreeNode} root + * @param {TreeNode} p + * @param {TreeNode} q + * @return {TreeNode} + */ +var lowestCommonAncestor = function(root, p, q) { + if(!root) return null + if(root === p || root === q) return root + let left = lowestCommonAncestor(root.left,p,q) + let right = lowestCommonAncestor(root.right,p,q) + if(left && right) return root + if(left) return left + if(right) return right + return null +}; \ No newline at end of file