题目描述
解法
思路:二分查找
class Solution { public: int searchInsert(vector<int>& nums, int target) { int left = 0, right = nums.size() - 1; int count = 0; if(nums[right] < target){ count++; //若target大于数组最后一个数,count++ } while(left < right){ int mid = left + (right - left) / 2; if(nums[mid] == target){ return mid; }else if(nums[mid] > target){ right = mid; }else{ left = mid + 1; } } return left + count; } };
标签:right,target,nums,int,mid,35,插入,LeetCode,left From: https://www.cnblogs.com/zc-030/p/17270210.html