【leetcode】81. Search in Rotated Sorted Array II【java】
2016-12-23 11:17
525 查看
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.
这道题是二分查找Search
Insert Position的变体,思路在Search
in Rotated Sorted Array中介绍过了,不了解的朋友可以先看看那道题哈。和Search
in Rotated Sorted Array唯一的区别是这道题目中元素会有重复的情况出现。不过正是因为这个条件的出现,出现了比较复杂的case,甚至影响到了算法的时间复杂度。原来我们是依靠中间和边缘元素的大小关系,来判断哪一半是不受rotate影响,仍然有序的。而现在因为重复的出现,如果我们遇到中间和边缘相等的情况,我们就丢失了哪边有序的信息,因为哪边都有可能是有序的结果。假设原数组是{1,2,3,3,3,3,3},那么旋转之后有可能是{3,3,3,3,3,1,2},或者{3,1,2,3,3,3,3},这样的我们判断左边缘和中心的时候都是3,如果我们要寻找1或者2,我们并不知道应该跳向哪一半。解决的办法只能是对边缘移动一步,直到边缘和中间不在相等或者相遇,这就导致了会有不能切去一半的可能。所以最坏情况(比如全部都是一个元素,或者只有一个元素不同于其他元素,而他就在最后一个)就会出现每次移动一步,总共是n步,算法的时间复杂度变成O(n)。代码如下:
public class Solution {
public boolean search(int[] nums, int target) {
if (nums == null || nums.length == 0){
return false;
}
int left = 0;
int right = nums.length - 1;
while (left <= right){
int mid = (left + right) / 2;
if (nums[mid] == target){
return true;
}
if (nums[mid] > nums[left]){
if (target >= nums[left] && target < nums[mid]){
right = mid - 1;
} else {
left = mid + 1;
}
} else if (nums[mid] < nums[left]){
if (target > nums[mid] && target <= nums[right]){
left = mid + 1;
} else {
right = mid -1;
}
} else {
left++;
}
}
return false;
}
}
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.
这道题是二分查找Search
Insert Position的变体,思路在Search
in Rotated Sorted Array中介绍过了,不了解的朋友可以先看看那道题哈。和Search
in Rotated Sorted Array唯一的区别是这道题目中元素会有重复的情况出现。不过正是因为这个条件的出现,出现了比较复杂的case,甚至影响到了算法的时间复杂度。原来我们是依靠中间和边缘元素的大小关系,来判断哪一半是不受rotate影响,仍然有序的。而现在因为重复的出现,如果我们遇到中间和边缘相等的情况,我们就丢失了哪边有序的信息,因为哪边都有可能是有序的结果。假设原数组是{1,2,3,3,3,3,3},那么旋转之后有可能是{3,3,3,3,3,1,2},或者{3,1,2,3,3,3,3},这样的我们判断左边缘和中心的时候都是3,如果我们要寻找1或者2,我们并不知道应该跳向哪一半。解决的办法只能是对边缘移动一步,直到边缘和中间不在相等或者相遇,这就导致了会有不能切去一半的可能。所以最坏情况(比如全部都是一个元素,或者只有一个元素不同于其他元素,而他就在最后一个)就会出现每次移动一步,总共是n步,算法的时间复杂度变成O(n)。代码如下:
public class Solution {
public boolean search(int[] nums, int target) {
if (nums == null || nums.length == 0){
return false;
}
int left = 0;
int right = nums.length - 1;
while (left <= right){
int mid = (left + right) / 2;
if (nums[mid] == target){
return true;
}
if (nums[mid] > nums[left]){
if (target >= nums[left] && target < nums[mid]){
right = mid - 1;
} else {
left = mid + 1;
}
} else if (nums[mid] < nums[left]){
if (target > nums[mid] && target <= nums[right]){
left = mid + 1;
} else {
right = mid -1;
}
} else {
left++;
}
}
return false;
}
}
相关文章推荐
- [leetcode]81. Search in Rotated Sorted Array II(Java)
- Leetcode 81. Search in Rotated Sorted Array II (Medium) (java)
- Java for LeetCode 081 Search in Rotated Sorted Array II
- leetcode[81]Search in Rotated Sorted Array II
- [Leetcode] Search in Rotated Sorted Array II (Java)
- LeetCode 81. Search in Rotated Sorted Array II
- [leetcode-81]Search in Rotated Sorted Array II(C)
- leetcode 81. Search in Rotated Sorted Array II
- leetcode 81: Search in Rotated Sorted Array II
- [Leetcode 81, Medium] Search in Rotated Sorted Array II
- !leetcode[33&81]:Search in Rotated Sorted Array[I & II]
- [LeetCode]81 Search in Rotated Sorted Array II
- 【LeetCode】81. Search in Rotated Sorted Array II (2 solutions)
- [LeetCode]81 Search in Rotated Sorted Array II
- leetCode 81.Search in Rotated Sorted Array II (旋转数组的搜索II) 解题思路和方法
- 81. Search in Rotated Sorted Array II Leetcode Python
- [Leetcode] 81. Search in Rotated Sorted Array II
- leetcode[81] Search in Rotated Sorted Array II
- LeetCode 81 Search in Rotated Sorted Array II
- Leetcode 81. Search in Rotated Sorted Array II