Skip to content

Commit 6c15e9c

Browse files
mod: spider
1 parent c533170 commit 6c15e9c

File tree

129 files changed

+846
-57
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

129 files changed

+846
-57
lines changed

‎scripts/generate-leetcode.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ rowMarkdowns.forEach(filename => {
4444
})
4545

4646
})
47+
4748

4849

4950
let oCustomStruct = {
@@ -58,7 +59,7 @@ rowMarkdowns.forEach(filename => {
5859
Logger.success(`开始生成 "${filename}"`)
5960

6061
Utils.writeFileSync('spider/yield-db-json',`${filename.slice(0,-3)}.json`, JSON.stringify(oCustomStruct, null, 2))
61-
62+
6263
Logger.success(`生成 "${filename}" 完毕`)
6364

6465
})

‎scripts/utils.js

+5
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@ class Utils {
2020
}
2121

2222

23+
static getKeyMatterReg() {
24+
return /(?:# )([.|[\r\n]]*?)(?:#)/g
25+
}
26+
27+
2328
static readFileSync(relativePath,filename) {
2429
return fs.readFileSync(pathResolve(__dirname, '../',relativePath,filename),{encoding:'utf8'})
2530
}

‎spider/yield-db-json/1.TwoSum.en.json

+6-1
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,10 @@
66
"tags": [
77
"TODO"
88
],
9-
"reslove": []
9+
"reslove": [
10+
{
11+
"lang": "js",
12+
"code": "\n/**\n * @param {number[]} nums\n * @param {number} target\n * @return {number[]}\n */\nconst twoSum = function (nums, target) {\n const map = new Map();\n for (let i = 0; i < nums.length; i++) {\n const diff = target - nums[i];\n if (map.has(diff)) {\n return [map.get(diff), i];\n }\n map.set(nums[i], i);\n }\n}\n"
13+
}
14+
]
1015
}

‎spider/yield-db-json/1.TwoSum.json

+6-1
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,10 @@
66
"tags": [
77
"TODO"
88
],
9-
"reslove": []
9+
"reslove": [
10+
{
11+
"lang": "js",
12+
"code": "\n/**\n * @param {number[]} nums\n * @param {number} target\n * @return {number[]}\n */\nconst twoSum = function (nums, target) {\n const map = new Map();\n for (let i = 0; i < nums.length; i++) {\n const diff = target - nums[i];\n if (map.has(diff)) {\n return [map.get(diff), i];\n }\n map.set(nums[i], i);\n }\n}\n"
13+
}
14+
]
1015
}

‎spider/yield-db-json/1011.capacity-to-ship-packages-within-d-days-cn.json

+4
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@
1414
{
1515
"lang": "python",
1616
"code": "\nclass Solution:\n def shipWithinDays(self, weights: List[int], D: int) -> int:\n lo = 0\n hi = 0\n\n def canShip(opacity):\n days = 1\n remain = opacity\n for weight in weights:\n if weight > opacity:\n return False\n remain -= weight\n if remain < 0:\n days += 1\n remain = opacity - weight\n return days <= D\n\n for weight in weights:\n hi += weight\n while lo < hi:\n mid = (lo + hi) // 2\n if canShip(mid):\n hi = mid\n else:\n lo = mid + 1\n\n return lo\n"
17+
},
18+
{
19+
"lang": "js",
20+
"code": "\n/**\n * @param {number[]} weights\n * @param {number} D\n * @return {number}\n */\nvar shipWithinDays = function(weights, D) {\n let high = weights.reduce((acc, cur) => acc + cur)\n let low = 0\n\n while(low < high) {\n let mid = Math.floor((high + low) / 2)\n if (canShip(mid)) {\n high = mid\n } else {\n low = mid + 1\n }\n }\n\n return low\n\n function canShip(opacity) {\n let remain = opacity\n let count = 1\n for (let weight of weights) {\n if (weight > opacity) {\n return false\n }\n remain -= weight\n if (remain < 0) {\n count++\n remain = opacity - weight\n }\n if (count > D) {\n return false\n }\n }\n return count <= D\n }\n};\n"
1721
}
1822
]
1923
}

‎spider/yield-db-json/1011.capacity-to-ship-packages-within-d-days-en.json

