Saturday, February 19, 2022

LeetCode 704: Binary Search

https://leetcode.com/problems/binary-search/
class Solution {
public:
int search(vector<int>& nums, int target) {
int l = 0;
int r = nums.size() - 1;
while(l <= r){
int mid = (l + r) / 2;
if(nums[mid] == target) return mid;
if(nums[mid] < target)
l = mid + 1;
else
r = mid - 1;
}
return -1;
}
};
view raw leetcode705.cpp hosted with ❤ by GitHub

No comments:

Post a Comment