Search in Rotated Sorted Array II leetcode java

题目:

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 in Rotated Sorted Array类似,问题只在于存在dupilcate。那么和之前那道题的解法区别就是,不能通过比较A[mid]和边缘值来确定哪边是有序的,会出现A[mid]与边缘值相等的状态。所以,解决方法就是对于A[mid]==A[low]和A[mid]==A[high]单独处理。

当中间值与边缘值相等时,让指向边缘值的指针分别往前移动,忽略掉这个相同点,再用之前的方法判断即可。

这一改变增加了时间复杂度,试想一个数组有同一数字组成{1,1,1,1,1},target=2, 那么这个算法就会将整个数组遍历,时间复杂度由O(logn)降到O(n)

实现代码如下:

1     public boolean search(int [] A,int target){
 2        if(A==null||A.length==0)
 3          return false;
 4         
 5        int low = 0;
 6        int high = A.length-1;
 7       
 8        while(low <= high){
 9            int mid = (low + high)/2;
10            if(target < A[mid]){
11                if(A[mid]<A[high])//right side is sorted
12                  high = mid - 1;//target must in left side
13                else if(A[mid]==A[high])//cannot tell right is sorted, move pointer high
14                  high--;
15                else//left side is sorted
16                  if(target<A[low])
17                     low = mid + 1;
18                  else 
19                     high = mid - 1;
20            }else if(target > A[mid]){
21                if(A[low]<A[mid])//left side is sorted
22                  low = mid + 1;//target must in right side
23                else if(A[low]==A[mid])//cannot tell left is sorted, move pointer low
24                  low++;
25                else//right side is sorted
26                  if(target>A[high])
27                     high = mid - 1;
28                  else
29                     low = mid + 1;
30            }else
31              return true;
32        }
33        
34        return false;
35 }

Reference: http://blog.csdn.net/linhuanmars/article/details/20588511

Search in Rotated Sorted Array II leetcode java

时间: 2024-07-30 19:33:32

Search in Rotated Sorted Array II leetcode java的相关文章

Search in Rotated Sorted Array II——LeetCode

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 in Rotated Sorted Array II -- leetcode

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. 循环有序数组,且存在重复. 基本思路: 进行折半查找 1. 左端点 小于 中间结点,  

leetcode 题解:Search in Rotated Sorted Array II (旋转已排序数组查找2)

题目: 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. 说明: 1)和1比只是有重复的数字,整体仍采用二分查找 2)方法二 : 实现:  

LeetCode: Search in Rotated Sorted Array II 解题报告

Search in Rotated Sorted Array II Follow up for "LeetCode: 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 arr

[leetcode]Search in Rotated Sorted Array II @ Python

原题地址:https://oj.leetcode.com/problems/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

[LeetCode] Search in Rotated Sorted Array II [36]

题目 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 in Rotated Sorted

LeetCode OJ: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. 这个和前面的一个不一样就在于可能会有重复的数字,那么判断的时候就应该注意了,遇到start

LeetCode——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://oj.leetcode.com/problems/search

LeetCode: Search in Rotated Sorted Array II [081]

[题目] 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 in Rotated Sorted Ar