+4
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@
1414
{
1515
"lang": "python",
1616
"code": "\r\nclass Solution:\r\n def shipWithinDays(self, weights: List[int], D: int) -> int:\r\n lo = 0\r\n hi = 0\r\n\r\n def canShip(opacity):\r\n days = 1\r\n remain = opacity\r\n for weight in weights:\r\n if weight > opacity:\r\n return False\r\n remain -= weight\r\n if remain < 0:\r\n days += 1\r\n remain = opacity - weight\r\n return days <= D\r\n\r\n for weight in weights:\r\n hi += weight\r\n while lo < hi:\r\n mid = (lo + hi) // 2\r\n if canShip(mid):\r\n hi = mid\r\n else:\r\n lo = mid + 1\r\n\r\n return lo\r\n"
17+
},
18+
{
19+
"lang": "js",
20+
"code": "\r\n/**\r\n * @param {number[]} weights\r\n * @param {number} D\r\n * @return {number}\r\n */\r\nvar shipWithinDays = function(weights, D) {\r\n let high = weights.reduce((acc, cur) => acc + cur)\r\n let low = 0\r\n\r\n while(low < high) {\r\n let mid = Math.floor((high + low) / 2)\r\n if (canShip(mid)) {\r\n high = mid\r\n } else {\r\n low = mid + 1\r\n }\r\n }\r\n\r\n return low\r\n\r\n function canShip(opacity) {\r\n let remain = opacity\r\n let count = 1\r\n for (let weight of weights) {\r\n if (weight > opacity) {\r\n return false\r\n }\r\n remain -= weight\r\n if (remain < 0) {\r\n count++\r\n remain = opacity - weight\r\n }\r\n if (count > D) {\r\n return false\r\n }\r\n }\r\n return count <= D\r\n }\r\n};\r\n"
1721
}
1822
]
1923
}

‎spider/yield-db-json/102.binary-tree-level-order-traversal.json

+4
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@
1010
{
1111
"lang": "python",
1212
"code": "\n# Definition for a binary tree node.\n# class TreeNode:\n# def __init__(self, x):\n# self.val = x\n# self.left = None\n# self.right = None\n\nclass Solution:\n def levelOrder(self, root: TreeNode) -> List[List[int]]:\n \"\"\"递归法\"\"\"\n if root is None:\n return []\n \n result = []\n \n def add_to_result(level, node):\n \"\"\"递归函数\n :param level int 当前在二叉树的层次\n :param node TreeNode 当前节点\n \"\"\"\n if level > len(result) - 1:\n result.append([])\n \n result[level].append(node.val)\n if node.left:\n add_to_result(level+1, node.left)\n if node.right:\n add_to_result(level+1, node.right)\n \n add_to_result(0, root)\n return result\n"
13+
},
14+
{
15+
"lang": "js",
16+
"code": "\n/*\n * @lc app=leetcode id=102 lang=javascript\n *\n * [102] Binary Tree Level Order Traversal\n *\n * https://leetcode.com/problems/binary-tree-level-order-traversal/description/\n *\n * algorithms\n * Medium (47.18%)\n * Total Accepted: 346.4K\n * Total Submissions: 731.3K\n * Testcase Example: '[3,9,20,null,null,15,7]'\n *\n * Given a binary tree, return the level order traversal of its nodes' values.\n * (ie, from left to right, level by level).\n *\n *\n * For example:\n * Given binary tree [3,9,20,null,null,15,7],\n *\n *\n * ⁠ 3\n * ⁠ / \\\n * ⁠ 9 20\n * ⁠ / \\\n * ⁠ 15 7\n *\n *\n *\n * return its level order traversal as:\n *\n * [\n * ⁠ [3],\n * ⁠ [9,20],\n * ⁠ [15,7]\n * ]\n *\n *\n */\n/**\n * Definition for a binary tree node.\n * function TreeNode(val) {\n * this.val = val;\n * this.left = this.right = null;\n * }\n */\n/**\n * @param {TreeNode} root\n * @return {number[][]}\n */\nvar levelOrder = function(root) {\n if (!root) return [];\n const items = []; // 存放所有节点\n const queue = [root, null]; // null 简化操作\n let levelNodes = []; // 存放每一层的节点\n\n while (queue.length > 0) {\n const t = queue.shift();\n\n if (t) {\n levelNodes.push(t.val)\n if (t.left) {\n queue.push(t.left);\n }\n if (t.right) {\n queue.push(t.right);\n }\n } else { // 一层已经遍历完了\n items.push(levelNodes);\n levelNodes = [];\n if (queue.length > 0) {\n queue.push(null)\n }\n }\n }\n\n return items;\n};\n\n"
1317
}
1418
]
1519
}

‎spider/yield-db-json/103.binary-tree-zigzag-level-order-traversal.json

