您的位置:首页 > 其它

[LeetCode]35. Search Insert Position

2017-03-06 22:17 351 查看

[LeetCode]35. Search Insert Position

题目描述



思路

二分查找

代码

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) {
low = mid + 1;
}
else {
high = mid - 1;
}
}
return low;
}
};
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: