Skip to content

feat: i18n update #39

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Nov 22, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 18 additions & 6 deletions src/codeTemplates/backtrack.js
Original file line number Diff line number Diff line change
@@ -65,18 +65,30 @@ module.exports = () => ({
text: `
const visited = {}
function backtrack(i) {
if (满足特定条件){
// 如果满足条件
if (Meet certain conditions) {
// 返回结果 or 退出搜索空间
// return result or exit search space
}
visited[i] = true // 将当前状态标为已搜索
dosomething(i) // 对i做一些操作
for (根据i能到达的下个状态j) {
if (!visited[j]) { // 如果状态j没有被搜索过
// 将当前状态标为已搜索
// mark the current state as searched
visited[i] = true
// 对i做一些操作
// do something with i
dosomething(i)
// for (根据i能到达的下个状态j) {
for (The next state j that can be reached based on i.) {
// 如果状态j没有被搜索过
// if state j has not been searched
if (!visited[j]) {
dfs(j)
}
}
undo(i) // 恢复i
// 恢复i
// restore i
undo(i)
}
backtrack(0)
`,
11 changes: 10 additions & 1 deletion src/codeTemplates/bfs.js
Original file line number Diff line number Diff line change
@@ -32,15 +32,20 @@ export default () => ({
class Solution:
def bfs(k):
# 使用双端队列,而不是数组。因为数组从头部删除元素的时间复杂度为 N,双端队列的底层实现其实是链表。
# 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.
queue = collections.deque([root])
# 记录层数
# Record the level or depth.
steps = 0
# 需要返回的节点
# The nodes to return.
ans = []
# 队列不空,生命不止!
# While the queue is not empty, we continue.
while queue:
size = len(queue)
# 遍历当前层的所有节点
# Traverse all the nodes in the current level.
for _ in range(size):
node = queue.popleft()
if (steps == k) ans.append(node)
@@ -49,6 +54,7 @@ export default () => ({
if node.left:
queue.append(node.left)
# 遍历完当前层所有的节点后 steps + 1
# After traversing all the nodes in the current level, steps + 1.
steps += 1
return ans
`,
@@ -72,12 +78,15 @@ export default () => ({
class Solution:
def bfs(k):
# 使用双端队列,而不是数组。因为数组从头部删除元素的时间复杂度为 N,双端队列的底层实现其实是链表。
# 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.
queue = collections.deque([root])
# 队列不空,生命不止!
# While the queue is not empty, we continue.
while queue:
node = queue.popleft()
# 由于没有记录 steps,因此我们肯定是不需要根据层的信息去判断的。否则就用带层的模板了。
if (node 是我们要找到的) return node
# 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.
if (node is what we are looking for) return node
if node.right:
queue.append(node.right)
if node.left:
50 changes: 48 additions & 2 deletions src/codeTemplates/binarySearch.js
Original file line number Diff line number Diff line change
@@ -20,6 +20,7 @@ module.exports = () => ({
text: `
public int binarySearch(int[] nums, int target) {
// 左右都闭合的区间 [l, r]
// A closed interval [l, r] with both ends inclusive.
int left = 0;
int right = nums.length - 1;
@@ -29,9 +30,11 @@ module.exports = () => ({
return mid;
if (nums[mid] < target)
// 搜索区间变为 [mid+1, right]
// Narrow down the search range to [mid+1, right]
left = mid + 1;
if (nums[mid] > target)
// 搜索区间变为 [left, mid - 1]
// Narrow down the search range to [left, mid - 1]
right = mid - 1;
}
return -1;
@@ -42,13 +45,16 @@ module.exports = () => ({
text: `
def binarySearch(nums, target):
# 左右都闭合的区间 [l, r]
# A closed interval [l, r] with both ends inclusive.
l, r = 0, len(nums) - 1
while l <= r:
mid = (left + right) >> 1
if nums[mid] == target: return mid
# 搜索区间变为 [mid+1, right]
# Narrow down the search range to [mid+1, right]
if nums[mid] < target: l = mid + 1
# 搜索区间变为 [left, mid - 1]
# Narrow down the search range to [left, mid - 1]
if nums[mid] > target: r = mid - 1
return -1`,
},
@@ -63,9 +69,11 @@ module.exports = () => ({
if (nums[mid] == target) return mid;
if (nums[mid] < target)
// 搜索区间变为 [mid+1, right]
// Narrow down the search range to [mid+1, right]
left = mid + 1;
if (nums[mid] > target)
// 搜索区间变为 [left, mid - 1]
// Narrow down the search range to [left, mid - 1]
right = mid - 1;
}
return -1;
@@ -83,9 +91,11 @@ module.exports = () => ({
int mid = left + ((right - left) >> 1);
if(nums[mid] == target){ return mid; }
// 搜索区间变为 [mid+1, right]
// Narrow down the search range to [mid+1, right]
else if(nums[mid] < target)
left = mid + 1;
// 搜索区间变为 [left, mid - 1]
// Narrow down the search range to [left, mid - 1]
else
right = mid - 1;
}
@@ -116,20 +126,24 @@ module.exports = () => ({
text: `
public int binarySearchLeft(int[] nums, int target) {
// 搜索区间为 [left, right]
// A closed interval [left, right] with both ends inclusive.
int left = 0;
int right = nums.length - 1;
while (left <= right) {
int mid = left + (right - left) / 2;
if (nums[mid] < target) {
// 搜索区间变为 [mid+1, right]
// Narrow down the search range to [mid+1, right]
left = mid + 1;
}
if (nums[mid] >= target) {
// 搜索区间变为 [left, mid-1]
// Narrow down the search range to [left, mid-1]
right = mid - 1;
}
}
// 检查是否越界
// Check if it is out of bounds
if (left >= nums.length || nums[left] != target)
return -1;
return left;
@@ -140,12 +154,15 @@ module.exports = () => ({
text: `
def binarySearchLeft(nums, target):
# 左右都闭合的区间 [l, r]
# A closed interval [l, r] with both ends inclusive.
l, r = 0, len(nums) - 1
while l <= r:
mid = (l + r) >> 1
# 搜索区间变为 [mid+1, right]
# Narrow down the search range to [mid+1, right]
if nums[mid] < target: l = mid + 1
# 搜索区间变为 [left, mid - 1]
# Narrow down the search range to [left, mid - 1]
if nums[mid] >= target: r = mid - 1
if l >= len(nums) or nums[l] != target: return -1
return l`,
@@ -160,12 +177,15 @@ module.exports = () => ({
const mid = Math.floor(left + (right - left) / 2);
if (nums[mid] < target)
// 搜索区间变为 [mid+1, right]
// Narrow down the search range to [mid+1, right]
left = mid + 1;
if (nums[mid] >= target)
// 搜索区间变为 [left, mid - 1]
// Narrow down the search range to [left, mid - 1]
right = mid - 1;
}
// 检查是否越界
// Check if it is out of bounds
if (left >= nums.length || nums[left] != target) return -1;
return left;
}`,
@@ -175,23 +195,28 @@ module.exports = () => ({
text: `
int binarySearchLeft(vector<int>& nums, int target) {
// 搜索区间为 [left, right]
// The search interval is [left, right].
int left = 0, right = nums.size() - 1;
while (left <= right) {
int mid = left + ((right - left) >> 1);
if (nums[mid] == target) {
// 收缩右边界
// Narrow down the right boundary
right = mid - 1;
}
if (nums[mid] < target) {
// 搜索区间变为 [mid+1, right]
// Narrow down the search range to [mid+1, right]
left = mid + 1;
}
if (nums[mid] > target) {
// 搜索区间变为 [left, mid-1]
// Narrow down the search range to [left, mid-1]
right = mid - 1;
}
}
// 检查是否越界
// Check if it is out of bounds
if (left >= nums.size() || nums[left] != target)
return -1;
return left;
@@ -221,20 +246,24 @@ module.exports = () => ({
text: `
public int binarySearchRight(int[] nums, int target) {
// 搜索区间为 [left, right]
// A closed interval [left, right] with both ends inclusive.
int left = 0
int right = nums.length - 1;
while (left <= right) {
int mid = left + (right - left) / 2;
if (nums[mid] <= target) {
// 搜索区间变为 [mid+1, right]
// Narrow down the search range to [mid+1, right]
left = mid + 1;
}
if (nums[mid] > target) {
// 搜索区间变为 [left, mid-1]
// Narrow down the search range to [left, mid-1]
right = mid - 1;
}
}
// 检查是否越界
// Check if it is out of bounds
if (right < 0 || nums[right] != target)
return -1;
return right;
@@ -245,12 +274,15 @@ module.exports = () => ({
text: `
def binarySearchRight(nums, target):
# 左右都闭合的区间 [l, r]
# A closed interval [l, r] with both ends inclusive.
l, r = 0, len(nums) - 1
while l <= r:
mid = (l + r) >> 1
# 搜索区间变为 [mid+1, right]
# Narrow down the search range to [mid+1, right]
if nums[mid] <= target: l = mid + 1
# 搜索区间变为 [left, mid - 1]
# Narrow down the search range to [left, mid - 1]
if nums[mid] > target: r = mid - 1
if r < 0 or nums[r] != target: return -1
return r`,
@@ -265,12 +297,15 @@ module.exports = () => ({
const mid = Math.floor(left + (right - left) / 2);
if (nums[mid] <= target)
// 搜索区间变为 [mid+1, right]
// Narrow down the search range to [mid+1, right]
left = mid + 1;
if (nums[mid] > target)
// 搜索区间变为 [left, mid - 1]
// Narrow down the search range to [left, mid - 1]
right = mid - 1;
}
// 检查是否越界
// Check if it is out of bounds
if (right < 0 || nums[right] != target) return -1;
return right;
}`,
@@ -280,23 +315,28 @@ module.exports = () => ({
text: `
int binarySearchRight(vector<int>& nums, int target) {
// 搜索区间为 [left, right]
// The search interval is [left, right].
int left = 0, right = nums.size() - 1;
while (left <= right) {
int mid = left + ((right - left) >> 1);
if (nums[mid] == target) {
// 收缩左边界
// Narrow down the left boundary
left = mid + 1;
}
if (nums[mid] < target) {
// 搜索区间变为 [mid+1, right]
// Narrow down the search range to [mid+1, right]
left = mid + 1;
}
if (nums[mid] > target) {
// 搜索区间变为 [left, mid-1]
// Narrow down the search range to [left, mid-1]
right = mid - 1;
}
}
// 检查是否越界
// Check if it is out of bounds
if (right < 0 || nums[right] != target)
return -1;
return right;
@@ -315,8 +355,10 @@ module.exports = () => ({
text: `
def bisect_left(nums, x):
# 内置 api
# built-in API
bisect.bisect_left(nums, x)
# 手写
# Manually write
l, r = 0, len(A) - 1
while l <= r:
mid = (l + r) // 2
@@ -330,13 +372,14 @@ module.exports = () => ({
text: `
/**
* @author suukii
* @description 寻找最左插入位置
* @description ${t("Locale.codeTemplate.binarySearch.item4")}
* @param {number[]} nums
* @param {number} x
* @returns {number}
*/
function searchInsertLeft(nums, x) {
// 题意转换一下,其实就是寻找第一个“大于等于” x 的数字,返回它的下标
// 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
let left = 0;
let right = nums.length - 1;
@@ -369,8 +412,10 @@ function searchInsertLeft(nums, x) {
text: `
def bisect_right(nums, x):
# 内置 api
# built-in API
bisect.bisect_right(nums, x)
# 手写
# Manually write
l, r = 0, len(A) - 1
while l <= r:
mid = (l + r) // 2
@@ -383,13 +428,14 @@ function searchInsertLeft(nums, x) {
language: "JS",
text: `
/**@author suukii
* @description 寻找最右插入位置
* @description ${t("Locale.codeTemplate.binarySearch.item5")}
* @param {number[]} nums
* @param {number} x
* @returns {number}
*/
function searchInsertRight(nums, x) {
// 题意转换一下,其实就是寻找第一个“大于” x 的数字,返回它的下标
// Change the meaning of the question, in fact, it is to find the first number "greater than" x and return its subscript
let left = 0;
let right = nums.length - 1;
Loading