+6-1
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,10 @@
66
"tags": [
77
"TODO"
88
],
9-
"reslove": []
9+
"reslove": [
10+
{
11+
"lang": "js",
12+
"code": "\n/*\n * @lc app=leetcode id=103 lang=javascript\n *\n * [103] Binary Tree Zigzag Level Order Traversal\n *\n * https://leetcode.com/problems/binary-tree-zigzag-level-order-traversal/description/\n *\n * algorithms\n * Medium (40.57%)\n * Total Accepted: 201.2K\n * Total Submissions: 493.7K\n * Testcase Example: '[3,9,20,null,null,15,7]'\n *\n * Given a binary tree, return the zigzag level order traversal of its nodes'\n * values. (ie, from left to right, then right to left for the next level and\n * alternate between).\n * \n * \n * For example:\n * Given binary tree [3,9,20,null,null,15,7],\n * \n * ⁠ 3\n * ⁠ / \\\n * ⁠ 9 20\n * ⁠ / \\\n * ⁠ 15 7\n * \n * \n * \n * return its zigzag level order traversal as:\n * \n * [\n * ⁠ [3],\n * ⁠ [20,9],\n * ⁠ [15,7]\n * ]\n * \n * \n */\n/**\n * Definition for a binary tree node.\n * function TreeNode(val) {\n * this.val = val;\n * this.left = this.right = null;\n * }\n */\n/**\n * @param {TreeNode} root\n * @return {number[][]}\n */\nvar zigzagLevelOrder = function(root) {\n if (!root) return []; \n const items = [];\n let isOdd = true;\n let levelNodes = [];\n \n const queue = [root, null];\n\n\n while(queue.length > 0) {\n const t = queue.shift();\n\n if (t) {\n levelNodes.push(t.val)\n if (t.left) {\n queue.push(t.left)\n }\n if (t.right) {\n queue.push(t.right)\n }\n } else {\n if (!isOdd) {\n levelNodes = levelNodes.reverse();\n }\n items.push(levelNodes)\n levelNodes = [];\n isOdd = !isOdd;\n if (queue.length > 0) {\n queue.push(null);\n }\n }\n }\n\n return items\n \n};\n"
13+
}
14+
]
1015
}

‎spider/yield-db-json/104.maximum-depth-of-binary-tree.json

+8
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,14 @@
1010
{
1111
"lang": "python",
1212
"code": "\nclass Solution:\n def maxDepth(self, root: TreeNode) -> int:\n if not root: return 0\n q, depth = [root, None], 1\n while q:\n node = q.pop(0)\n if node:\n if node.left: q.append(node.left)\n if node.right: q.append(node.right)\n elif q:\n q.append(None)\n depth += 1\n return depth\n"
13+
},
14+
{
15+
"lang": "js",
16+
"code": "\nvar maxDepth = function(root) {\n if (!root) return 0;\n if (!root.left && !root.right) return 1;\n return 1 + Math.max(maxDepth(root.left), maxDepth(root.right));\n};\n"
17+
},
18+
{
19+
"lang": "js",
20+
"code": "\n/*\n * @lc app=leetcode id=104 lang=javascript\n *\n * [104] Maximum Depth of Binary Tree\n */\n/**\n * Definition for a binary tree node.\n * function TreeNode(val) {\n * this.val = val;\n * this.left = this.right = null;\n * }\n */\n/**\n * @param {TreeNode} root\n * @return {number}\n */\nvar maxDepth = function(root) {\n if (!root) return 0;\n if (!root.left && !root.right) return 1;\n\n // 层次遍历 BFS\n let cur = root;\n const queue = [root, null];\n let depth = 1;\n\n while ((cur = queue.shift()) !== undefined) {\n if (cur === null) {\n // 注意⚠️: 不处理会无限循环,进而堆栈溢出\n if (queue.length === 0) return depth;\n depth++;\n queue.push(null);\n continue;\n }\n const l = cur.left;\n const r = cur.right;\n\n if (l) queue.push(l);\n if (r) queue.push(r);\n }\n\n return depth;\n};\n"
1321
}
1422
]
1523
}

‎spider/yield-db-json/11.container-with-most-water.json

