diff --git a/.prettierrc b/.prettierrc index 6d334cc..483497e 100644 --- a/.prettierrc +++ b/.prettierrc @@ -1,5 +1,5 @@ { "printWidth": 80, "tabWidth": 2, - "semi": false + "semi": true } \ No newline at end of file diff --git "a/BFS\351\227\256\351\242\230/\350\267\263\350\267\203\346\270\270\346\210\217 III-1306.js" "b/BFS\351\227\256\351\242\230/\350\267\263\350\267\203\346\270\270\346\210\217 III-1306.js" index 73d910e..019e717 100644 --- "a/BFS\351\227\256\351\242\230/\350\267\263\350\267\203\346\270\270\346\210\217 III-1306.js" +++ "b/BFS\351\227\256\351\242\230/\350\267\263\350\267\203\346\270\270\346\210\217 III-1306.js" @@ -3,25 +3,51 @@ * @param {number} start * @return {boolean} */ -let canReach = function (arr, start) { +var canReach = function (arr, start) { let n = arr.length let visited = [] let queue = [start] while (queue.length) { - let index = queue.pop() - let val = arr[index] - if (val === 0) { - return true - } - let left = index - val - let right = index + val - if (left >= 0 && !visited[left]) { - queue.push(left) - } - if (right < n && !visited[right]) { - queue.push(right) - } - visited[index] = true + let index = queue.pop() + let val = arr[index] + if (val === 0) { + return true + } + let left = index - val + let right = index + val + if (left >= 0 && !visited[left]) { + queue.push(left) + } + if (right < n && !visited[right]) { + queue.push(right) + } + visited[index] = true } return false -}; \ No newline at end of file +} + +var canReach = function (arr, start) { + let n = arr.length + let visited = [] + let queue = [start] + while (queue.length) { + let index = queue.pop() + let val = arr[index] + if (val === 0) { + return true + } + let left = index - val + let right = index + val + if (left >= 0 && !visited[left]) { + queue.push(left) + } + if (right < n && !visited[right]) { + queue.push(right) + } + visited[index] = true + } + return false +} + +console.log(canReach([4, 2, 3, 0, 3, 1, 2], 5)) +console.log(canReach([1, 1, 1], 1)) diff --git "a/BFS\351\227\256\351\242\230/\350\267\263\350\267\203\346\270\270\346\210\217 IV-1345.js" "b/BFS\351\227\256\351\242\230/\350\267\263\350\267\203\346\270\270\346\210\217 IV-1345.js" index be115b2..5c29548 100644 --- "a/BFS\351\227\256\351\242\230/\350\267\263\350\267\203\346\270\270\346\210\217 IV-1345.js" +++ "b/BFS\351\227\256\351\242\230/\350\267\263\350\267\203\346\270\270\346\210\217 IV-1345.js" @@ -42,12 +42,16 @@ let minJumps = function (arr) { let count = 0 let queue = [0] while (queue.length) { + console.log("queue", queue, count) + count++ let len = queue.length for (let i = 0; i < len; i++) { + console.log("queue111", queue) let index = queue.shift() // 找到了 由于bfs的特性 此时用的跳跃次数一定是最少的 if (index === n - 1) { + console.log(index, n) return count - 1 } @@ -70,3 +74,5 @@ let minJumps = function (arr) { } return n } + +console.log(minJumps([1, 2, 3, 4, 5, 9, 2, 9, 8, 9])) diff --git "a/DFS\351\227\256\351\242\230/\345\262\233\345\261\277\347\232\204\346\234\200\345\244\247\351\235\242\347\247\257-659.js" "b/DFS\351\227\256\351\242\230/\345\262\233\345\261\277\347\232\204\346\234\200\345\244\247\351\235\242\347\247\257-659.js" deleted file mode 100644 index 2a46b59..0000000 --- "a/DFS\351\227\256\351\242\230/\345\262\233\345\261\277\347\232\204\346\234\200\345\244\247\351\235\242\347\247\257-659.js" +++ /dev/null @@ -1,41 +0,0 @@ -// https://github.com/sl1673495/daily-plan/issues/18 - -/** - * @param {number[][]} grid - * @return {number} - */ -let maxAreaOfIsland = function (grid) { - let yLen = grid.length; - if (!yLen) return grid; - let xLen = grid[0].length; - let max = 0; - - for (let y = 0; y < yLen; y++) { - for (let x = 0; x < xLen; x++) { - if (grid[y][x] === 1) { - let countRef = { current: 0 }; - dfs(grid, y, x, countRef); - if (countRef.current > max) { - max = countRef.current; - } - } - } - } - return max; -}; - -function dfs(grid, y, x, countRef) { - if (!grid[y] || !grid[y][x] || grid[y][x] === 0 || grid[y][x] === "COMPLETE") { - return; - } - - if (grid[y][x] === 1) { - grid[y][x] = "COMPLETE"; - countRef.current++; - } - - dfs(grid, y - 1, x, countRef); - dfs(grid, y + 1, x, countRef); - dfs(grid, y, x - 1, countRef); - dfs(grid, y, x + 1, countRef); -} \ No newline at end of file diff --git "a/DFS\351\227\256\351\242\230/\345\262\233\345\261\277\347\232\204\346\234\200\345\244\247\351\235\242\347\247\257-695.js" "b/DFS\351\227\256\351\242\230/\345\262\233\345\261\277\347\232\204\346\234\200\345\244\247\351\235\242\347\247\257-695.js" new file mode 100644 index 0000000..f2e796e --- /dev/null +++ "b/DFS\351\227\256\351\242\230/\345\262\233\345\261\277\347\232\204\346\234\200\345\244\247\351\235\242\347\247\257-695.js" @@ -0,0 +1,146 @@ +// https://github.com/sl1673495/daily-plan/issues/18 + +/** + * @param {number[][]} grid + * @return {number} + */ +var maxAreaOfIsland = function (grid) { + let yLen = grid.length + if (!yLen) return grid + let xLen = grid[0].length + let max = 0 + + for (let y = 0; y < yLen; y++) { + for (let x = 0; x < xLen; x++) { + if (grid[y][x] === 1) { + let countRef = { current: 0 } + dfs(grid, y, x, countRef) + if (countRef.current > max) { + max = countRef.current + } + } + } + } + return max +} + +function dfs(grid, y, x, countRef) { + if ( + !grid[y] || + !grid[y][x] || + grid[y][x] === 0 || + grid[y][x] === "COMPLETE" + ) { + return + } + + if (grid[y][x] === 1) { + grid[y][x] = "COMPLETE" + countRef.current++ + } + + dfs(grid, y - 1, x, countRef) + dfs(grid, y + 1, x, countRef) + dfs(grid, y, x - 1, countRef) + dfs(grid, y, x + 1, countRef) +} + +// 错的 flag 冲突了 + +/** + * @param {number[][]} grid + * @return {number} + */ +var maxAreaOfIsland = function (grid) { + let yLen = grid.length + if (!yLen) return 0 + let xLen = grid[0].length + let perimeter = {} + + for (let y = 0; y < yLen; y++) { + for (let x = 0; x < xLen; x++) { + if (grid[y][x] === 1) { + // perimeter[`${y}@${x}`] = 1 + dfs(grid, y, x, perimeter, `${y}@${x}`) + } + } + } + console.log(perimeter) + var arr = Object.values(perimeter) + const _len = arr.length + return !_len ? 0 : Math.max(...arr) +} + +function dfs(grid, y, x, perimeter, key) { + // let cell = grid[y][x] + + // if (cell === "COMPLETE") return + + if ( + !grid[y] || + !grid[y][x] || + grid[y][x] === 0 || + grid[y][x] === "COMPLETE" + ) { + return + } + + grid[y][x] = "COMPLETE" + perimeter[key] ? perimeter[key]++ : (perimeter[key] = 1) + let below = grid[y - 1] && grid[y - 1][x] + let upper = grid[y + 1] && grid[y + 1][x] + let left = grid[y][x - 1] + let right = grid[y][x + 1] + // console.log(below, upper, left, right) + dfs(grid, y - 1, x, perimeter, key) + dfs(grid, y + 1, x, perimeter, key) + dfs(grid, y, x - 1, perimeter, key) + dfs(grid, y, x + 1, perimeter, key) + return + + if (below === 1) { + perimeter[key] ? perimeter[key]++ : (perimeter[key] = 1) + dfs(grid, y - 1, x, perimeter, key) + } else { + } + + if (upper === 1) { + perimeter[key] ? perimeter[key]++ : (perimeter[key] = 1) + dfs(grid, y + 1, x, perimeter, key) + } else { + } + + if (left === 1) { + perimeter[key] ? perimeter[key]++ : (perimeter[key] = 1) + dfs(grid, y, x - 1, perimeter, key) + } else { + } + + if (right === 1) { + perimeter[key] ? perimeter[key]++ : (perimeter[key] = 1) + dfs(grid, y, x + 1, perimeter, key) + } else { + } +} +var a = maxAreaOfIsland([ + [0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0], + [0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0], + [0, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0], + [0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 1, 0, 0], + [0, 1, 0, 0, 1, 1, 0, 0, 1, 1, 1, 0, 0], + [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0], + [0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0], + [0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0], +]) +// var a = maxAreaOfIsland([[0, 0, 0, 0, 0, 0, 0, 0]]) +// var a = maxAreaOfIsland([ +// [1, 1, 0, 0, 0], +// [1, 1, 0, 0, 0], +// [0, 0, 0, 1, 1], +// [0, 0, 0, 1, 1], +// ]) + +var b = a +debugger + +console.log(a) diff --git "a/LRU\347\274\223\345\255\230.js" "b/LRU\347\274\223\345\255\230.js" index 25d09e7..d052035 100644 --- "a/LRU\347\274\223\345\255\230.js" +++ "b/LRU\347\274\223\345\255\230.js" @@ -1,94 +1,18 @@ -class DoubleNode { - constructor(key, val) { - this.key = key - this.val = val - - this.prev = null - this.next = null +class dbStudentData { + constructor() { + this.scoreList = [] } -} - -class LRUCache { - constructor(max) { - this.max = max - this.map = new Map() - - this.head = null - this.tail = null + add(score) { + this.scoreList.push(score) } - - get(key) { - const node = this.map.get(key) - if (!node) { - return -1 - } else { - const res = node.val - this.remove(node) - this.appendHead(node) - return res - } + del(score) { + this.scoreList.pop(score) } - - put(key, value) { - let node = this.map.get(key) - // 有这个缓存 - if (node) { - node.val = value - // 新加入的 放在最前面 - this.remove(node) - this.appendHead(node) - } else { - // 没有这个缓存 - node = new DoubleNode(key, value) - // 如果超出容量了 删除最后一个 再放到头部 - if (this.map.size >= this.max) { - this.map.delete(this.tail.key) - this.remove(this.tail) - this.appendHead(node) - this.map.set(key, node) - } else { - // 未超出容量 就直接放到头部 - this.appendHead(node) - this.map.set(key, node) - } - } - } - - /** - * 把头部指针的改变成新的node - * @param {DoubleNode} node - */ - appendHead(node) { - if (this.head === null) { - this.head = this.tail = node - } else { - node.next = this.head - this.head.prev = node - this.head = node - } - } - - /** - * 删除某个节点 - * @param {DoubleNode} node - */ - remove(node) { - if (this.head === this.tail) { - this.head = this.tail = null - } else { - // 删除头部 - if (this.head === node) { - this.head = this.head.next - node.next = null - } else if (this.tail === node) { - this.tail = this.tail.prev - this.tail.next = null - node.prev = null - } else { - node.prev.next = node.next - node.next.prev = node.prev - node.prev = node.next = null - } - } + getMin() { + return Math.min(...this.scoreList) } } + +var a = new dbStudentData() +debugger +var b = a \ No newline at end of file diff --git a/README.md b/README.md index 78e30a6..abb815b 100644 --- a/README.md +++ b/README.md @@ -17,14 +17,13 @@ ## 目录 - ### 例题详解 [最接近的三数之和-16](https://github.com/sl1673495/leetcode-javascript/issues/115) [分发饼干-455](https://github.com/sl1673495/leetcode-javascript/issues/88) -[N皇后-51](https://github.com/sl1673495/leetcode-javascript/issues/78) +[N 皇后-51](https://github.com/sl1673495/leetcode-javascript/issues/78) [单词搜索-79](https://github.com/sl1673495/leetcode-javascript/issues/77) @@ -68,7 +67,7 @@ [解数独-37](https://github.com/sl1673495/leetcode-javascript/issues/79) -[N皇后-51](https://github.com/sl1673495/leetcode-javascript/issues/78) +[N 皇后-51](https://github.com/sl1673495/leetcode-javascript/issues/78) [单词搜索-79](https://github.com/sl1673495/leetcode-javascript/issues/77) @@ -92,7 +91,7 @@ [分割回文串-131](https://github.com/sl1673495/leetcode-javascript/issues/67) -[复原IP地址-93](https://github.com/sl1673495/leetcode-javascript/issues/66) +[复原 IP 地址-93](https://github.com/sl1673495/leetcode-javascript/issues/66) [电话号码的字母组合-17](https://github.com/sl1673495/leetcode-javascript/issues/65) @@ -166,7 +165,7 @@ ### 前缀和 -[和为K的子数组-560](https://github.com/sl1673495/leetcode-javascript/issues/110) +[和为 K 的子数组-560](https://github.com/sl1673495/leetcode-javascript/issues/110) ### 位运算 @@ -208,15 +207,15 @@ [移除链表元素-203](https://github.com/sl1673495/leetcode-javascript/issues/97) -[两数相加-3](https://github.com/sl1673495/leetcode-javascript/issues/94) +[两数相加-2](https://github.com/sl1673495/leetcode-javascript/issues/94) [两两交换链表中的节点-24](https://github.com/sl1673495/leetcode-javascript/issues/51) -[删除链表的倒数第N个节点-19](https://github.com/sl1673495/leetcode-javascript/issues/46) +[删除链表的倒数第 N 个节点-19](https://github.com/sl1673495/leetcode-javascript/issues/46) -[删除链表的节点-面试题18](https://github.com/sl1673495/leetcode-javascript/issues/40) +[删除链表的节点-面试题 18](https://github.com/sl1673495/leetcode-javascript/issues/40) -[反转链表II-92](https://github.com/sl1673495/leetcode-javascript/issues/39) +[反转链表 II-92](https://github.com/sl1673495/leetcode-javascript/issues/39) [反转链表 206](https://github.com/sl1673495/leetcode-javascript/issues/38) @@ -231,6 +230,7 @@ ### DFS [二叉树的最近公共祖先-236](https://github.com/sl1673495/leetcode-javascript/issues/64) +[岛屿的最大面积-695](https://github.com/sl1673495/daily-plan/issues/18) [将有序数组转换为二叉搜索树](https://github.com/sl1673495/leetcode-javascript/issues/63) diff --git "a/\344\272\214\345\210\206\346\237\245\346\211\276/\344\272\214\345\210\206\346\237\245\346\211\276.js" "b/\344\272\214\345\210\206\346\237\245\346\211\276/\344\272\214\345\210\206\346\237\245\346\211\276.js" index 2c29806..85223bd 100644 --- "a/\344\272\214\345\210\206\346\237\245\346\211\276/\344\272\214\345\210\206\346\237\245\346\211\276.js" +++ "b/\344\272\214\345\210\206\346\237\245\346\211\276/\344\272\214\345\210\206\346\237\245\346\211\276.js" @@ -1,5 +1,5 @@ /** - * 二分搜索 从一个数组中搜索一个确定的数 + * 二分搜索 从一个有序数组中搜索一个确定的数 */ function binarySearch(arr, n, target) { diff --git "a/\345\212\250\346\200\201\350\247\204\345\210\222/\344\270\200\345\222\214\351\233\266-474.js" "b/\345\212\250\346\200\201\350\247\204\345\210\222/\344\270\200\345\222\214\351\233\266-474.js" index 69f703a..42e014c 100644 --- "a/\345\212\250\346\200\201\350\247\204\345\210\222/\344\270\200\345\222\214\351\233\266-474.js" +++ "b/\345\212\250\346\200\201\350\247\204\345\210\222/\344\270\200\345\222\214\351\233\266-474.js" @@ -1,4 +1,5 @@ /** + * TODO:看是看懂了,自己写又想不起思路 * @param {string[]} strs * @param {number} m * @param {number} n @@ -9,7 +10,7 @@ let findMaxForm = function (strs, m, n) { if (!sl) { return 0 } - + // dp[i][j][s] 表示输入字符串在子区间 [0, s] 能够使用 i 个 0 和 j 个 1 的字符串的最大数量。 let dp = [] for (let i = 0; i <= m; i++) { diff --git "a/\345\212\250\346\200\201\350\247\204\345\210\222/\344\270\213\351\231\215\350\267\257\345\276\204\346\234\200\345\260\217\345\222\214-931.js" "b/\345\212\250\346\200\201\350\247\204\345\210\222/\344\270\213\351\231\215\350\267\257\345\276\204\346\234\200\345\260\217\345\222\214-931.js" index 30ef633..2218643 100644 --- "a/\345\212\250\346\200\201\350\247\204\345\210\222/\344\270\213\351\231\215\350\267\257\345\276\204\346\234\200\345\260\217\345\222\214-931.js" +++ "b/\345\212\250\346\200\201\350\247\204\345\210\222/\344\270\213\351\231\215\350\267\257\345\276\204\346\234\200\345\260\217\345\222\214-931.js" @@ -2,33 +2,61 @@ * @param {number[][]} A * @return {number} */ -let minFallingPathSum = function (A) { - let n = A.length +var minFallingPathSum = function (A) { + let n = A.length; - let dp = [] + let dp = []; for (let i = 0; i < n; i++) { - dp[i] = [] + dp[i] = []; } for (let j = 0; j < n; j++) { - dp[n - 1][j] = A[n - 1][j] + dp[n - 1][j] = A[n - 1][j]; } for (let i = n - 2; i >= 0; i--) { for (let j = 0; j < n; j++) { - dp[i][j] = Infinity - let left = j - 1 - let right = j + 1 - let mid = j - let nextRowIndexes = [left, mid, right] + dp[i][j] = Infinity; + let left = j - 1; + let right = j + 1; + let mid = j; + let nextRowIndexes = [left, mid, right]; for (let nextRowIndex of nextRowIndexes) { if (nextRowIndex >= 0 && nextRowIndex < n) { - dp[i][j] = Math.min(dp[i][j], A[i][j] + dp[i + 1][nextRowIndex]) + dp[i][j] = Math.min(dp[i][j], A[i][j] + dp[i + 1][nextRowIndex]); } } } } // 第一行的最小值 可以确定整体的最小路径 - return Math.min(...dp[0]) -} + return Math.min(...dp[0]); +}; +var minFallingPathSum = function (matrix) { + let n = matrix.length; + let safeNum = Infinity; + + let dp = []; + for (let i = 0; i < n; i++) { + dp[i] = []; + } + + dp[n - 1] = matrix[n - 1]; + for (var i = n - 2; i >= 0; i--) { + for (var j = 0; j <= n - 1; j++) { + const left = j - 1 >= 0 ? dp[i + 1][j - 1] : safeNum; + const right = j + 1 <= n - 1 ? dp[i + 1][j + 1] : safeNum; + dp[i][j] = matrix[i][j] + Math.min(left, dp[i + 1][j], right); + } + } + return Math.min(...dp[0]); +}; + +console.log( + minFallingPathSum([ + [100, -42, -46, -41], + [31, 97, 10, -10], + [-58, -51, 82, 89], + [51, 81, 69, -51], + ]) +); diff --git "a/\345\212\250\346\200\201\350\247\204\345\210\222/\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272.js" "b/\345\212\250\346\200\201\350\247\204\345\210\222/\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272-121.js" similarity index 100% rename from "\345\212\250\346\200\201\350\247\204\345\210\222/\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272.js" rename to "\345\212\250\346\200\201\350\247\204\345\210\222/\344\271\260\345\215\226\350\202\241\347\245\250\347\232\204\346\234\200\344\275\263\346\227\266\346\234\272-121.js" diff --git "a/\345\212\250\346\200\201\350\247\204\345\210\222/\344\275\277\347\224\250\346\234\200\345\260\217\350\212\261\350\264\271\347\210\254\346\245\274\346\242\257-746.js" "b/\345\212\250\346\200\201\350\247\204\345\210\222/\344\275\277\347\224\250\346\234\200\345\260\217\350\212\261\350\264\271\347\210\254\346\245\274\346\242\257-746.js" index 59f9997..b145745 100644 --- "a/\345\212\250\346\200\201\350\247\204\345\210\222/\344\275\277\347\224\250\346\234\200\345\260\217\350\212\261\350\264\271\347\210\254\346\245\274\346\242\257-746.js" +++ "b/\345\212\250\346\200\201\350\247\204\345\210\222/\344\275\277\347\224\250\346\234\200\345\260\217\350\212\261\350\264\271\347\210\254\346\245\274\346\242\257-746.js" @@ -2,15 +2,37 @@ * @param {number[]} cost * @return {number} */ -let minCostClimbingStairs = function (cost) { - let dp = []; +var minCostClimbingStairs = function (cost) { + let dp = [] for (let i = cost.length - 1; i >= 0; i--) { - let oneStep = cost[i] + (dp[i + 1] || 0); - let twoStep = cost[i] + (dp[i + 2] || 0); + let oneStep = cost[i] + (dp[i + 1] || 0) + let twoStep = cost[i] + (dp[i + 2] || 0) - dp[i] = Math.min(oneStep, twoStep); + dp[i] = Math.min(oneStep, twoStep) } - return Math.min(dp[0], dp[1]); -}; \ No newline at end of file + return Math.min(dp[0], dp[1]) +} + +var minCostClimbingStairs = function (cost) { + let dp = [] + const len = cost.length + // dp[0] = 0 + dp[1] = Math.min(cost[0], cost[1]) + // if (len === 2) { + // return dp[1] + // } + dp[2] = Math.min(cost[0] + cost[2], cost[1]) + // for (let i = len - 1; i >= 0; i--) { + for (let i = 4; i <= len; i++) { + dp[i - 1] = Math.min(dp[i - 3] + cost[i - 1], dp[i - 2]) + } + + console.log(dp) + return dp[cost.length - 1] +} +// console.log(minCostClimbingStairs([10, 15, 20])) +// console.log(minCostClimbingStairs([1, 5, 1])) +// console.log(minCostClimbingStairs([1, 5, 1, 1])) +console.log(minCostClimbingStairs([1, 100, 1, 1, 1, 100, 1, 1, 100, 1])) diff --git "a/\345\212\250\346\200\201\350\247\204\345\210\222/\345\210\206\345\211\262\347\255\211\345\222\214\345\255\220\351\233\206-416.js" "b/\345\212\250\346\200\201\350\247\204\345\210\222/\345\210\206\345\211\262\347\255\211\345\222\214\345\255\220\351\233\206-416.js" index b7f5fe0..c53e586 100644 --- "a/\345\212\250\346\200\201\350\247\204\345\210\222/\345\210\206\345\211\262\347\255\211\345\222\214\345\255\220\351\233\206-416.js" +++ "b/\345\212\250\346\200\201\350\247\204\345\210\222/\345\210\206\345\211\262\347\255\211\345\222\214\345\255\220\351\233\206-416.js" @@ -2,21 +2,19 @@ * @param {number[]} nums * @return {boolean} */ -let canPartition = function (nums) { +var canPartition = function (nums) { let n = nums.length - - let sum = nums.reduce((a, b) => a + b); - - let target = sum / 2; + let sum = nums.reduce((a, b) => a + b) + let target = sum / 2 // 数据不是整数 直接return if (Math.ceil(target) !== target) { - return false; + return false } - let dp = new Array(n); + let dp = new Array(n) for (let i = 0; i < dp.length; i++) { - dp[i] = new Array(target + 1).fill(false); + dp[i] = new Array(target + 1).fill(false) } // 列代表可以选择去凑数的数值 @@ -24,28 +22,61 @@ let canPartition = function (nums) { // 行代表是否可以凑到这个数字j for (let j = 0; j <= target; j++) { // 不用当前数,直接选择前一行的结果 - let pickPrev = (dp[i - 1] ? dp[i - 1][j]: false) || false + let pickPrev = (dp[i - 1] ? dp[i - 1][j] : false) || false // 拿出当前数,并且从前一行里找其他的值能否凑成剩下的值 - let pickCurrentAndPrev = (dp[i - 1] ? dp[i - 1][j - nums[i]]: false) || false + let pickCurrentAndPrev = + (dp[i - 1] ? dp[i - 1][j - nums[i]] : false) || false // 只拿的值直接去凑目标值 let pickCurrent = j === nums[i] // 任意一者满足 即可理解成 「i下标的值」配合「i下标之前的数值」 可以一起凑成目标值 - let can = ( - pickPrev || - pickCurrent|| - pickCurrentAndPrev - ) + let can = pickPrev || pickCurrent || pickCurrentAndPrev dp[i][j] = can // 只要任意一行的 target 列满足条件 即可认为有「子数组」可以凑成目标值 直接返回 true - if ((j === target) && can) { + if (j === target && can) { return true } } } return dp[n - 1][target] -}; \ No newline at end of file +} + +var canPartition = function (nums) { + let n = nums.length + let sum = nums.reduce((a, b) => a + b) + let target = sum / 2 + + // 数据不是整数 直接return + if (Math.ceil(target) !== target) { + return false + } + let dp = new Array(n) + for (let i = 0; i < dp.length; i++) { + dp[i] = new Array(target + 1).fill(false) + } + + dp[0][nums[0]] = true + for (let i = 0; i < dp.length; i++) { + console.log(dp[i]) + dp[i][0] = true + if (i >= 1) { + for (let j = 0; j < target + 1; j++) { + if (j < nums[i]) { + dp[i][j] = dp[i - 1][j] + } else { + dp[i][j] = dp[i - 1][j] || dp[i - 1][j - nums[i]] + } + } + } + } + return !!dp.find((item) => { + return item[target] === true + }) + console.log(dp) +} + +console.log(canPartition([1, 5, 11, 5])) diff --git "a/\345\212\250\346\200\201\350\247\204\345\210\222/\345\215\225\350\257\215\346\213\206\345\210\206-139.js" "b/\345\212\250\346\200\201\350\247\204\345\210\222/\345\215\225\350\257\215\346\213\206\345\210\206-139.js" index dbb4672..256b890 100644 --- "a/\345\212\250\346\200\201\350\247\204\345\210\222/\345\215\225\350\257\215\346\213\206\345\210\206-139.js" +++ "b/\345\212\250\346\200\201\350\247\204\345\210\222/\345\215\225\350\257\215\346\213\206\345\210\206-139.js" @@ -3,10 +3,10 @@ * @param {string[]} wordDict * @return {boolean} */ -let wordBreak = function (s, wordDict) { +var wordBreak = function (s, wordDict) { let n = s.length if (!n) return true - + // 去重 let wordSet = new Set(wordDict) let dp = [] dp[0] = true @@ -23,3 +23,55 @@ let wordBreak = function (s, wordDict) { return !!dp[n] } + +/** + * 自己写的性能太差了。。。力扣不通过。。。orz + * @param {string} s + * @param {string[]} wordDict + * @return {boolean} + */ +// var wordBreak = function (s, wordDict) { +// wordDict = new Set(wordDict) +// wordDict = Array.from(wordDict) + +// let flag = false +// let str = s +// let dfs = (string) => { +// for (var i = 0; i < wordDict.length; i++) { +// const itemStr = wordDict[i] +// var reg = new RegExp("^" + itemStr) +// // console.log( +// // "222222222", +// // reg, +// // string, +// // "reg.test(string)===>", +// // reg.test(string) +// // ) +// if (reg.test(string)) { +// // console.log("itemStr", itemStr, reg, string) +// let _string = string.slice(itemStr.length) +// if (_string.length === 0) { +// flag = true +// return +// } +// // console.log("string", _string) +// dfs(_string) +// } else { +// // dfs(string) +// // return +// } +// } +// } + +// dfs(str) + +// return flag +// } + +// "bb"[("a", "b", "bbb", "bbbb")] +// "leetcode" +// ["leet","code"] +// console.log(wordBreak("bb", ["a", "b", "bbb", "bbbb"])) + + +console.log(1111, wordBreak("leetcode", ["leet","code"])) diff --git "a/\345\212\250\346\200\201\350\247\204\345\210\222/\345\217\257\350\216\267\345\276\227\347\232\204\346\234\200\345\244\247\347\202\271\346\225\260-1423.js" "b/\345\212\250\346\200\201\350\247\204\345\210\222/\345\217\257\350\216\267\345\276\227\347\232\204\346\234\200\345\244\247\347\202\271\346\225\260-1423.js" index 12a63d3..5a24655 100644 --- "a/\345\212\250\346\200\201\350\247\204\345\210\222/\345\217\257\350\216\267\345\276\227\347\232\204\346\234\200\345\244\247\347\202\271\346\225\260-1423.js" +++ "b/\345\212\250\346\200\201\350\247\204\345\210\222/\345\217\257\350\216\267\345\276\227\347\232\204\346\234\200\345\244\247\347\202\271\346\225\260-1423.js" @@ -1,7 +1,7 @@ // 力扣超时 卡在第26个用例 let maxScore = function (cardPoints, k) { let n = cardPoints.length - + //创建一个二维 的全为0的数组 0[][] let prevTimeChunk = [] for (let i = 0; i < n; i++) { for (let j = i; j < n; j++) { diff --git "a/\345\212\250\346\200\201\350\247\204\345\210\222/\345\256\214\345\205\250\345\271\263\346\226\271\346\225\260-279.js" "b/\345\212\250\346\200\201\350\247\204\345\210\222/\345\256\214\345\205\250\345\271\263\346\226\271\346\225\260-279.js" index 35c760f..8333355 100644 --- "a/\345\212\250\346\200\201\350\247\204\345\210\222/\345\256\214\345\205\250\345\271\263\346\226\271\346\225\260-279.js" +++ "b/\345\212\250\346\200\201\350\247\204\345\210\222/\345\256\214\345\205\250\345\271\263\346\226\271\346\225\260-279.js" @@ -20,14 +20,17 @@ let numSquares = function (n) { if (prev < 0) { break; } - + // console.log(j) // 假设i = 10、j = 1 实际上就是在求dp[10 - 1] + 1 // 也就是凑成 9 的最小次数 再加上 1(也就是 1 这个平方数的次数) min = Math.min(min, dp[prev] + 1); j++; } + console.log(i, min); dp[i] = min === Infinity ? 0 : min; } return dp[n]; }; + +console.log(numSquares(45)); diff --git "a/\345\212\250\346\200\201\350\247\204\345\210\222/\346\213\254\345\217\267\347\224\237\346\210\220-22.js" "b/\345\212\250\346\200\201\350\247\204\345\210\222/\346\213\254\345\217\267\347\224\237\346\210\220-22.js" index 07735c1..8a3cedc 100644 --- "a/\345\212\250\346\200\201\350\247\204\345\210\222/\346\213\254\345\217\267\347\224\237\346\210\220-22.js" +++ "b/\345\212\250\346\200\201\350\247\204\345\210\222/\346\213\254\345\217\267\347\224\237\346\210\220-22.js" @@ -1,23 +1,43 @@ +// let generateParenthesis = function (n) { +// let dp = [] +// dp[0] = [''] +// dp[1] = ['()'] + +// for (let i = 2; i <= n; i++) { +// let res = [] +// for (let j = 0; j <= i - 1; j++) { +// let inners = dp[j] +// let outers = dp[i - 1 - j] + +// for (let inner of inners) { +// for (let outer of outers) { +// res.push(`(${inner})${outer}`) +// } +// } +// } +// dp[i] = res +// } +// return dp[n] +// }; let generateParenthesis = function (n) { - let dp = [] - dp[0] = [''] - dp[1] = ['()'] + let res = [] - for (let i = 2; i <= n; i++) { - let res = [] - for (let j = 0; j <= i - 1; j++) { - let inners = dp[j] - let outers = dp[i - 1 - j] + let helper = (left, right, prev) => { + if (left < 0 || right < 0 ) { + return + } + if (left === 0 && right === 0) { + // res.shift(prev) + res.push(prev) + return + } - for (let inner of inners) { - for (let outer of outers) { - res.push(`(${inner})${outer}`) - } - } - } - dp[i] = res - } - return dp[n] -}; + helper(left - 1, right, prev + '(') + helper(left, right - 1, prev + ')') + } -console.log(generateParenthesis(4)) \ No newline at end of file + helper(n, n, '') + return res +} +// console.log(generateParenthesis(4)) +console.log(generateParenthesis(1)) \ No newline at end of file diff --git "a/\345\212\250\346\200\201\350\247\204\345\210\222/\346\234\200\345\244\247\345\255\220\345\272\217\345\222\214-53.js" "b/\345\212\250\346\200\201\350\247\204\345\210\222/\346\234\200\345\244\247\345\255\220\345\272\217\345\222\214-53.js" index 666e03a..e4888b1 100644 --- "a/\345\212\250\346\200\201\350\247\204\345\210\222/\346\234\200\345\244\247\345\255\220\345\272\217\345\222\214-53.js" +++ "b/\345\212\250\346\200\201\350\247\204\345\210\222/\346\234\200\345\244\247\345\255\220\345\272\217\345\222\214-53.js" @@ -2,10 +2,10 @@ * @param {number[]} nums * @return {number} */ -let maxSubArray = function(nums) { +var maxSubArray = function (nums) { let n = nums.length; let dp = []; - + // 代表以第 n-1个数结尾的「连续子数组的最大和」 dp[n - 1] = nums[n - 1]; for (let i = n - 2; i >= 0; i--) { @@ -15,4 +15,50 @@ let maxSubArray = function(nums) { } return Math.max(...dp); -}; \ No newline at end of file +}; + +/** + * @param {number[]} nums + * @return {number} + * @时间复杂度 O(N) + * @空间复杂度 O(N) + */ +var maxSubArray = function (nums) { + let len = nums.length; + if (!len) { + return 0; + } + let dp = []; + // 代表以第 n-1个数结尾的「连续子数组的最大和」 + dp[0] = nums[0]; + for (let i = 1; i < len; i++) { + let cur = nums[i]; + let prev = dp[i - 1]; + dp[i] = prev < 0 ? nums[i] : prev + nums[i]; + } + + return Math.max(...dp); +}; +/** + * @param {number[]} nums + * @return {number} + * @时间复杂度 O(N) + * @空间复杂度 O(1) + */ +var maxSubArray = function (nums) { + let len = nums.length; + if (!len) { + return 0; + } + // let dp = [] + // 代表以第 n-1个数结尾的「连续子数组的最大和」 + let res = (prev = nums[0]); + for (let i = 1; i < len; i++) { + let cur = nums[i]; + // let prev = dp[i - 1] + prev = prev < 0 ? cur : prev + cur; + res = Math.max(res, prev); + } + + return res; +}; diff --git "a/\345\212\250\346\200\201\350\247\204\345\210\222/\351\233\266\351\222\261\345\205\221\346\215\242II-518.js" "b/\345\212\250\346\200\201\350\247\204\345\210\222/\351\233\266\351\222\261\345\205\221\346\215\242II-518.js" index d2fcc5e..75bb26c 100644 --- "a/\345\212\250\346\200\201\350\247\204\345\210\222/\351\233\266\351\222\261\345\205\221\346\215\242II-518.js" +++ "b/\345\212\250\346\200\201\350\247\204\345\210\222/\351\233\266\351\222\261\345\205\221\346\215\242II-518.js" @@ -8,6 +8,7 @@ let change = function (amount, coins) { dp[0] = 1; + // 不同的币种 for (let coin of coins) { for (let i = 1; i <= amount; i++) { if (i >= coin) { diff --git "a/\346\273\221\345\212\250\347\252\227\345\217\243/\346\211\276\345\210\260\345\255\227\347\254\246\344\270\262\344\270\255\346\211\200\346\234\211\345\255\227\346\257\215\345\274\202\344\275\215\350\257\215-438.js" "b/\346\273\221\345\212\250\347\252\227\345\217\243/\346\211\276\345\210\260\345\255\227\347\254\246\344\270\262\344\270\255\346\211\200\346\234\211\345\255\227\346\257\215\345\274\202\344\275\215\350\257\215-438.js" index d1b1485..c498c2b 100644 --- "a/\346\273\221\345\212\250\347\252\227\345\217\243/\346\211\276\345\210\260\345\255\227\347\254\246\344\270\262\344\270\255\346\211\200\346\234\211\345\255\227\346\257\215\345\274\202\344\275\215\350\257\215-438.js" +++ "b/\346\273\221\345\212\250\347\252\227\345\217\243/\346\211\276\345\210\260\345\255\227\347\254\246\344\270\262\344\270\255\346\211\200\346\234\211\345\255\227\346\257\215\345\274\202\344\275\215\350\257\215-438.js" @@ -26,6 +26,7 @@ let findAnagrams = function (s, p) { return res } +// 是否全等? let isAnagrams = function (windowMap, targetMap) { let targetKeys = Object.keys(targetMap) for (let targetKey of targetKeys) { @@ -47,6 +48,8 @@ function addCountToMap(map, str) { } } + +// strs 字符串的map次数 function makeCountMap(strs) { let map = {} for (let i = 0; i < strs.length; i++) { @@ -57,3 +60,6 @@ function makeCountMap(strs) { } console.log(findAnagrams("abab", "ab")) + + +console.log(isAnagrams('cab','cab')) \ No newline at end of file diff --git "a/\346\273\221\345\212\250\347\252\227\345\217\243/\346\227\240\351\207\215\345\244\215\345\255\227\347\254\246\347\232\204\346\234\200\351\225\277\345\255\220\344\270\262-3.js" "b/\346\273\221\345\212\250\347\252\227\345\217\243/\346\227\240\351\207\215\345\244\215\345\255\227\347\254\246\347\232\204\346\234\200\351\225\277\345\255\220\344\270\262-3.js" index 84cbb59..ee340fc 100644 --- "a/\346\273\221\345\212\250\347\252\227\345\217\243/\346\227\240\351\207\215\345\244\215\345\255\227\347\254\246\347\232\204\346\234\200\351\225\277\345\255\220\344\270\262-3.js" +++ "b/\346\273\221\345\212\250\347\252\227\345\217\243/\346\227\240\351\207\215\345\244\215\345\255\227\347\254\246\347\232\204\346\234\200\351\225\277\345\255\220\344\270\262-3.js" @@ -11,6 +11,7 @@ let lengthOfLongestSubstring = function (str) { let max = 0 // 找到的满足条件子串的最长长度 while (left < n) { + console.log("left", left, "right", right) let nextLetter = str[right + 1] if (!freqMap[nextLetter] && nextLetter !== undefined) { freqMap[nextLetter] = 1 @@ -19,10 +20,11 @@ let lengthOfLongestSubstring = function (str) { freqMap[str[left]] = 0 left++ } + console.log("-----------left", left, "right", right) max = Math.max(max, right - left + 1) } return max } -console.log(lengthOfLongestSubstring("pwwkew")) \ No newline at end of file +console.log(lengthOfLongestSubstring("pwwkew")) diff --git "a/\346\273\221\345\212\250\347\252\227\345\217\243/\351\225\277\345\272\246\346\234\200\345\260\217\347\232\204\345\255\220\346\225\260\347\273\204-209.js" "b/\346\273\221\345\212\250\347\252\227\345\217\243/\351\225\277\345\272\246\346\234\200\345\260\217\347\232\204\345\255\220\346\225\260\347\273\204-209.js" index bd05c78..f57235d 100644 --- "a/\346\273\221\345\212\250\347\252\227\345\217\243/\351\225\277\345\272\246\346\234\200\345\260\217\347\232\204\345\255\220\346\225\260\347\273\204-209.js" +++ "b/\346\273\221\345\212\250\347\252\227\345\217\243/\351\225\277\345\272\246\346\234\200\345\260\217\347\232\204\345\255\220\346\225\260\347\273\204-209.js" @@ -3,7 +3,7 @@ * @param {number[]} nums * @return {number} */ -let minSubArrayLen = function (s, nums) { +var minSubArrayLen = function (s, nums) { let n = nums.length // 定义[i,...j]滑动窗口 取这个窗口里的和 let i = 0 @@ -27,4 +27,33 @@ let minSubArrayLen = function (s, nums) { return res === Infinity ? 0 : res } -console.log(minSubArrayLen(7, [2, 3, 1, 2, 4, 3])) +var minSubArrayLen = function (target, nums) { + // function getSumInList(start, end) { + // return sum(nums.slice(start, end + 1)) + // } + let n = nums.length + + let result = 99999999 + for (var i = 0; i < n; i++) { + let left = i + let right = i + let sumData = 0 + while (sumData < target && right <= n - 1) { + right++ + // console.log("nums[right]", nums[right]) + sumData += nums[right - 1] || 0 + } + // console.log("sumData", sumData) + if (sumData >= target) { + result = Math.min(right - left, result) + // console.log("result", result, "right", right, "left", left) + } + } + + return result === 99999999 ? 0 : result +} + +const sum = (arr) => arr.reduce((pre, item) => pre + item, 0) + +// console.log(minSubArrayLen(7, [2, 3, 1, 2, 4, 3])) +console.log(minSubArrayLen(6, [1, 2, 3])) diff --git "a/\350\264\252\345\277\203\347\256\227\346\263\225/\345\210\244\346\226\255\345\255\220\345\272\217\345\210\227-392.js" "b/\350\264\252\345\277\203\347\256\227\346\263\225/\345\210\244\346\226\255\345\255\220\345\272\217\345\210\227-392.js" index bf9f8f1..505285c 100644 --- "a/\350\264\252\345\277\203\347\256\227\346\263\225/\345\210\244\346\226\255\345\255\220\345\272\217\345\210\227-392.js" +++ "b/\350\264\252\345\277\203\347\256\227\346\263\225/\345\210\244\346\226\255\345\255\220\345\272\217\345\210\227-392.js" @@ -3,7 +3,7 @@ * @param {string} t * @return {boolean} */ -let isSubsequence = function (s, t) { +var isSubsequence = function (s, t) { let i = 0 let sl = s.length if (!sl) { @@ -28,7 +28,7 @@ let isSubsequence = function (s, t) { * @param {string} t * @return {boolean} */ -let isSubsequence = function (s, t) { +var isSubsequence = function (s, t) { let sl = s.length if (!sl) { return true @@ -48,3 +48,33 @@ let isSubsequence = function (s, t) { return false } + +// 自己写是繁琐方法 , +var isSubsequence = function (s, t) { + let flag = false + let sl = s.length + if (!sl) { + return true + } + for (let i = 0; i < t.length; i++) { + let str = t + let sFlag = "" + for (let j = 0; j < s.length; j++) { + const _index = str.indexOf(s[j]) + if (_index === -1) { + break + } + sFlag += s[j] + str = str.slice(_index + 1) + if (sFlag === s) { + flag = true + console.log(str, sFlag, _index) + break + } + } + } + + return flag +} + +console.log(isSubsequence("aaaaaa", "bbaaaa")) diff --git "a/\351\200\222\345\275\222\344\270\216\345\233\236\346\272\257/\344\272\214\350\277\233\345\210\266\346\211\213\350\241\250-401.js" "b/\351\200\222\345\275\222\344\270\216\345\233\236\346\272\257/\344\272\214\350\277\233\345\210\266\346\211\213\350\241\250-401.js" index 0a9ce85..0a73c0c 100644 --- "a/\351\200\222\345\275\222\344\270\216\345\233\236\346\272\257/\344\272\214\350\277\233\345\210\266\346\211\213\350\241\250-401.js" +++ "b/\351\200\222\345\275\222\344\270\216\345\233\236\346\272\257/\344\272\214\350\277\233\345\210\266\346\211\213\350\241\250-401.js" @@ -1,54 +1,139 @@ /** - * @param {number} num + * @param {number} turnedOn * @return {string[]} */ -let HOURS = [1, 2, 4, 8] -let MINUTES = [1, 2, 4, 8, 16, 32] +let HOURS = [1, 2, 4, 8]; +let MINUTES = [1, 2, 4, 8, 16, 32]; -let readBinaryWatch = function (num) { - let res = [] +var readBinaryWatch = function (num) { + let res = []; let combine = (arr, num) => { if (num === 0) { - return [0] + return [0]; } - let res = [] + let res = []; let helper = (start, prevCount, prevSum) => { if (prevCount === num) { - res.push(prevSum) - return + res.push(prevSum); + return; } for (let i = start; i < arr.length; i++) { - let cur = arr[i] - helper(i + 1, prevCount + 1, prevSum + cur) + let cur = arr[i]; + helper(i + 1, prevCount + 1, prevSum + cur); } - } - helper(0, 0, 0) - return res - } + }; + helper(0, 0, 0); + return res; + }; for (let i = 0; i <= num; i++) { - let hours = combine(HOURS, i) - let minutes = combine(MINUTES, num - i) + let hours = combine(HOURS, i); + let minutes = combine(MINUTES, num - i); for (let hour of hours) { - if (hour > 11) continue + if (hour > 11) continue; for (let minute of minutes) { if (minute > 59) { - continue + continue; } - res.push(`${hour}:${padLeft(minute)}`) + res.push(`${hour}:${padLeft(minute)}`); } } } - return res -} + return res; +}; function padLeft(num) { - let str = num.toString() + let str = num.toString(); if (str.length === 1) { - str = `0${str}` + str = `0${str}`; } - return str -} \ No newline at end of file + return str; +} + +var readBinaryWatch = function (num) { + // 拿到各种组合情况 + let commonResult = (list, maxNum) => { + if (maxNum === 0) { + return [[0]]; + } + let res = []; + let helper = (index, pre) => { + for (let dig = index; dig < list.length; dig++) { + let cur = pre.concat(list[dig]); + if (cur.length === maxNum) { + res.push(cur); + // return + } + helper(dig + 1, cur); + } + }; + helper(0, []); + return res; + }; + // var getHourSize = commonResult(HOURS, 11) + // var getHourSize = commonResult(HOURS, 3) + // var getMinSize = commonResult(MINUTES, 3) + // getMinSize = getMinSize.filter((item) => { + // return getSum(item) <= 59 + // }) + // getHourSize = getHourSize.filter((item) => { + // return getSum(item) <= 11 + // }) + let res = []; + for (let i = 0; i <= num; i++) { + let j = num - i; + let getHourSize = commonResult(HOURS, i); + let getMinSize = commonResult(MINUTES, j); + console.log("getHourSize", JSON.stringify(getHourSize)); + console.log("getMinSize", JSON.stringify(getMinSize)); + // getMinSize = getMinSize.filter((item) => { + // return getSum(item) <= 59 + // }) + // getHourSize = getHourSize.filter((item) => { + // return getSum(item) <= 11 + // }) + // console.log("getHourSize", getHourSize, i) + // console.log("getMinSize", getMinSize, j) + + for (let x = 0; x < getHourSize.length; x++) { + let hour = getSum(getHourSize[x]); + if (hour > 11) { + continue; + } + for (let y = 0; y < getMinSize.length; y++) { + let minute = getSum(getMinSize[y]); + if (minute > 59) { + continue; + } + res.push(`${hour}:${padLeft(minute)}`); + } + } + } + + // for (let x = 0; x < getHourSize.length; x++) { + // let hour = getSum(getHourSize[x]) + // if (hour > 11) { + // continue + // } + // for (let y = 0; y < getMinSize.length; y++) { + // let minute = getSum(getMinSize[y]) + // if (minute > 59) { + // continue + // } + // res.push(`${hour}:${padLeft(minute)}`) + // } + // } + + console.log("res", res); + + return res; +}; + +var getSum = (arr) => { + return arr.reduce((pre, item) => pre + item, 0); +}; + +readBinaryWatch(1); diff --git "a/\351\200\222\345\275\222\344\270\216\345\233\236\346\272\257/\345\215\225\350\257\215\346\220\234\347\264\242-79.js" "b/\351\200\222\345\275\222\344\270\216\345\233\236\346\272\257/\345\215\225\350\257\215\346\220\234\347\264\242-79.js" index c9306df..06da8b8 100644 --- "a/\351\200\222\345\275\222\344\270\216\345\233\236\346\272\257/\345\215\225\350\257\215\346\220\234\347\264\242-79.js" +++ "b/\351\200\222\345\275\222\344\270\216\345\233\236\346\272\257/\345\215\225\350\257\215\346\220\234\347\264\242-79.js" @@ -3,62 +3,67 @@ * @param {string} word * @return {boolean} */ -let directions = [[-1, 0], [1, 0], [0, -1], [0, 1]] // 左 右 上 下 +let directions = [ + [-1, 0], + [1, 0], + [0, -1], + [0, 1], +]; // 左 右 上 下 let exist = function (board, word) { - let maxY = board.length - if (!maxY) return false - let maxX = board[0].length + let maxY = board.length; + if (!maxY) return false; + let maxX = board[0].length; - // 二维数组记录已访问过的元素 - let visited = new Array(maxY) - for (let y = 0; y < visited.length; y++) { - visited[y] = new Array(maxX) - } + // 二维数组记录已访问过的元素 + let visited = new Array(maxY); + for (let y = 0; y < visited.length; y++) { + visited[y] = new Array(maxX); + } - let inArea = (x, y) => { - return x >= 0 && x < maxX && y >= 0 && y < maxY - } + let inArea = (x, y) => { + return x >= 0 && x < maxX && y >= 0 && y < maxY; + }; - let search = (startX, startY, wordIndex) => { - // 当前起始字符不匹配 直接失败 - let curCell = board[startY][startX] - let curChar = word[wordIndex] - if (curCell !== curChar) { - return false - } + let search = (startX, startY, wordIndex) => { + // 当前起始字符不匹配 直接失败 + let curCell = board[startY][startX]; + let curChar = word[wordIndex]; + if (curCell !== curChar) { + return false; + } - // 如果递归到最后一位字符 就直接返回最后一位字符是否匹配成功 - if (wordIndex === word.length - 1) { - return curChar === curChar - } + // 如果递归到最后一位字符 就直接返回最后一位字符是否匹配成功 + if (wordIndex === word.length - 1) { + return curChar === curChar; + } - // 进一步递归 先记录为已访问元素 防止递归的时候重复访问 - visited[startY][startX] = true + // 进一步递归 先记录为已访问元素 防止递归的时候重复访问 + visited[startY][startX] = true; - for (let direction of directions) { - let [x, y] = direction - let nextX = startX + x - let nextY = startY + y + for (let direction of directions) { + let [x, y] = direction; + let nextX = startX + x; + let nextY = startY + y; - // 需要保证未越界且未被访问过 - if (inArea(nextX, nextY) && !visited[nextY][nextX]) { - if (search(nextX, nextY, wordIndex + 1)) { - return true - } - } + // 需要保证未越界且未被访问过 + if (inArea(nextX, nextY) && !visited[nextY][nextX]) { + if (search(nextX, nextY, wordIndex + 1)) { + return true; } - // 重置已访问标记位 - visited[startY][startX] = false + } } + // 重置已访问标记位 + visited[startY][startX] = false; + }; - for (let y = 0; y < maxY; y++) { - for (let x = 0; x < maxX; x++) { - if (search(x, y, 0)) { - return true - } - } + for (let y = 0; y < maxY; y++) { + for (let x = 0; x < maxX; x++) { + if (search(x, y, 0)) { + return true; + } } + } - return false -}; \ No newline at end of file + return false; +}; diff --git "a/\351\200\222\345\275\222\344\270\216\345\233\236\346\272\257/\345\222\214\344\270\272K\347\232\204\345\255\220\346\225\260\347\273\204-560.js" "b/\351\200\222\345\275\222\344\270\216\345\233\236\346\272\257/\345\222\214\344\270\272K\347\232\204\345\255\220\346\225\260\347\273\204-560.js" index 25582b5..b55316d 100644 --- "a/\351\200\222\345\275\222\344\270\216\345\233\236\346\272\257/\345\222\214\344\270\272K\347\232\204\345\255\220\346\225\260\347\273\204-560.js" +++ "b/\351\200\222\345\275\222\344\270\216\345\233\236\346\272\257/\345\222\214\344\270\272K\347\232\204\345\255\220\346\225\260\347\273\204-560.js" @@ -20,4 +20,36 @@ let subarraySum = function (nums, k) { } } return res -}; \ No newline at end of file +}; + +/** + * @param {number[]} nums + * @param {number} k + * @return {number} + */ +let subarraySum = function (nums, k) { + let n = nums.length + if (!n) { + return 0 + } + + let res = 0 + for (let i = 0; i < n; i++) { + let total = 0 + let flag = false + for (let j = i; j < n; j++) { + total += nums[j] + if (total === k) { + res += 1 + flag = true + // continue + } + } + if(flag){ + // continue + } + } + return res +}; + +// 超时了 orz \ No newline at end of file diff --git "a/\351\200\222\345\275\222\344\270\216\345\233\236\346\272\257/\351\273\204\351\207\221\347\237\277\345\267\245-1219.js" "b/\351\200\222\345\275\222\344\270\216\345\233\236\346\272\257/\351\273\204\351\207\221\347\237\277\345\267\245-1219.js" index 0d8d53c..3b00c4c 100644 --- "a/\351\200\222\345\275\222\344\270\216\345\233\236\346\272\257/\351\273\204\351\207\221\347\237\277\345\267\245-1219.js" +++ "b/\351\200\222\345\275\222\344\270\216\345\233\236\346\272\257/\351\273\204\351\207\221\347\237\277\345\267\245-1219.js" @@ -2,7 +2,7 @@ * @param {number[][]} grid * @return {number} */ -let getMaximumGold = function (grid) { +var getMaximumGold = function (grid) { let maxY = grid.length if (maxY === 0) { return 0 diff --git "a/\351\223\276\350\241\250/\344\270\244\344\270\244\344\272\244\346\215\242\351\223\276\350\241\250\344\270\255\347\232\204\350\212\202\347\202\271-24.js" "b/\351\223\276\350\241\250/\344\270\244\344\270\244\344\272\244\346\215\242\351\223\276\350\241\250\344\270\255\347\232\204\350\212\202\347\202\271-24.js" index 527b48a..22493ad 100644 --- "a/\351\223\276\350\241\250/\344\270\244\344\270\244\344\272\244\346\215\242\351\223\276\350\241\250\344\270\255\347\232\204\350\212\202\347\202\271-24.js" +++ "b/\351\223\276\350\241\250/\344\270\244\344\270\244\344\272\244\346\215\242\351\223\276\350\241\250\344\270\255\347\232\204\350\212\202\347\202\271-24.js" @@ -8,7 +8,7 @@ let swapPairs = function (head) { node.next.next = node if (tempNextNext) { node.next = helper(tempNextNext) - }else { + } else { node.next = null } } @@ -25,4 +25,4 @@ node.next = new ListNode(2) node.next.next = new ListNode(3) node.next.next.next = new ListNode(4) -console.log(swapPairs(node)) +console.log(1111, JSON.stringify(swapPairs(node))) diff --git "a/\351\223\276\350\241\250/\344\270\244\346\225\260\347\233\270\345\212\240-3.js" "b/\351\223\276\350\241\250/\344\270\244\346\225\260\347\233\270\345\212\240-2.js" similarity index 59% rename from "\351\223\276\350\241\250/\344\270\244\346\225\260\347\233\270\345\212\240-3.js" rename to "\351\223\276\350\241\250/\344\270\244\346\225\260\347\233\270\345\212\240-2.js" index 9eec915..f429157 100644 --- "a/\351\223\276\350\241\250/\344\270\244\346\225\260\347\233\270\345\212\240-3.js" +++ "b/\351\223\276\350\241\250/\344\270\244\346\225\260\347\233\270\345\212\240-2.js" @@ -10,7 +10,7 @@ * @param {ListNode} l2 * @return {ListNode} */ -let addTwoNumbers = function (l1, l2) { +var addTwoNumbers = function (l1, l2) { let i = 0 let root = new ListNode() let cur = root @@ -57,3 +57,38 @@ let addTwoNumbers = function (l1, l2) { return root.next } + +var addTwoNumbers = function (l1, l2) { + let i = 0 + let root = new ListNode() + let cur = root + let plus = false + + let traverse = (node1, node2, isPlus) => { + if (!node1 && !node2) { + return + } + + if (node1.val && node2.val) { + let sum = node1.val + node2.val + (isPlus ? 1 : 0) + if (sum >= 10) { + sum %= 10 + isPlus = true + } else { + isPlus = false + } + cur.val = sum + cur.next = new ListNode() + } + + traverse(node1.next, node2.next, isPlus) + } + + traverse(l1, l2, false) + + if (plus) { + cur.next = new ListNode(1) + } + + return root +} diff --git "a/\351\223\276\350\241\250/\345\217\215\350\275\254\351\223\276\350\241\250-206.js" "b/\351\223\276\350\241\250/\345\217\215\350\275\254\351\223\276\350\241\250-206.js" index 32097f9..2c4cf59 100644 --- "a/\351\223\276\350\241\250/\345\217\215\350\275\254\351\223\276\350\241\250-206.js" +++ "b/\351\223\276\350\241\250/\345\217\215\350\275\254\351\223\276\350\241\250-206.js" @@ -1,3 +1,14 @@ +/** + * Definition for singly-linked list. + * function ListNode(val, next) { + * this.val = (val===undefined ? 0 : val) + * this.next = (next===undefined ? null : next) + * } + */ +/** + * @param {ListNode} head + * @return {ListNode} + */ function reverse(head) { let prev = null let cur = head @@ -11,3 +22,19 @@ function reverse(head) { // 返回反转后的头节点 return prev } + +var reverseList = function (head) { + if (!head) { + return null + } + let newList = null + let cur = head + while (cur) { + // newList = cur + var curNext = cur.next + cur.next = newList + newList = cur + cur = curNext + } + return newList +} diff --git "a/\351\223\276\350\241\250/\345\217\215\350\275\254\351\223\276\350\241\250II-92.js" "b/\351\223\276\350\241\250/\345\217\215\350\275\254\351\223\276\350\241\250II-92.js" index ab69981..e030e25 100644 --- "a/\351\223\276\350\241\250/\345\217\215\350\275\254\351\223\276\350\241\250II-92.js" +++ "b/\351\223\276\350\241\250/\345\217\215\350\275\254\351\223\276\350\241\250II-92.js" @@ -71,4 +71,54 @@ function reverse(head) { let node = new ListNode(3) node.next = new ListNode(5) +var reverseBetween = function (head, left, right) { + console.log('left', left, 'right', right) + if (left === right) { + return head + } + let node = head + let index = 1 + let nodeLeftPre = null + let nodeLeft = null + let nodeLeftNext = null + let nodeRightPre = null + let nodeRight = null + let nodeRightNext = null + let flag = false + while (index <= right) { + if (left === 1) { + nodeLeft = node + nodeLeftNext = node.next + flag = true + } + else if (index === left - 1) { + nodeLeftPre = node + nodeLeft = node.next + nodeLeftNext = node.next.next + } + if (index === right - 1) { + nodeRight = node.next + nodeRightNext = node.next.next + nodeRightPre = node + if (flag) { + nodeRight.next = nodeLeftNext + nodeRightPre.next = nodeLeft + nodeLeft.next = nodeRightNext + } else { + nodeLeftPre.next = nodeRight + nodeRight.next = nodeLeftNext + nodeRightPre.next = nodeLeft + nodeLeft.next = nodeRightNext + } + + } + node = node.next + index++ + } + return head + +}; + console.log(JSON.stringify(reverseBetween(node, 1, 1))) + +