Leetcode_search-insert-position
2014-03-17 21:28
351 查看
http://oj.leetcode.com/problems/search-insert-position/
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.
分析:数组是排序好的,所以是典型的二分查找。
若数组中没有target,即最后没有匹配A[mid] == target,那么
如果最后front > mid, (即rear = mid),那么target比A[mid]大(因此才会有front=mid+1),应位于A[mid]之后,返回mid+1;
如果最后rear < mid, (即front = mid),那么target比A[mid]小(因此才会rear=mid-1),target应位于mid这个位置,返回mid,原来在mid这个位置的往后挪一位。
上面的关系一定要理清楚。
参考代码:
//SECOND TRIAL
class Solution {public: int searchInsert(int A[], int n, int target) { if(!A || !n) return 0; int left = 0, right = n-1, mid; while(left<=right) { mid = (left+right)/2; if(target == A[mid]) return mid; else if(target < A[mid]) right = mid-1; else left = mid+1; } return left; }};
python version:
class Solution: # @param A, a list of integers # @param target, an integer to be inserted # @return integer def searchInsert(self, A, target): if not A: return 0; left = 0 right = len(A)-1 while left<=right: mid = (left+right)//2 if A[mid] < target: left = mid+1 elif A[mid] > target: right = mid-1 else: return mid return left
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
分析:数组是排序好的,所以是典型的二分查找。
若数组中没有target,即最后没有匹配A[mid] == target,那么
如果最后front > mid, (即rear = mid),那么target比A[mid]大(因此才会有front=mid+1),应位于A[mid]之后,返回mid+1;
如果最后rear < mid, (即front = mid),那么target比A[mid]小(因此才会rear=mid-1),target应位于mid这个位置,返回mid,原来在mid这个位置的往后挪一位。
上面的关系一定要理清楚。
参考代码:
class Solution { public: int searchInsert(int A[], int n, int target) { int front = 0, rear = n-1, mid; while(front<=rear) { mid = (front+rear)/2; if(A[mid]==target) { return mid; } else if(A[mid] < target) { front = mid+1; } else { rear = mid-1; } } if(mid > rear) { return mid; } return mid+1; } };
//SECOND TRIAL
class Solution {public: int searchInsert(int A[], int n, int target) { if(!A || !n) return 0; int left = 0, right = n-1, mid; while(left<=right) { mid = (left+right)/2; if(target == A[mid]) return mid; else if(target < A[mid]) right = mid-1; else left = mid+1; } return left; }};
python version:
class Solution: # @param A, a list of integers # @param target, an integer to be inserted # @return integer def searchInsert(self, A, target): if not A: return 0; left = 0 right = len(A)-1 while left<=right: mid = (left+right)//2 if A[mid] < target: left = mid+1 elif A[mid] > target: right = mid-1 else: return mid return left
相关文章推荐
- LeetCode[Array]: Search Insert Position
- 【LeetCode】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(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
- [leetcode] Search Insert Position
- Leetcode: Search Insert Position
- [勇者闯LeetCode] 35. Search Insert Position
- Leetcode 35 - Search Insert Position
- LeetCode35. Search Insert Position
- [LeetCode]—Search Insert Position 有序数组中找目标插入的位置
- Leetcode 35. Search Insert Position (Medium) (cpp)