[LeetCode] Search in Rotated Sorted Array、Search for a Range、Search Insert Position、Search in Rotate
来源:互联网 发布:sql substring的用法 编辑:程序博客网 时间:2024/04/30 16:48
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.
前阵寝室同学面百度也问到这个问题了。一般简单点会说没有重复的,如果有重复的会更复杂一些。
有两个地方要注意一下,一个是A[mid] >= A[l] ,注意等号,还有下面两个if 也是要有等号,又粗心掉了,哎。我还有救吗
class Solution {public: int search(int A[], int n, int target) { // Start typing your C/C++ solution below // DO NOT write int main() function assert(A&&n>0); int l=0,r=n-1; if ( A[r]>A[l] ) return bisearch(A,l,r,target); while(l<=r) { int mid=(l+r)>>1; if ( A[mid]==target ) return mid; if ( A[mid] >= A[l] ) { if ( target>=A[l] && target <=A[mid]) return bisearch(A,l,mid,target); l=mid+1; } else { if ( target >=A[mid] && target <= A[r] ) return bisearch(A,mid,r,target); r=mid-1; } } return -1; } int bisearch(int A[],int l,int r,int tar) { while(l<=r) { int mid=(l+r)>>1; if ( A[mid] <tar ) l=mid+1; else if ( A[mid]>tar ) r=mid-1; else return mid; } return -1; }};
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]
.
class Solution {public: vector<int> searchRange(int A[], int n, int target) { // Start typing your C/C++ solution below // DO NOT write int main() function assert(A&&n>=0); int l=0,r=n-1; int left=-1,right=-1; while(l<=r) { int mid=(l+r)>>1; if ( A[mid]>target ) r=mid-1; else if ( A[mid] < target ) l=mid+1; else { left=mid; r=mid-1; } } if ( left==-1 ) return vector<int>(2,-1); else if ( left<n-1&&A[left+1]!=target) return vector<int>(2,left); l=0,r=n-1; while(l<=r) { int mid=(l+r)>>1; if ( A[mid]>target ) r=mid-1; else if ( A[mid] < target ) l=mid+1; else { right=mid; l=mid+1; } } vector<int> ans; ans.push_back(left); ans.push_back(right); return ans; }};
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
class Solution {public: int searchInsert(int A[], int n, int target) { // Start typing your C/C++ solution below // DO NOT write int main() function if ( !A || n<=0) return -1; int l=0,r=n-1; while(l<=r) { int mid=(l+r)>>1; if (A[mid]<target) l=mid+1; else r=mid-1; } return l; }};
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.
当无法知道是那边的时候,我用了个顺序查找,其实可以递归左右都尝试一下。只是记得把mid去掉,免得会死循环。
class Solution {public: bool search(int A[], int n, int target) {// Start typing your C/C++ solution below// DO NOT write int main() function//assert(A&&n>=0);int l=0,r=n-1;while(l<=r){int mid=l+((r-l)>>1);if ( A[mid]==target)return true;if (A[mid]>A[l]){if ( A[mid]<target || A[l]>target )l=mid+1;elsereturn _bisearch(A,l,mid,target);}else if ( A[mid]<A[l] ){if ( A[mid] <target && target<=A[r] )return _bisearch(A,mid,r,target);elser=mid-1;}else{if ( A[mid]>A[r] )l=mid+1;else{for(int i=l;i<=r;i++)if ( A[i] == target)return true;return false;}}}return false;}bool _bisearch(int A[],int l,int r,int t){while(l<=r){int mid=(l+r)>>1;if (A[mid]==t )return true;else if (A[mid]<t )l=mid+1;else r=mid-1;}return false;}};
- [LeetCode] Search in Rotated Sorted Array、Search for a Range、Search Insert Position、Search in Rotate
- leetcode Search in Rotated Sorted Array & Search for a Range & Search Insert Position
- Search Insert Position &&Search in Rotated Sorted Array &&Search in Rotated Sorted Array II
- search-insert-position/search-in-rotated-sorted-array/search-in-rotated-sorted-array ii
- Search Insert Position &Search in Rotated Sorted Array
- [leetcode][search] Search in Rotated Sorted Array
- [Leetcode] Search in a Rotated Sorted Array
- Search Rotate Position in Sorted Array
- [leetcode Q34&Q35] Search for a Range&Search Insert Position
- LeetCode: Search in Rotated Sorted Array
- LeetCode: Search in Rotated Sorted Array II
- LeetCode Search in Rotated Sorted Array
- LeetCode Search in Rotated Sorted Array II
- [Leetcode] Search in Rotated Sorted Array
- LeetCode : Search in Rotated Sorted Array
- LeetCode : Search in Rotated Sorted Array II
- [LeetCode] Search in Rotated Sorted Array II
- [LeetCode] Search in Rotated Sorted Array
- Java反射详解
- 外部排序技术之多路归并
- assert()函数
- C# DateTime 日期加1天 减一天 加一月 减一月 等方法
- 言简意赅话彩虹, 虹,霓
- [LeetCode] Search in Rotated Sorted Array、Search for a Range、Search Insert Position、Search in Rotate
- 经典过桥问题的思路解析
- c语言中的常量,变量
- Linux shell 不同进制数据转换(二进制,八进制,十六进制,base64)
- 安装WE7遇到的问题
- 最真的梦——文/林清玄
- android jni Log
- cookie的作用域
- 为什么匿名内部类和局部内部类只能访问final变量