0704. 二分查找 #83
utterances-bot
started this conversation in
Comments
Replies: 2 comments
-
ans = mid |
Beta Was this translation helpful? Give feedback.
0 replies
-
贴个c++的 class Solution {
public:
int search(vector<int>& nums, int target) {
int n = nums.size();
int left = 0, right = n - 1;
while (left <= right) {
int mid = left + ((right - left) / 2);
if (nums[mid] < target) {
left = mid + 1;
} else {
right = mid - 1;
}
}
if (left == n || nums[left] != target) {
return -1;
}
return left;
}
}; |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
0704. 二分查找 | 算法通关手册
https://algo.itcharge.cn/Solutions/0700-0799/binary-search/
Beta Was this translation helpful? Give feedback.
All reactions