-
Notifications
You must be signed in to change notification settings - Fork 4
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
任务分解 #1
Comments
领取:爬虫。 爬取 https://github.com/azl397985856/leetcode 下的题目信息,用作数据源。包括题目信息,tag信息,公司信息等 |
领取:整理20-40的题解格式 |
领取:整理41-60的题解格式 |
关于模板(C++/Python/Java/Js):
|
可视化调试 @MingkHe 出一个 RFC |
公司信息完善 @fly0o0 可以先去LeetCode 评论区搞一点。 |
ok |
二分法使用场景
查找一个数
Javapublic int binarySearch(int[] nums, int target) {
// 左右都闭合的区间 [l, r]
int left = 0;
int right = nums.length - 1;
while(left <= right) {
int mid = left + (right - left) / 2;
if(nums[mid] == target)
return mid;
if (nums[mid] < target)
// 搜索区间变为 [mid+1, right]
left = mid + 1;
if (nums[mid] > target)
// 搜索区间变为 [left, mid - 1]
right = mid - 1;
}
return -1;
} Pythondef binarySearch(nums, target):
# 左右都闭合的区间 [l, r]
l, r = 0, len(nums) - 1
while l <= r:
mid = (left + right) >> 1
if nums[mid] == target: return mid
# 搜索区间变为 [mid+1, right]
if nums[mid] < target: l = mid + 1
# 搜索区间变为 [left, mid - 1]
if nums[mid] > target: r = mid - 1
return -1 JavaScriptfunction binarySearch(nums, target) {
let left = 0;
let right = nums.length - 1;
while (left <= right) {
const mid = Math.floor(left + (right - left) / 2);
if (nums[mid] == target) return mid;
if (nums[mid] < target)
// 搜索区间变为 [mid+1, right]
left = mid + 1;
if (nums[mid] > target)
// 搜索区间变为 [left, mid - 1]
right = mid - 1;
}
return -1;
} C++暂时空缺,欢迎 PR 寻找最左边的满足条件的值
Javapublic int binarySearchLeft(int[] nums, int target) {
// 搜索区间为 [left, right]
int left = 0;
int right = nums.length - 1;
while (left <= right) {
int mid = left + (right - left) / 2;
if (nums[mid] < target) {
// 搜索区间变为 [mid+1, right]
left = mid + 1;
}
if (nums[mid] > target) {
// 搜索区间变为 [left, mid-1]
right = mid - 1;
}
if (nums[mid] == target) {
// 收缩右边界
right = mid - 1;
}
}
// 检查是否越界
if (left >= nums.length || nums[left] != target)
return -1;
return left;
} Pythondef binarySearchLeft(nums, target):
# 左右都闭合的区间 [l, r]
l, r = 0, len(nums) - 1
while l <= r:
mid = (left + right) >> 1
if nums[mid] == target:
# 收缩右边界
r = mid - 1;
# 搜索区间变为 [mid+1, right]
if nums[mid] < target: l = mid + 1
# 搜索区间变为 [left, mid - 1]
if nums[mid] > target: r = mid - 1
if l >= len(nums) or nums[l] != target: return -1
return l JavaScriptfunction binarySearchLeft(nums, target) {
let left = 0;
let right = nums.length - 1;
while (left <= right) {
const mid = Math.floor(left + (right - left) / 2);
if (nums[mid] == target)
// 收缩右边界
right = mid - 1;
if (nums[mid] < target)
// 搜索区间变为 [mid+1, right]
left = mid + 1;
if (nums[mid] > target)
// 搜索区间变为 [left, mid - 1]
right = mid - 1;
}
// 检查是否越界
if (left >= nums.length || nums[left] != target) return -1;
return left;
} C++暂时空缺,欢迎 PR 寻找最右边的满足条件的值
Javapublic int binarySearchRight(int[] nums, int target) {
// 搜索区间为 [left, right]
int left = 0
int right = nums.length - 1;
while (left <= right) {
int mid = left + (right - left) / 2;
if (nums[mid] < target) {
// 搜索区间变为 [mid+1, right]
left = mid + 1;
}
if (nums[mid] > target) {
// 搜索区间变为 [left, mid-1]
right = mid - 1;
}
if (nums[mid] == target) {
// 收缩左边界
left = mid + 1;
}
}
// 检查是否越界
if (right < 0 || nums[right] != target)
return -1;
return right;
} Pythondef binarySearchRight(nums, target):
# 左右都闭合的区间 [l, r]
l, r = 0, len(nums) - 1
while l <= r:
mid = (left + right) >> 1
if nums[mid] == target:
# 收缩左边界
l = mid + 1;
# 搜索区间变为 [mid+1, right]
if nums[mid] < target: l = mid + 1
# 搜索区间变为 [left, mid - 1]
if nums[mid] > target: r = mid - 1
if r < 0 or nums[r] != target: return -1
return r JavaScriptfunction binarySearchRight(nums, target) {
let left = 0;
let right = nums.length - 1;
while (left <= right) {
const mid = Math.floor(left + (right - left) / 2);
if (nums[mid] == target)
// 收缩左边界
left = mid + 1;
if (nums[mid] < target)
// 搜索区间变为 [mid+1, right]
left = mid + 1;
if (nums[mid] > target)
// 搜索区间变为 [left, mid - 1]
right = mid - 1;
}
// 检查是否越界
if (right < 0 || nums[right] != target) return -1;
return right;
} C++暂时空缺,欢迎 PR |
BFS(WIP)使用场景
模板Pythonfrom collections import deque
# n * m 大小的矩阵
n, m = len(grid), len(grid[0])
# 扩展方向
direction = [[0, 1], [0, -1], [-1, 0], [1, 0]]
# 记录节点是否被访问
visited = [[False for _ in range(m)] for _ in range(n)]
queue = deque()
# 深度
level = 0
# 加入初始节点
queue.append([0, 0])
visited[0][0] = True
while len(queue) > 0:
top = queue.popleft()
x, y = top[0], top[1]
# 扩展节点
for d in direction:
next_x = x + d[0]
next_y = y + d[1]
# 判断相邻节点是否有效
if (
next_x < 0
or next_x >= n
or next_y < 0
or next_y >= m
or visited[next_x][next_y]
):
continue
queue.append([next_x, next_y])
visited[next_x][next_y] = True
# 深度增加
level += 1 |
The text was updated successfully, but these errors were encountered: