LeetCode : Search Insert Position
2017-03-15 10:50
253 查看
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
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) { int l = 0,h = nums.size()-1; while(l<=h) { int m = (l+h)/2; if(nums[m]==target) return m; else if(nums[m]>target) { h = m-1; } else { l = m+1; } } return l; } };
相关文章推荐
- leetcode Search in Rotated Sorted Array & Search for a Range & Search Insert Position
- Leetcode- Search Insert Position
- LeetCode: Search Insert Position
- LeetCode-Search Insert Position
- [LeetCode] Search Insert Position
- LeetCode 35: Search Insert Position
- leetcode-Search Insert Position
- Leetcode_Search Insert Position
- [leetcode]#35. Search Insert Position
- Leetcode: Search Insert Position
- Search Insert Position 找适合插入的位置@LeetCode
- LeetCode Search Insert Position
- LeetCode | Search Insert Position
- 【LEETCODE】35-Search Insert Position
- LeetCode 35. Search Insert Position
- leetcode 35. Search Insert Position
- LeetCode:Search Insert Position
- 【leetcode】Search Insert Position
- leetcode做题总结,题目Search Insert Position 2012/03/03
- LeetCode刷题笔录 Search Insert Position