LeetCode Solutions : 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.

Java Solution (
refer to my blog LeetCode Solutions : Search in Rotated Sorted Array ):

public class Solution {
    public boolean search(int[] A, int target) {
		if(A.length==0)
			return false;
        int low=0;
		int high=A.length-1;
		while(low<=high){
			int mid=low+(high-low)/2;
			if(target==A[mid])
				return true;
			if(A[low]<A[mid]){// the elements from low to mid is strictly increasing order
				if(A[low]<=target&&target<A[mid])
					high=mid-1;
				else
					low=mid+1;
			}else if(A[low]>A[mid]){// the elements from mid to high is strictly increasing order
				if(A[mid]<target&&target<=A[high])
					low=mid+1;
				else
					high=mid-1;
			}
			else // skip the duplicate one,that is to say,A[low] == A[mid]
				low++;
		}
		return false;
    }
}

Of course, you are able to resolve it with the brute one, also need O(n)

时间: 2024-11-08 09:38:09

LeetCode Solutions : Search in Rotated Sorted Array II的相关文章

【LeetCode】Search in Rotated Sorted Array II (2 solutions)

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. 解法一:顺序查找 cl

【leetcode】Search in Rotated Sorted Array II

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. 与I类似,只是在二分搜

Java for LeetCode 081 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. 解题思路: 参考Java for LeetCode 033 Search in Rota

leetcode No81. Search in Rotated Sorted Array II

Question: 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. 在旋转数组中找target,与I不同的是,这次存在重复元素 Algo

[LeetCode][Java] 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. 题意: 伴随着问题<Search in Rotated Sorted Ar

【leetcode】Search in Rotated Sorted Array II(middle)☆

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. 我的思路: 太混乱了 不提了.注意关键区分依据 排好序的一定是从小到大的 看大神的吧: b

leetCode 81.Search in Rotated Sorted Array II (旋转数组的搜索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. 思路:此题在解的时候,才发现Search in Rotated Sorted Array

【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. [解析] 相比Search in Rotated Sorted Array,在

leetcode 【 Search in Rotated Sorted Array II 】python 实现

题目: 与上一道题几乎相同:不同之处在于array中允许有重复元素:但题目要求也简单了,只要返回true or false http://www.cnblogs.com/xbf9xbf/p/4254590.html 代码:oj测试通过 Runtime: 73 ms 1 class Solution: 2 # @param A a list of integers 3 # @param target an integer 4 # @return a boolean 5 def search(sel