leetcode刷题35
最近刷题还比较有感觉,,难道是我水平在提高?。。其实是做到了比较简单的题目。。。
Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order.
You may assume no duplicates in the array.
Here are few examples.
[1,3,5,6]
, 5 → 2
[1,3,5,6]
, 2 → 1
[1,3,5,6]
, 7 → 4
[1,3,5,6]
, 0 → 0
这个题比较简单,,,就不说明什么了,直接上代码
class Solution { public: int searchInsert(vector<int>& nums, int target) { vector<int>::iterator it; if ((it = find(nums.begin(), nums.end(), target)) != nums.end()) return it - nums.begin(); else { for (it = nums.begin(); it != nums.end(); ++it) { if (*it > target) break; } return it - nums.begin(); } } };