chapter_searching/binary_search/ #52
Replies: 22 comments 25 replies
-
请问对于 《旋转数组的最小数字》这种题目,题解使用了双闭区间的初始值,但终止条件却是left<right,在移动right的时候也采用了right = mid 而不是 right = mid - 1,对于这种灵活的条件该怎么学习和使用呢,谢谢。 |
Beta Was this translation helpful? Give feedback.
-
这个二分查找的代码认真的吗 m := (i + j) / 2 // 计算中点索引 m 如果 i + j 是个奇数的话 咋么办 出现小数位 还能找到吗 |
Beta Was this translation helpful? Give feedback.
-
为什么线性查找和哈希查找的内容都没有了? |
Beta Was this translation helpful? Give feedback.
-
可以用>>1取代/2 精度高一点点 |
Beta Was this translation helpful? Give feedback.
-
不够看啊,动态规划啥时候安排上。感谢K神 |
Beta Was this translation helpful? Give feedback.
-
双闭区间在索引是无符号整数的语言中是有坑的 |
Beta Was this translation helpful? Give feedback.
-
大佬,问个题外话,你写作用的什么工具啊? |
Beta Was this translation helpful? Give feedback.
-
public static int binarySearch(int[] nums, int target) {
} 感觉这样更清晰 |
Beta Was this translation helpful? Give feedback.
-
左闭右开区间的rust算法中,右边界缩小写错了,应该是 |
Beta Was this translation helpful? Give feedback.
-
Rust 代码off-by-one error: /* 二分查找(左闭右开区间) */
fn binary_search_lcro(nums: &[i32], target: i32) -> i32 {
// 初始化左闭右开区间 [0, n) ,即 i, j 分别指向数组首元素、尾元素+1
let mut i = 0;
let mut j = nums.len() as i32;
// 循环,当搜索区间为空时跳出(当 i = j 时为空)
while i < j {
let m = i + (j - i) / 2; // 计算中点索引 m
if nums[m as usize] < target { // 此情况说明 target 在区间 [m+1, j) 中
i = m + 1;
} else if nums[m as usize] > target { // 此情况说明 target 在区间 [i, m) 中
// ###########################################
// ##### shoule be `j = m;` ##############
// ###########################################
j = m - 1;
} else { // 找到目标元素,返回其索引
return m;
}
}
// 未找到目标元素,返回 -1
return -1;
} |
Beta Was this translation helpful? Give feedback.
-
K神,左闭右开区间,输入target = 35; nums = { 1, 3, 6, 8, 12, 15, 23, 26, 31, 35 } 时,应该返回-1才对,结果是返回的9。初始化的时候应该这样才对
/* 二分查找(左闭右开) */
static int binarySearchLCRO(int[] nums, int target) {
// 初始化左闭右开 [0, n) ,即 i, j 分别指向数组首元素、尾元素+1
int i = 0, j = nums.length - 1;
// 循环,当搜索区间为空时跳出(当 i = j 时为空)
while (i < j) {
int m = i + (j - i) / 2; // 计算中点索引 m
if (nums[m] < target) // 此情况说明 target 在区间 [m+1, j) 中
i = m + 1;
else if (nums[m] > target) // 此情况说明 target 在区间 [i, m) 中
j = m;
else // 找到目标元素,返回其索引
return m;
}
// 未找到目标元素,返回 -1
return -1;
} 还是我对左闭右开区间理解有问题,麻烦解答一下 |
Beta Was this translation helpful? Give feedback.
-
请问 RUBY 的代码片段是缺失了吗? |
Beta Was this translation helpful? Give feedback.
-
hi, 对应的题目能否帮忙给出力扣相应或相似题目的链接? |
Beta Was this translation helpful? Give feedback.
-
二分的mid有3种写法:
|
Beta Was this translation helpful? Give feedback.
-
emmm 看到二分我的第一思路居然是直接用递归做了。。。不过这个办法只能返回 true / false,没法记录 idx,尴尬 func binarySearch(sortedArr []int, wantedVal int) bool {
if len(sortedArr) == 0 || (len(sortedArr) == 1 && sortedArr[0] != wantedVal) {
return false
}
middle := len(sortedArr) / 2
if wantedVal == sortedArr[middle] {
return true
}
if wantedVal < sortedArr[middle] {
return binarySearch(sortedArr[:middle], wantedVal)
}
if wantedVal > sortedArr[middle] {
return binarySearch(sortedArr[middle+1:], wantedVal)
}
return false
} |
Beta Was this translation helpful? Give feedback.
-
为什么不把BFS、DFS拎出来讲呀 |
Beta Was this translation helpful? Give feedback.
-
请问最后二分查找的局限性当中,如果按照代码的顺序,最少的单元操作数不应该是5个单元吗 |
Beta Was this translation helpful? Give feedback.
-
拓宽思路,小数据量情况下,线性查找反而比二分查找更快。不要只看算法的时间复杂度,更要结合实际场景 |
Beta Was this translation helpful? Give feedback.
-
更正:图 10-3里的循环终止条件:i>j这里应该是笔误typo了,应该是i<j才对 |
Beta Was this translation helpful? Give feedback.
-
在半开/双开区间情况下,请问为什么左索引与右索引反而要分别减1或者加1呢?考虑左闭右开情况,为什么右索引为size()呢?是为了适应区间无效的条件(left_index >= right_index)吗? |
Beta Was this translation helpful? Give feedback.
-
chapter_searching/binary_search/
一本动画图解、能运行、可提问的数据结构与算法入门书
https://www.hello-algo.com/chapter_searching/binary_search/
Beta Was this translation helpful? Give feedback.
All reactions