Advertisement
momo2345

binary search

Aug 27th, 2020
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.35 KB | None | 0 0
  1. class Solution {
  2. public:
  3.     int search(vector<int>& nums, int target) {
  4.         int l=0,r=nums.size()-1;
  5.         while(l<=r)
  6.         {
  7.             int mid=(l+r)/2;
  8.             if(nums[mid]==target)
  9.                 return mid;
  10.             if(nums[mid]>target) r=mid-1;
  11.             else l=mid+1;
  12.         }
  13.         return -1;
  14.        
  15.     }
  16. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement