c++ binary search (6 ms)


  • 1
    class Solution {
      public:
        int searchInsert(vector<int>& nums, int target) {
          int l = 0, r = nums.size();
          while (l < r) {
            int mid = (l + r) >> 1;
            if (nums[mid] >= target) r = mid;
            else l = mid + 1;
          }
          return l;
        }
    };
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.