这5道题都是array的binary search就可以搞定了
分别是leetcode(35)——Search
Insert Position leetcode(33)——Search
in Rotated Sorted Array leetcode(81)——Search
in Rotated Sorted Array II leetcode(34)——Search
for a Range leetcode(74)——Search
a 2D Matrix
一:leetcode(35)——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.
[1,3,5,6]
, 5 → 2
[1,3,5,6]
, 2 → 1
[1,3,5,6]
, 7 → 4
[1,3,5,6]
, 0 → 0
链接:https://leetcode.com/problems/search-insert-position/
分析:通过分析我们发现当找到直接返回mid就可以了,当没找到需要返回begin就满足要求了
class Solution { public: int searchInsert(int A[], int n, int target) { int begin = 0, end = n-1; int mid = 0; while(begin <= end){ // 二分查找+判断边界 mid = (begin+end)/2; if(A[mid] == target) return mid; else if(A[mid] < target) begin = mid+1; else end = mid-1; } return begin; } };
二:leetcode(33)——Search
in Rotated Sorted Array
题目:
Suppose a sorted array is rotated at some pivot unknown to you beforehand.
(i.e., 0 1 2 4 5 6 7
might become 4
).
5 6 7 0 1 2
You are given a target value to search. If found in the array return its index, otherwise return -1.
You may assume no duplicate exists in the array.
链接:https://leetcode.com/problems/search-in-rotated-sorted-array/
分析:只要我们找到最小值O(N)时间,然后通过最后一个元素确定二分查找的区间就可以了
class Solution { public: int search(int A[], int n, int target) { if(n == 0) return -1; int minNum = A[0]; int index = 0; for(int i = 1; i < n; i++){ // 找到最小元素 if(A[i] < minNum){ minNum = A[i]; index = i; } } int begin = 0, end = n-1; // 看在哪个区间 if(A[n-1] >= target) // 注意这里是大于等于 begin = index; else end = index-1; while(begin <= end){ int mid = (begin+end)/2; if(A[mid] == target) return mid; else if(A[mid] < target) begin = mid+1; else end = mid-1; } return -1; } };
三:leetcode(81)——Search
in Rotated Sorted Array II
题目:
Follow up for "Search in Rotated Sorted Array":
What if duplicates are allowed?
Would this affect the run-time complexity? How and why?
Write a function to determine if a given target is in the array.
链接:https://leetcode.com/problems/search-in-rotated-sorted-array-ii/
分析:这里要求元素可以重复,因此我们需要找到紧靠最大值的最小即可,其它思路和上题一模一样
class Solution { public: bool search(int A[], int n, int target) { if(n == 0) return false; int minNum = A[0]; int index = 0; for(int i = 1; i < n; i++){ // 找到最小元素 if(A[i] <= minNum && A[i-1] > A[i]){ // 只改变了一点 确保最大值最小值相邻 minNum = A[i]; index = i; } } int begin = 0, end = n-1; // 通过A[n-1]看在哪个区间 if(A[n-1] >= target) // 注意这里是大于等于 begin = index; else end = index-1; while(begin <= end){ int mid = (begin+end)/2; if(A[mid] == target) return true; else if(A[mid] < target) begin = mid+1; else end = mid-1; } return false; } };
四: leetcode(34)——Search
for a Range
题目:
Given a sorted array of integers, find the starting and ending position of a given target value.
Your algorithm‘s runtime complexity must be in the order of O(log n).
If the target is not found in the array, return [-1, -1]
.
For example,
Given [5, 7, 7, 8, 8, 10]
and target value 8,
return [3, 4]
.
链接:https://leetcode.com/problems/search-for-a-range/
分析:当通过二分查找找到该元素时,我们并没有直接返回,而是通过判断begin和end处是否等于target,如果不等则通过begin++ 与end--逐渐逼近
class Solution { public: vector<int> searchRange(int A[], int n, int target) { int begin = 0, end = n-1; vector<int> result(2, -1); while(begin <= end){ int mid = (begin+end)>>1; if( A[mid]== target){ if(A[begin] == target && A[end] == target){ // 当中间值相等时直到左右区间都等于target才终止 result[0]= begin; result[1] = end; break; } if(A[begin] != target) begin++; // 否则begin++ end--逐渐逼近 if(A[end] != target) end--; }else if(A[mid] < target) begin = mid+1; else end = mid-1; } return result; } };
五:leetcode(74)——Search
a 2D Matrix
题目:
Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the following properties:
- Integers in each row are sorted from left to right.
- The first integer of each row is greater than the last integer of the previous row.
For example,
Consider the following matrix:
[ [1, 3, 5, 7], [10, 11, 16, 20], [23, 30, 34, 50] ]
Given target = 3
, return true
.
链接:https://leetcode.com/problems/search-a-2d-matrix/
分析:此题通过两次二分查找,先确定元素所在的行,然后在行中进行二分查找即可。
class Solution { public: bool searchMatrix(vector<vector<int> > &matrix, int target) { int m = matrix.size(); if(m == 0) return false; int n = matrix[0].size(); int begin = 0, end = m-1; int mid = 0; while(begin <= end){ // 先对第一列进行二分查找找到元素所在行 mid = (begin+end)/2; if(matrix[mid][0] == target) return true; else if(matrix[mid][0] < target) begin = mid+1; else end = mid-1; } int currentRow = 0; if(matrix[mid][0] > target) currentRow = mid -1; else currentRow = mid; if(currentRow < 0) return false; begin = 0, end = n-1; while(begin <= end){ // 然后对找到的行进行二分查找 mid = (begin + end)/2; if(matrix[currentRow][mid] == target) return true; else if(matrix[currentRow][mid] < target)begin = mid+1; else end = mid-1; } return false; } };