Skip to content

Commit af4c62a

Browse files
author
robot
committed
feat: 更新题库,代码模板
1 parent cfbf602 commit af4c62a

File tree

3 files changed

+94
-52
lines changed

3 files changed

+94
-52
lines changed

src/codeTemplates/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import preSum from "./preSum";
1010
import handWriting from "./hand-writing";
1111
import rotate from "./rotate";
1212
import grapth from "./grapth";
13+
import prime from './prime'
1314
export default [
1415
preSum,
1516
grapth,
@@ -23,4 +24,5 @@ export default [
2324
segmemntTree,
2425
handWriting,
2526
rotate,
27+
prime
2628
];

src/codeTemplates/prime.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
const pyCode = `
2+
MAXN = int(1e5)
3+
flag = [True] * (MAXN + 10) # if flag[i] is true, then i is prime
4+
flag[0], flag[1] = False, False
5+
for i in range(2, int(MAXN ** 0.5) + 1):
6+
if flag[i]:
7+
for j in range(i * 2, MAXN + 1, i):
8+
flag[j] = False
9+
`
10+
11+
module.exports = {
12+
title: "求质数",
13+
// logo: require("../imgs/preSum.svg"),
14+
list: [
15+
{
16+
text: "质数筛选法",
17+
problems: [
18+
{
19+
title: "2867. 统计树中的合法路径数目",
20+
id: "count-valid-paths-in-a-tree",
21+
},
22+
],
23+
codes: [
24+
{
25+
language: "py",
26+
text: pyCode,
27+
},
28+
],
29+
},
30+
]
31+
};

src/db/root.db.js

Lines changed: 61 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -2565,60 +2565,12 @@
25652565
"sort-colors":{
25662566
"id": "75",
25672567
"name": "sort-colors",
2568-
"pre": [
2569-
{
2570-
"text": "荷兰国旗问题",
2571-
"link": "https://en.wikipedia.org/wiki/Dutch_national_flag_problem",
2572-
"color": "purple"
2573-
},
2574-
{
2575-
"text": "排序",
2576-
"link": null,
2577-
"color": "purple"
2578-
}
2579-
],
2580-
"keyPoints": [
2581-
{
2582-
"text": "荷兰国旗问题",
2583-
"link": null,
2584-
"color": "blue"
2585-
},
2586-
{
2587-
"text": "countingsort",
2588-
"link": null,
2589-
"color": "blue"
2590-
}
2591-
],
2592-
"companies": [
2593-
{
2594-
"name": "阿里巴巴"
2595-
},
2596-
{
2597-
"name": "腾讯"
2598-
},
2599-
{
2600-
"name": "百度"
2601-
},
2602-
{
2603-
"name": "字节跳动"
2604-
}
2605-
],
2568+
"pre": [],
2569+
"keyPoints": [],
2570+
"companies": [],
26062571
"giteeSolution": "https://gitee.com/golong/leetcode/blob/master/problems/75.sort-colors.md",
26072572
"solution": "https://github.com/azl397985856/leetcode/blob/master/problems/75.sort-colors.md",
2608-
"code": [
2609-
{
2610-
"language": "cpp",
2611-
"text": "\nclass Solution {\npublic:\n void sortColors(vector<int>& nums) {\n int r = 0, g = 0, b = 0;\n for (int n : nums) {\n if (n == 0) {\n nums[b++] = 2;\n nums[g++] = 1;\n nums[r++] = 0;\n } else if (n == 1) {\n nums[b++] = 2;\n nums[g++] = 1;\n } else nums[b++] = 2;\n }\n }\n};\n"
2612-
},
2613-
{
2614-
"language": "py",
2615-
"text": "\nclass Solution:\n def sortColors(self, strs):\n # p0 是右边界\n # p1 是右边界\n # p2 是左边界\n # p1 超过 p2 结束\n p0, p1, p2 = 0, 0, len(strs) - 1\n\n while p1 <= p2:\n if strs[p1] == 'blue':\n strs[p2], strs[p1] = strs[p1], strs[p2]\n p2 -= 1\n elif strs[p1] == 'red':\n strs[p0], strs[p1] = strs[p1], strs[p0]\n p0 += 1\n p1 += 1 # p0 一定不是 blue,因此 p1 += 1\n else: # p1 === 'green'\n p1 += 1\n return strs\n"
2616-
},
2617-
{
2618-
"language": "py",
2619-
"text": "\nclass Solution:\n def partition(self, head: ListNode, x: int) -> ListNode:\n l1 = cur = head\n while cur:\n if cur.val < x:\n cur.val, l1.val = l1.val, cur.val\n l1 = l1.next\n cur = cur.next\n return head\n"
2620-
}
2621-
]
2573+
"code": []
26222574
},
26232575
"subsets":{
26242576
"id": "78",
@@ -13426,6 +13378,38 @@
1342613378
}
1342713379
]
1342813380
},
13381+
"maximum-score-of-a-good-subarray":{
13382+
"id": "1793",
13383+
"name": "maximum-score-of-a-good-subarray",
13384+
"pre": [
13385+
{
13386+
"text": "单调栈",
13387+
"link": null,
13388+
"color": "purple"
13389+
}
13390+
],
13391+
"keyPoints": [
13392+
{
13393+
"text": "贡献法",
13394+
"link": null,
13395+
"color": "blue"
13396+
},
13397+
{
13398+
"text": "单调栈",
13399+
"link": null,
13400+
"color": "blue"
13401+
}
13402+
],
13403+
"companies": [],
13404+
"giteeSolution": "https://gitee.com/golong/leetcode/blob/master/problems/1793.maximum-score-of-a-good-subarray.md",
13405+
"solution": "https://github.com/azl397985856/leetcode/blob/master/problems/1793.maximum-score-of-a-good-subarray.md",
13406+
"code": [
13407+
{
13408+
"language": "py",
13409+
"text": "\nclass Solution:\n def maximumScore(self, nums: List[int], k: int) -> int:\n # 单调栈求出 nums[i] 的下一个更小的下标 j\n st = []\n ans = 0\n nums += [0]\n for i in range(len(nums)):\n while st and nums[st[-1]] > nums[i]:\n # 含义:st[-1] 的下一个更小的是 i\n left = st[-2] if len(st) > 1 else -1 # 注意这里是 -2,因为 st[-1] 是当前元素, 我们要在当前元素的左边记录找。也可以先 st.pop() 后在 st[-1]\n if left < k < i: # 注意由于 left 和 i 我们都无法取到(开区间),因此这里不能有等号\n ans = max(ans, (i - left - 1) * nums[st[-1]])\n st.pop()\n st.append(i)\n return ans\n"
13410+
}
13411+
]
13412+
},
1342913413
"single-threaded-cpu":{
1343013414
"id": "1834",
1343113415
"name": "single-threaded-cpu",
@@ -14102,6 +14086,31 @@
1410214086
}
1410314087
]
1410414088
},
14089+
"minimum-absolute-difference-between-elements-with-constraint":{
14090+
"id": "2817",
14091+
"name": "minimum-absolute-difference-between-elements-with-constraint",
14092+
"pre": [
14093+
{
14094+
"text": "二分查找",
14095+
"link": null,
14096+
"color": "magenta"
14097+
}
14098+
],
14099+
"keyPoints": [],
14100+
"companies": [],
14101+
"giteeSolution": "https://gitee.com/golong/leetcode/blob/master/problems/2817.minimum-absolute-difference-between-elements-with-constraint.md",
14102+
"solution": "https://github.com/azl397985856/leetcode/blob/master/problems/2817.minimum-absolute-difference-between-elements-with-constraint.md",
14103+
"code": [
14104+
{
14105+
"language": "py",
14106+
"text": "\nclass Solution:\n def minAbsoluteDifference(self, nums: List[int], x: int) -> int:\n n = len(nums)\n minDiff = float('inf')\n\n for i in range(n):\n for j in range(i + x, n):\n absDiff = abs(nums[i] - nums[j])\n if absDiff < minDiff:\n minDiff = absDiff\n\n return minDiff\n\n"
14107+
},
14108+
{
14109+
"language": "py",
14110+
"text": "\nfrom sortedcontainers import SortedList\n\nclass Solution:\n def minAbsoluteDifference(self, nums: List[int], x: int) -> int:\n n = len(nums)\n\n # 初始化答案为无穷大\n res = float('inf') \n\n # 维护前面元素的有序序列\n ls = SortedList() \n\n for i in range(n - x):\n\n # 将nums[i]加入有序序列ls,SortedList保证插入后仍然有序\n v = nums[i]\n ls.add(v) \n\n # 使用二分查找寻找前面序列中最后一个<=nums[i+x]的元素\n v = nums[i + x]\n idx = ls.bisect_right(v)\n\n # 使用和nums[i+x]最接近的元素更新答案,将答案更新为当前答案和新差值中的较小值\n res = min(res, abs(v - ls[idx - 1]))\n\n # 如果存在更接近的元素,也尝试更新答案\n if idx < len(ls): \n res = min(res, abs(ls[idx] - v))\n\n return res\n"
14111+
}
14112+
]
14113+
},
1410514114
"selling-pieces-of-wood":{
1410614115
"id": "5254",
1410714116
"name": "selling-pieces-of-wood",

0 commit comments

Comments
 (0)