标签:
1 class Solution { 2 public: 3 int searchInsert(vector<int>& nums, int target) { 4 int left = 0, right = nums.size() - 1; 5 int mid = 0; 6 while (left <= right) { 7 mid = left + (right - left) / 2; 8 if (nums[mid] > target) right = mid - 1; 9 else if (nums[mid] < target) left = mid + 1; 10 else break; 11 } 12 if (nums[mid] == target) return mid; 13 else return right + 1; 14 } 15 };
标签:
原文地址:http://www.cnblogs.com/shadowwalker9/p/5861309.html