leetcode---search-insert-position---查找
2017-10-01 15:24
411 查看
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(int A[], int n, int target) { int h = n-1; int l = 0; while(l <= h) { int m = (l + h) / 2; if(A[m] == target) return m; else if(A[m] < target) l = m + 1; else h = m - 1; } return l; } };
相关文章推荐
- leetcode_35题——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(查找插入的位置)
- 【LeetCode35】【Search Insert Position】二分法查找
- leetcode 35. Search Insert Position搜索插入位置(二分查找)
- LeetCode 35 Search Insert Position 二叉查找相关(一)
- Leetcode 35 Search Insert Position 二分查找(二分下标)
- [leetcode-查找]--35. Search Insert Position
- leetcode——Search Insert Position 二分查找 binary search
- LeetCode @ Search Insert Position 二分查找
- LeetCode:Search Insert Position,Search for a Range (二分查找,lower_bound,upper_bound)
- LeetCode---35. Search Insert Position(查找插入位置)
- Leetcode刷题记—— 35. Search Insert Position(查找插入位置)
- LeetCode 35 Search Insert Position(查找插入位置)
- [LeetCode]Search Insert Position (二分查找总结) Java Version
- LeetCode | Search Insert Position(查找插入位置)
- 【LeetCode】- Search Insert Position(查找插入的位置)