aboutsummaryrefslogtreecommitdiff
path: root/search_insert_pos.cpp
blob: f6768a58fd7c3e4b8b7ab7a33a8f0760ad8e9f83 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include "leetcode.h"

class Solution {
public:
  int searchInsert(vector<int> &nums, int target) {
    int low = 0, high = nums.size() - 1;
    while (low <= high) {
      int mid = low + (high - low) / 2;
      if (nums[mid] == target)
        return mid;
      else if (nums[mid] > target)
        high = mid - 1;
      else
        low = mid + 1;
    }
    return low;
  }
};

int main() {}