Skip to content

Commit 1e7ea80

Browse files
authored
Merge pull request #39 from rennzhang/feat-lang
feat: i18n update
2 parents 6a64cb9 + 00f3710 commit 1e7ea80

File tree

13 files changed

+364
-76
lines changed

13 files changed

+364
-76
lines changed

‎src/codeTemplates/backtrack.js

+18-6
Original file line numberDiff line numberDiff line change
@@ -65,18 +65,30 @@ module.exports = () => ({
6565
text: `
6666
const visited = {}
6767
function backtrack(i) {
68-
if (满足特定条件){
68+
// 如果满足条件
69+
if (Meet certain conditions) {
6970
// 返回结果 or 退出搜索空间
71+
// return result or exit search space
7072
}
7173
72-
visited[i] = true // 将当前状态标为已搜索
73-
dosomething(i) // 对i做一些操作
74-
for (根据i能到达的下个状态j) {
75-
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]) {
7686
dfs(j)
7787
}
7888
}
79-
undo(i) // 恢复i
89+
// 恢复i
90+
// restore i
91+
undo(i)
8092
}
8193
backtrack(0)
8294
`,

‎src/codeTemplates/bfs.js

+10-1
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,20 @@ export default () => ({
3232
class Solution:
3333
def bfs(k):
3434
# 使用双端队列,而不是数组。因为数组从头部删除元素的时间复杂度为 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.
3536
queue = collections.deque([root])
3637
# 记录层数
38+
# Record the level or depth.
3739
steps = 0
3840
# 需要返回的节点
41+
# The nodes to return.
3942
ans = []
4043
# 队列不空,生命不止!
44+
# While the queue is not empty, we continue.
4145
while queue:
4246
size = len(queue)
4347
# 遍历当前层的所有节点
48+
# Traverse all the nodes in the current level.
4449
for _ in range(size):
4550
node = queue.popleft()
4651
if (steps == k) ans.append(node)
@@ -49,6 +54,7 @@ export default () => ({
4954
if node.left:
5055
queue.append(node.left)
5156
# 遍历完当前层所有的节点后 steps + 1
57+
# After traversing all the nodes in the current level, steps + 1.
5258
steps += 1
5359
return ans
5460
`,
@@ -72,12 +78,15 @@ export default () => ({
7278
class Solution:
7379
def bfs(k):
7480
# 使用双端队列,而不是数组。因为数组从头部删除元素的时间复杂度为 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.
7582
queue = collections.deque([root])
7683
# 队列不空,生命不止!
84+
# While the queue is not empty, we continue.
7785
while queue:
7886
node = queue.popleft()
7987
# 由于没有记录 steps,因此我们肯定是不需要根据层的信息去判断的。否则就用带层的模板了。
80-
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
8190
if node.right:
8291
queue.append(node.right)
8392
if node.left:

‎src/codeTemplates/binarySearch.js

+48-2
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ module.exports = () => ({
2020
text: `
2121
public int binarySearch(int[] nums, int target) {
2222
// 左右都闭合的区间 [l, r]
23+
// A closed interval [l, r] with both ends inclusive.
2324
int left = 0;
2425
int right = nums.length - 1;
2526
@@ -29,9 +30,11 @@ module.exports = () => ({
2930
return mid;
3031
if (nums[mid] < target)
3132
// 搜索区间变为 [mid+1, right]
33+
// Narrow down the search range to [mid+1, right]
3234
left = mid + 1;
3335
if (nums[mid] > target)
3436
// 搜索区间变为 [left, mid - 1]
37+
// Narrow down the search range to [left, mid - 1]
3538
right = mid - 1;
3639
}
3740
return -1;
@@ -42,13 +45,16 @@ module.exports = () => ({
4245
text: `
4346
def binarySearch(nums, target):
4447
# 左右都闭合的区间 [l, r]
48+
# A closed interval [l, r] with both ends inclusive.
4549
l, r = 0, len(nums) - 1
4650
while l <= r:
4751
mid = (left + right) >> 1
4852
if nums[mid] == target: return mid
4953
# 搜索区间变为 [mid+1, right]
54+
# Narrow down the search range to [mid+1, right]
5055
if nums[mid] < target: l = mid + 1
5156
# 搜索区间变为 [left, mid - 1]
57+
# Narrow down the search range to [left, mid - 1]
5258
if nums[mid] > target: r = mid - 1
5359
return -1`,
5460
},
@@ -63,9 +69,11 @@ module.exports = () => ({
6369
if (nums[mid] == target) return mid;
6470
if (nums[mid] < target)
6571
// 搜索区间变为 [mid+1, right]
72+
// Narrow down the search range to [mid+1, right]
6673
left = mid + 1;
6774
if (nums[mid] > target)
6875
// 搜索区间变为 [left, mid - 1]
76+
// Narrow down the search range to [left, mid - 1]
6977
right = mid - 1;
7078
}
7179
return -1;
@@ -83,9 +91,11 @@ module.exports = () => ({
8391
int mid = left + ((right - left) >> 1);
8492
if(nums[mid] == target){ return mid; }
8593
// 搜索区间变为 [mid+1, right]
94+
// Narrow down the search range to [mid+1, right]
8695
else if(nums[mid] < target)
8796
left = mid + 1;
8897
// 搜索区间变为 [left, mid - 1]
98+
// Narrow down the search range to [left, mid - 1]
8999
else
90100
right = mid - 1;
91101
}
@@ -116,20 +126,24 @@ module.exports = () => ({
116126
text: `
117127
public int binarySearchLeft(int[] nums, int target) {
118128
// 搜索区间为 [left, right]
129+
// A closed interval [left, right] with both ends inclusive.
119130
int left = 0;
120131
int right = nums.length - 1;
121132
while (left <= right) {
122133
int mid = left + (right - left) / 2;
123134
if (nums[mid] < target) {
124135
// 搜索区间变为 [mid+1, right]
136+
// Narrow down the search range to [mid+1, right]
125137
left = mid + 1;
126138
}
127139
if (nums[mid] >= target) {
128140
// 搜索区间变为 [left, mid-1]
141+
// Narrow down the search range to [left, mid-1]
129142
right = mid - 1;
130143
}
131144
}
132145
// 检查是否越界
146+
// Check if it is out of bounds
133147
if (left >= nums.length || nums[left] != target)
134148
return -1;
135149
return left;
@@ -140,12 +154,15 @@ module.exports = () => ({
140154
text: `
141155
def binarySearchLeft(nums, target):
142156
# 左右都闭合的区间 [l, r]
157+
# A closed interval [l, r] with both ends inclusive.
143158
l, r = 0, len(nums) - 1
144159
while l <= r:
145160
mid = (l + r) >> 1
146161
# 搜索区间变为 [mid+1, right]
162+
# Narrow down the search range to [mid+1, right]
147163
if nums[mid] < target: l = mid + 1
148164
# 搜索区间变为 [left, mid - 1]
165+
# Narrow down the search range to [left, mid - 1]
149166
if nums[mid] >= target: r = mid - 1
150167
if l >= len(nums) or nums[l] != target: return -1
151168
return l`,
@@ -160,12 +177,15 @@ module.exports = () => ({
160177
const mid = Math.floor(left + (right - left) / 2);
161178
if (nums[mid] < target)
162179
// 搜索区间变为 [mid+1, right]
180+
// Narrow down the search range to [mid+1, right]
163181
left = mid + 1;
164182
if (nums[mid] >= target)
165183
// 搜索区间变为 [left, mid - 1]
184+
// Narrow down the search range to [left, mid - 1]
166185
right = mid - 1;
167186
}
168187
// 检查是否越界
188+
// Check if it is out of bounds
169189
if (left >= nums.length || nums[left] != target) return -1;
170190
return left;
171191
}`,
@@ -175,23 +195,28 @@ module.exports = () => ({
175195
text: `
176196
int binarySearchLeft(vector<int>& nums, int target) {
177197
// 搜索区间为 [left, right]
198+
// The search interval is [left, right].
178199
int left = 0, right = nums.size() - 1;
179200
while (left <= right) {
180201
int mid = left + ((right - left) >> 1);
181202
if (nums[mid] == target) {
182203
// 收缩右边界
204+
// Narrow down the right boundary
183205
right = mid - 1;
184206
}
185207
if (nums[mid] < target) {
186208
// 搜索区间变为 [mid+1, right]
209+
// Narrow down the search range to [mid+1, right]
187210
left = mid + 1;
188211
}
189212
if (nums[mid] > target) {
190213
// 搜索区间变为 [left, mid-1]
214+
// Narrow down the search range to [left, mid-1]
191215
right = mid - 1;
192216
}
193217
}
194218
// 检查是否越界
219+
// Check if it is out of bounds
195220
if (left >= nums.size() || nums[left] != target)
196221
return -1;
197222
return left;
@@ -221,20 +246,24 @@ module.exports = () => ({
221246
text: `
222247
public int binarySearchRight(int[] nums, int target) {
223248
// 搜索区间为 [left, right]
249+
// A closed interval [left, right] with both ends inclusive.
224250
int left = 0
225251
int right = nums.length - 1;
226252
while (left <= right) {
227253
int mid = left + (right - left) / 2;
228254
if (nums[mid] <= target) {
229255
// 搜索区间变为 [mid+1, right]
256+
// Narrow down the search range to [mid+1, right]
230257
left = mid + 1;
231258
}
232259
if (nums[mid] > target) {
233260
// 搜索区间变为 [left, mid-1]
261+
// Narrow down the search range to [left, mid-1]
234262
right = mid - 1;
235263
}
236264
}
237265
// 检查是否越界
266+
// Check if it is out of bounds
238267
if (right < 0 || nums[right] != target)
239268
return -1;
240269
return right;
@@ -245,12 +274,15 @@ module.exports = () => ({
245274
text: `
246275
def binarySearchRight(nums, target):
247276
# 左右都闭合的区间 [l, r]
277+
# A closed interval [l, r] with both ends inclusive.
248278
l, r = 0, len(nums) - 1
249279
while l <= r:
250280
mid = (l + r) >> 1
251281
# 搜索区间变为 [mid+1, right]
282+
# Narrow down the search range to [mid+1, right]
252283
if nums[mid] <= target: l = mid + 1
253284
# 搜索区间变为 [left, mid - 1]
285+
# Narrow down the search range to [left, mid - 1]
254286
if nums[mid] > target: r = mid - 1
255287
if r < 0 or nums[r] != target: return -1
256288
return r`,
@@ -265,12 +297,15 @@ module.exports = () => ({
265297
const mid = Math.floor(left + (right - left) / 2);
266298
if (nums[mid] <= target)
267299
// 搜索区间变为 [mid+1, right]
300+
// Narrow down the search range to [mid+1, right]
268301
left = mid + 1;
269302
if (nums[mid] > target)
270303
// 搜索区间变为 [left, mid - 1]
304+
// Narrow down the search range to [left, mid - 1]
271305
right = mid - 1;
272306
}
273307
// 检查是否越界
308+
// Check if it is out of bounds
274309
if (right < 0 || nums[right] != target) return -1;
275310
return right;
276311
}`,
@@ -280,23 +315,28 @@ module.exports = () => ({
280315
text: `
281316
int binarySearchRight(vector<int>& nums, int target) {
282317
// 搜索区间为 [left, right]
318+
// The search interval is [left, right].
283319
int left = 0, right = nums.size() - 1;
284320
while (left <= right) {
285321
int mid = left + ((right - left) >> 1);
286322
if (nums[mid] == target) {
287323
// 收缩左边界
324+
// Narrow down the left boundary
288325
left = mid + 1;
289326
}
290327
if (nums[mid] < target) {
291328
// 搜索区间变为 [mid+1, right]
329+
// Narrow down the search range to [mid+1, right]
292330
left = mid + 1;
293331
}
294332
if (nums[mid] > target) {
295333
// 搜索区间变为 [left, mid-1]
334+
// Narrow down the search range to [left, mid-1]
296335
right = mid - 1;
297336
}
298337
}
299338
// 检查是否越界
339+
// Check if it is out of bounds
300340
if (right < 0 || nums[right] != target)
301341
return -1;
302342
return right;
@@ -315,8 +355,10 @@ module.exports = () => ({
315355
text: `
316356
def bisect_left(nums, x):
317357
# 内置 api
358+
# built-in API
318359
bisect.bisect_left(nums, x)
319360
# 手写
361+
# Manually write
320362
l, r = 0, len(A) - 1
321363
while l <= r:
322364
mid = (l + r) // 2
@@ -330,13 +372,14 @@ module.exports = () => ({
330372
text: `
331373
/**
332374
* @author suukii
333-
* @description 寻找最左插入位置
375+
* @description ${t("Locale.codeTemplate.binarySearch.item4")}
334376
* @param {number[]} nums
335377
* @param {number} x
336378
* @returns {number}
337379
*/
338380
function searchInsertLeft(nums, x) {
339381
// 题意转换���下,其实就是寻找第一个“大于等于” x 的数字,返回它的下标
382+
// Change the meaning of the question, in fact, it is to find the first number "greater than or equal to" x and return its subscript
340383
let left = 0;
341384
let right = nums.length - 1;
342385
@@ -369,8 +412,10 @@ function searchInsertLeft(nums, x) {
369412
text: `
370413
def bisect_right(nums, x):
371414
# 内置 api
415+
# built-in API
372416
bisect.bisect_right(nums, x)
373417
# 手写
418+
# Manually write
374419
l, r = 0, len(A) - 1
375420
while l <= r:
376421
mid = (l + r) // 2
@@ -383,13 +428,14 @@ function searchInsertLeft(nums, x) {
383428
language: "JS",
384429
text: `
385430
/**@author suukii
386-
* @description 寻找最右插入位置
431+
* @description ${t("Locale.codeTemplate.binarySearch.item5")}
387432
* @param {number[]} nums
388433
* @param {number} x
389434
* @returns {number}
390435
*/
391436
function searchInsertRight(nums, x) {
392437
// 题意转换一下,其实就是寻找第一个“大于” x 的数字,返回它的下标
438+
// Change the meaning of the question, in fact, it is to find the first number "greater than" x and return its subscript
393439
let left = 0;
394440
let right = nums.length - 1;
395441

0 commit comments

Comments
 (0)