LeetCode-33. Search in Rotated Sorted Array
2017-11-20 23:22
330 查看
Suppose an array sorted in ascending order 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.
时间复杂度:O(n);空间复杂度:O(1)。
Java实现
由题意,我们并不知道排序后的数组从哪儿旋转,但我们可以确定数组的某一部分肯定是从小到大排序的,比如题目中的例子:
0 1 2 4 5 6 7
7 0 1 2 4 5 6
6 7 0 1 2 4 5
5 6 7 0 1 2 4
4 5 6 7 0 1 2
2 4 5 6 7 0 1
1 2 3 5 6 7 0
上方加粗的都是以中间数为界形成的有序序列。
从中我们可以看出当中间数小于最右边的数时,右侧是有序的;而中间数大于最右边的数时,左侧是有序的。
所以当target不等于中间数时,我们要先判断target是在有序区还是无序区。具体的如下:
中间数小于最右边的数,若target比中间数大而且比最右侧数小,则一定落在有序区(右边),左指针变化;否则落在无序区,右指针变化。
中间数大于最右边的数,若target比中间数小而且比最左侧数大,则一定落在有序区(左边),右指针变化;否则落在无序区,左指针变化。
时间复杂度O(logN),空间复杂度O(1)。
C++实现
官网上还有另一种二分查找的解法,具体可见下方链接。
https://leetcode.com/problems/search-in-rotated-sorted-array/discuss/
(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.
方法一:暴力搜索
遍历整个数组,如果发现相同的则返回下标,否则返回-1。时间复杂度:O(n);空间复杂度:O(1)。
Java实现
class Solution { public int search(int[] nums, int target) { for(int i=0;i<nums.length;++i) { if(nums[i]==target) return i; } return -1; } }
方法二:二分查找的变式
使用二分查找关键在于比较数组的中间数与target时,左右两指针如何变化。由题意,我们并不知道排序后的数组从哪儿旋转,但我们可以确定数组的某一部分肯定是从小到大排序的,比如题目中的例子:
0 1 2 4 5 6 7
7 0 1 2 4 5 6
6 7 0 1 2 4 5
5 6 7 0 1 2 4
4 5 6 7 0 1 2
2 4 5 6 7 0 1
1 2 3 5 6 7 0
上方加粗的都是以中间数为界形成的有序序列。
从中我们可以看出当中间数小于最右边的数时,右侧是有序的;而中间数大于最右边的数时,左侧是有序的。
所以当target不等于中间数时,我们要先判断target是在有序区还是无序区。具体的如下:
中间数小于最右边的数,若target比中间数大而且比最右侧数小,则一定落在有序区(右边),左指针变化;否则落在无序区,右指针变化。
中间数大于最右边的数,若target比中间数小而且比最左侧数大,则一定落在有序区(左边),右指针变化;否则落在无序区,左指针变化。
时间复杂度O(logN),空间复杂度O(1)。
C++实现
class Solution { public: int search(vector<int>& nums, int target) { int left=0,right=nums.size()-1; while(left<=right) { int mid=(left+right)/2; if(nums[mid]==target) return mid; else if(nums[mid]>nums[right]) { if(nums[mid]>target&&nums[left]<=target) right=mid-1; else left=mid+1; } else { if(nums[mid]<target&&nums[right]>=target) left=mid+1; else right=mid-1; } } return -1; } };
官网上还有另一种二分查找的解法,具体可见下方链接。
https://leetcode.com/problems/search-in-rotated-sorted-array/discuss/
相关文章推荐
- LeetCode 33. Search in Rotated Sorted Array
- 【LeetCode 33】Search in Rotated Sorted Array
- leetcode - 33. Search in Rotated Sorted Array
- leetcode 33. Search in Rotated Sorted Array
- LeetCode 33 Search in Rotated Sorted Array 题解
- LeetCode 33 Search in Rotated Sorted Array
- Leetcode 33. Search in Rotated Sorted Array
- LeetCode(33)Search in Rotated Sorted Array
- leetcode[33]Search in Rotated Sorted Array
- LeetCode-33. Search in Rotated Sorted Array
- LeetCode 33. Search in Rotated Sorted Array(旋转数组搜索)
- [leetcode33] Search in Rotated Sorted Array
- LeetCode 33. Search in Rotated Sorted Array && 81. Search in Rotated Sorted Array II
- 【Leetcode】33. Search in Rotated Sorted Array
- Leetcode-33.Search in Rotated Sorted Array.
- LeetCode(33)Search in Rotated Sorted Array
- LeetCode 33 Search in Rotated Sorted Array
- leetcode33~Search in Rotated Sorted Array
- Leetcode 33. Search in Rotated Sorted Array (Hard) (cpp)
- leetcode 33. Search in Rotated Sorted Array (复杂二分查找)