码迷,mamicode.com
首页 > 其他好文 > 详细

035. Search Insert Position

时间:2016-09-11 11:29:46      阅读:150      评论:0      收藏:0      [点我收藏+]

标签:

 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 };

 

035. Search Insert Position

标签:

原文地址:http://www.cnblogs.com/shadowwalker9/p/5861309.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!