Skip to content

Commit 941528b

Browse files
author
robot
committed
2 parents 8fabe29 + 1e7ea80 commit 941528b

File tree

18 files changed

+1336
-325
lines changed

18 files changed

+1336
-325
lines changed

src/codeTemplates/backtrack.js

Lines changed: 49 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,62 @@
1+
const { t } = require("../locales");
12
module.exports = () => ({
23
logo: require("../imgs/backtrack.svg"),
3-
title: "回溯",
4+
// title: "回溯",
5+
title: t("Locale.codeTemplate.backtrack.title"),
46
list: [
57
{
6-
text: "标准回溯(伪代码)",
8+
// text: "标准回溯(伪代码)",
9+
text: t("Locale.codeTemplate.backtrack.item1"),
710
problems: [
811
{
912
id: "combination-sum",
10-
title: "39. 组合总和",
13+
// title: "39. 组合总和",
14+
title: t("Locale.problem.39"),
1115
},
1216
{
13-
title: "40. 组合总和 II",
17+
// title: "40. 组合总和 II",
18+
title: t("Locale.problem.40"),
1419
id: "combination-sum-ii",
1520
},
1621
{
17-
title: "46. 全排列",
22+
// title: "46. 全排列",
23+
title: t("Locale.problem.46"),
1824
id: "permutations",
1925
},
2026
{
21-
title: "47. 全排列 II",
27+
// title: "47. 全排列 II",
28+
title: t("Locale.problem.47"),
2229
id: "permutations-ii",
2330
},
2431
{
2532
id: "N-Queens-II",
26-
title: "52. N 皇后 II",
33+
// title: "52. N 皇后 II",
34+
title: t("Locale.problem.52"),
2735
},
2836
{
2937
id: "subsets",
30-
title: "78. 子集",
38+
// title: "78. 子集",
39+
title: t("Locale.problem.78"),
3140
},
3241
{
3342
id: "subsets-ii",
34-
title: "90. 子集 II",
43+
// title: "90. 子集 II",
44+
title: t("Locale.problem.90"),
3545
},
3646
{
3747
id: "path-sum-ii",
38-
title: "113. 路径总和 II",
48+
// title: "113. 路径总和 II",
49+
title: t("Locale.problem.113"),
3950
},
4051
{
4152
id: "palindrome-partitioning",
42-
title: "131. 分割回文串",
53+
// title: "131. 分割回文串",
54+
title: t("Locale.problem.131"),
4355
},
4456
{
4557
id: "maximum-score-words-formed-by-letters",
46-
title: "1255. 得分最高的单词集合",
58+
// title: "1255. 得分最高的单词集合",
59+
title: t("Locale.problem.1255"),
4760
},
4861
],
4962
codes: [
@@ -52,34 +65,49 @@ module.exports = () => ({
5265
text: `
5366
const visited = {}
5467
function backtrack(i) {
55-
if (满足特定条件){
68+
// 如果满足条件
69+
if (Meet certain conditions) {
5670
// 返回结果 or 退出搜索空间
71+
// return result or exit search space
5772
}
5873
59-
visited[i] = true // 将当前状态标为已搜索
60-
dosomething(i) // 对i做一些操作
61-
for (根据i能到达的下个状态j) {
62-
if (!visited[j]) { // 如果状态j没有被搜索过
74+
// 将当前状态标为已搜索
75+
// mark the current state as searched
76+
visited[i] = true
77+
// 对i做一些操作
78+
// do something with i
79+
dosomething(i)
80+
81+
// for (根据i能到达的下个状态j) {
82+
for (The next state j that can be reached based on i.) {
83+
// 如果状态j没有被搜索过
84+
// if state j has not been searched
85+
if (!visited[j]) {
6386
dfs(j)
6487
}
6588
}
66-
undo(i) // 恢复i
89+
// 恢复i
90+
// restore i
91+
undo(i)
6792
}
6893
backtrack(0)
6994
`,
7095
},
7196
],
7297
},
7398
{
74-
text: "笛卡尔积优化",
99+
// text: "笛卡尔积优化",
100+
text: t("Locale.codeTemplate.backtrack.item2"),
75101
problems: [
76102
{
77103
id: "word-break-ii",
78-
title: "140. 单词拆分 II",
104+
// title: "140. 单词拆分 II",
105+
title: t("Locale.problem.140"),
79106
},
80107
{
81108
id: "ambiguous-coordinates",
82-
title: "816. 模糊坐标",
109+
// title: "816. 模糊坐标",
110+
title: t("Locale.problem.816"),
83111
},
84112
],
85113
codes: [

src/codeTemplates/bfs.js

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,27 @@
11
import bfsLogo from "../imgs/bfs.svg";
2+
const { t } = require("../locales");
23

34
export default () => ({
45
title: "BFS",
56
logo: bfsLogo,
67
list: [
78
{
8-
text: "带层信息",
9+
// text: "带层信息",
10+
text: t("Locale.codeTemplate.BFS.item1"),
911
problems: [
1012
{
11-
title: "513.找树左下角的值",
13+
// title: "513.找树左下角的值",
14+
title: t("Locale.problem.513"),
1215
id: "find-bottom-left-tree-value",
1316
},
1417
{
15-
title: "662. 二叉树最大宽度",
18+
// title: "662. 二叉树最大宽度",
19+
title: t("Locale.problem.662"),
1620
id: "maximum-width-of-binary-tree",
1721
},
1822
{
19-
title: "863. 二叉树中所有距离为 K 的结点",
23+
// title: "863. 二叉树中所有距离为 K 的结点",
24+
title: t("Locale.problem.863"),
2025
id: "all-nodes-distance-k-in-binary-tree",
2126
},
2227
],
@@ -27,15 +32,20 @@ export default () => ({
2732
class Solution:
2833
def bfs(k):
2934
# 使用双端队列,而不是数组。因为数组从头部删除元素的时间复杂度为 N,双端队列的底层实现其实是链表。
35+
# Utilize a double-ended queue instead of an array, as the time complexity for removing elements from the head of an array is O(N), whereas a double-ended queue, implemented as a linked list, offers a more efficient alternative.
3036
queue = collections.deque([root])
3137
# 记录层数
38+
# Record the level or depth.
3239
steps = 0
3340
# 需要返回的节点
41+
# The nodes to return.
3442
ans = []
3543
# 队列不空,生命不止!
44+
# While the queue is not empty, we continue.
3645
while queue:
3746
size = len(queue)
3847
# 遍历当前层的所有节点
48+
# Traverse all the nodes in the current level.
3949
for _ in range(size):
4050
node = queue.popleft()
4151
if (steps == k) ans.append(node)
@@ -44,17 +54,20 @@ export default () => ({
4454
if node.left:
4555
queue.append(node.left)
4656
# 遍历完当前层所有的节点后 steps + 1
57+
# After traversing all the nodes in the current level, steps + 1.
4758
steps += 1
4859
return ans
4960
`,
5061
},
5162
],
5263
},
5364
{
54-
text: "不带层信息",
65+
// text: "不带层信息",
66+
text: t("Locale.codeTemplate.BFS.item2"),
5567
problems: [
5668
{
57-
title: "116. 填充每个节点的下一个右侧节点指针",
69+
// title: "116. 填充每个节点的下一个右侧节点指针",
70+
title: t("Locale.problem.116"),
5871
id: "populating-next-right-pointers-in-each-node",
5972
},
6073
],
@@ -65,12 +78,15 @@ export default () => ({
6578
class Solution:
6679
def bfs(k):
6780
# 使用双端队列,而不是数组。因为数组从头部删除元素的时间复杂度为 N,双端队列的底层实现其实是链表。
81+
# Utilize a double-ended queue instead of an array, as the time complexity for removing elements from the head of an array is O(N), whereas a double-ended queue, implemented as a linked list, offers a more efficient alternative.
6882
queue = collections.deque([root])
6983
# 队列不空,生命不止!
84+
# While the queue is not empty, we continue.
7085
while queue:
7186
node = queue.popleft()
7287
# 由于没有记录 steps,因此我们肯定是不需要根据层的信息去判断的。否则就用带层的模板了。
73-
if (node 是我们要找到的) return node
88+
# Since we don't record steps, we don't need to judge based on the level information. Otherwise, we would use the template with level information.
89+
if (node is what we are looking for) return node
7490
if node.right:
7591
queue.append(node.right)
7692
if node.left:

0 commit comments

Comments
 (0)