+10-1
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,14 @@
66
"tags": [
77
"TODO"
88
],
9-
"reslove": []
9+
"reslove": [
10+
{
11+
"lang": "js",
12+
"code": "\n // 这个解法比较暴力,效率比较低\n // 时间复杂度是O(n^2)\n let max = 0;\n for(let i = 0; i < height.length; i++) {\n for(let j = i + 1; j < height.length; j++) {\n const currentArea = Math.abs(i - j) * Math.min(height[i], height[j]);\n if (currentArea > max) {\n max = currentArea;\n }\n }\n }\n return max;\n\n"
13+
},
14+
{
15+
"lang": "js",
16+
"code": "\n/**\n * @param {number[]} height\n * @return {number}\n */\nvar maxArea = function(height) {\n if (!height || height.length <= 1) return 0;\n \n let leftPos = 0;\n let rightPos = height.length - 1;\n let max = 0;\n while(leftPos < rightPos) {\n \n const currentArea = Math.abs(leftPos - rightPos) * Math.min(height[leftPos] , height[rightPos]);\n if (currentArea > max) {\n max = currentArea;\n }\n // 更新小的\n if (height[leftPos] < height[rightPos]) {\n leftPos++;\n } else { // 如果相等就随便了\n rightPos--;\n }\n }\n\n return max;\n};\n"
17+
}
18+
]
1019
}

‎spider/yield-db-json/113.path-sum-ii.json

+4
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@
1010
{
1111
"lang": "python",
1212
"code": "\n# Definition for a binary tree node.\n# class TreeNode:\n# def __init__(self, x):\n# self.val = x\n# self.left = None\n# self.right = None\n\nclass Solution:\n def pathSum(self, root: TreeNode, sum: int) -> List[List[int]]:\n if not root:\n return []\n \n result = []\n \n def trace_node(pre_list, left_sum, node):\n new_list = pre_list.copy()\n new_list.append(node.val)\n if not node.left and not node.right:\n # 这个判断可以和上面的合并,但分开写会快几毫秒,可以省去一些��必要的判断\n if left_sum == node.val:\n result.append(new_list)\n else:\n if node.left:\n trace_node(new_list, left_sum-node.val, node.left)\n if node.right:\n trace_node(new_list, left_sum-node.val, node.right)\n \n trace_node([], sum, root)\n return result\n"
13+
},
14+
{
15+
"lang": "js",
16+
"code": "\n/*\n * @lc app=leetcode id=113 lang=javascript\n *\n * [113] Path Sum II\n */\n/**\n * Definition for a binary tree node.\n * function TreeNode(val) {\n * this.val = val;\n * this.left = this.right = null;\n * }\n */\nfunction backtrack(root, sum, res, tempList) {\n if (root === null) return;\n if (root.left === null && root.right === null && sum === root.val)\n return res.push([...tempList, root.val]);\n\n tempList.push(root.val);\n backtrack(root.left, sum - root.val, res, tempList);\n\n backtrack(root.right, sum - root.val, res, tempList);\n tempList.pop();\n}\n/**\n * @param {TreeNode} root\n * @param {number} sum\n * @return {number[][]}\n */\nvar pathSum = function(root, sum) {\n if (root === null) return [];\n const res = [];\n backtrack(root, sum, res, []);\n return res;\n};\n"
1317
}
1418
]
1519
}

‎spider/yield-db-json/121.best-time-to-buy-and-sell-stock.json

+4
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@
1010
{
1111
"lang": "python",
1212
"code": "\nclass Solution:\n def maxProfit(self, prices: 'List[int]') -> int:\n if not prices: return 0\n\n min_price = float('inf')\n max_profit = 0\n\n for price in prices:\n if price < min_price:\n min_price = price\n elif max_profit < price - min_price:\n max_profit = price - min_price\n return max_profit\n"
13+
},
14+
{
15+
"lang": "js",
16+
"code": "\n/**\n * @param {number[]} prices\n * @return {number}\n */\nvar maxProfit = function(prices) {\n let min = prices[0];\n let profit = 0;\n // 7 1 5 3 6 4\n for(let i = 1; i < prices.length; i++) {\n if (prices[i] > prices[i -1]) {\n profit = Math.max(profit, prices[i] - min);\n } else {\n min = Math.min(min, prices[i]);;\n }\n }\n\n return profit;\n};\n"
1317
}
1418
]
1519
}

‎spider/yield-db-json/122.best-time-to-buy-and-sell-stock-ii.json

