0035.cpp (411B)
1 class Solution { 2 public: 3 int searchInsert(vector<int> &nums, int target) { 4 int low = 0, high = nums.size() - 1; 5 while (low <= high) { 6 int mid = low + (high - low) / 2; 7 if (nums[mid] == target) return mid; 8 if (nums[mid] < target) 9 low = mid + 1; 10 else 11 high = mid - 1; 12 } 13 return high + 1; 14 } 15 };