+4
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@
1010
{
1111
"lang": "python",
1212
"code": "\nclass Solution:\n def maxProfit(self, prices: 'List[int]') -> int:\n gains = [prices[i] - prices[i-1] for i in range(1, len(prices))\n if prices[i] - prices[i-1] > 0]\n return sum(gains)\n#评论区里都讲这是一道开玩笑的送分题.\n"
13+
},
14+
{
15+
"lang": "js",
16+
"code": "\n/**\n * @param {number[]} prices\n * @return {number}\n */\nvar maxProfit = function(prices) {\n let profit = 0;\n\n for(let i = 1; i < prices.length; i++) {\n if (prices[i] > prices[i -1]) {\n profit = profit + prices[i] - prices[i - 1];\n }\n }\n\n return profit;\n};\n"
1317
}
1418
]
1519
}

‎spider/yield-db-json/124.binary-tree-maximum-path-sum.json

+4
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@
1010
{
1111
"lang": "java",
1212
"code": "\n/**\n * Definition for a binary tree node.\n * public class TreeNode {\n * int val;\n * TreeNode left;\n * TreeNode right;\n * TreeNode(int x) { val = x; }\n * }\n */\nclass Solution {\n int ans;\n public int maxPathSum(TreeNode root) {\n ans = Integer.MIN_VALUE;\n helper(root); // recursion\n return ans;\n }\n\n public int helper(TreeNode root) {\n if (root == null) return 0;\n int leftMax = Math.max(0, helper(root.left)); // find the max sub-path sum in left sub-tree\n int rightMax = Math.max(0, helper(root.right)); // find the max sub-path sum in right sub-tree\n ans = Math.max(ans, leftMax+rightMax+root.val); // find the max path sum at current node\n return max(leftMax, rightMax) + root.val; // according to the definition of path, the return value of current node can only be that the sum of current node value plus either left or right max path sum.\n }\n}\n"
13+
},
14+
{
15+
"lang": "js",
16+
"code": "\n\n\n/*\n * @lc app=leetcode id=124 lang=javascript\n *\n * [124] Binary Tree Maximum Path Sum\n */\n/**\n * Definition for a binary tree node.\n * function TreeNode(val) {\n * this.val = val;\n * this.left = this.right = null;\n * }\n */\nfunction helper(node, payload) {\n if (node === null) return 0;\n\n const l = helper(node.left, payload);\n const r = helper(node.right, payload);\n\n payload.max = Math.max(\n node.val + Math.max(0, l) + Math.max(0, r),\n payload.max\n );\n\n return node.val + Math.max(l, r, 0);\n}\n/**\n * @param {TreeNode} root\n * @return {number}\n */\nvar maxPathSum = function(root) {\n if (root === null) return 0;\n const payload = {\n max: root.val\n };\n helper(root, payload);\n return payload.max;\n};\n"
1317
}
1418
]
1519
}

‎spider/yield-db-json/125.valid-palindrome.json

+4
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@
1010
{
1111
"lang": "python",
1212
"code": "\nclass Solution:\n def isPalindrome(self, s: str) -> bool:\n left, right = 0, len(s) - 1\n while left < right:\n if not s[left].isalnum():\n left += 1\n continue\n if not s[right].isalnum():\n right -= 1\n continue\n if s[left].lower() == s[right].lower():\n left += 1\n right -= 1\n else:\n break\n return right <= left\n\n def isPalindrome2(self, s: str) -> bool:\n \"\"\"\n 使用语言特性进行求解\n \"\"\"\n s = ''.join(i for i in s if i.isalnum()).lower()\n return s == s[::-1]\n"
13+
},
14+
{
15+
"lang": "js",
16+
"code": "\n\n/*\n * @lc app=leetcode id=125 lang=javascript\n *\n * [125] Valid Palindrome\n */\n// 只处理英文字符(题目忽略大小写,我们前面全部转化成了小写, 因此这里我们只判断小写)和数字\nfunction isValid(c) {\n const charCode = c.charCodeAt(0);\n const isDigit =\n charCode >= \"0\".charCodeAt(0) && charCode <= \"9\".charCodeAt(0);\n const isChar = charCode >= \"a\".charCodeAt(0) && charCode <= \"z\".charCodeAt(0);\n\n return isDigit || isChar;\n}\n/**\n * @param {string} s\n * @return {boolean}\n */\nvar isPalindrome = function(s) {\n s = s.toLowerCase();\n let left = 0;\n let right = s.length - 1;\n\n while (left < right) {\n if (!isValid(s[left])) {\n left++;\n continue;\n }\n if (!isValid(s[right])) {\n right--;\n continue;\n }\n\n if (s[left] === s[right]) {\n left++;\n right--;\n } else {\n break;\n }\n }\n\n return right <= left;\n};\n"
1317
}
1418
]
1519
}

0 commit comments

Comments
 (0)