lintcode-medium-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.

public class Solution {
    /**
     * param A : an integer ratated sorted array and duplicates are allowed
     * param target :  an integer to be search
     * return : a boolean
     */
    public boolean search(int[] A, int target) {
        // write your code here

        if(A == null || A.length == 0)
            return false;

        int left = 0;
        int right = A.length - 1;

        return search(A, target, left, right);
    }

    public boolean search(int[] A, int target, int left, int right){

        if(left > right)
            return false;

        if(left == right){
            if(A[left] == target)
                return true;
            else
                return false;
        }

        int mid = left + (right - left) / 2;

        if(A[mid] == target)
            return true;

        if(A[mid] > target){
            if(A[mid] > A[left]){
                boolean flag1 = search(A, target, left, mid - 1);
                boolean flag2 = search(A, target, mid + 1, right);

                if(!flag1 && !flag2)
                    return false;
                else if(!flag1)
                    return flag2;
                else
                    return flag1;
            }
            else if(A[mid] < A[left]){
                return search(A, target, left, mid - 1);
            }
            else{
                return search(A, target, left + 1, right);
            }
        }
        else{
            if(A[mid] > A[left]){
                return search(A, target, mid + 1, right);
            }
            else if(A[mid] < A[left]){
                boolean flag1 = search(A, target, left, mid - 1);
                boolean flag2 = search(A, target, mid + 1, right);

                if(!flag1 && !flag2)
                    return false;
                else if(!flag1)
                    return flag2;
                else
                    return flag1;
            }
            else{
                return search(A, target, left + 1, right);
            }
        }

    }

}
时间: 2024-10-09 20:58:36

lintcode-medium-Search in Rotated Sorted Array II的相关文章

[Lintcode]62. Search in Rotated Sorted Array/[Leetcode]33. Search in Rotated Sorted Array

[Lintcode]62. Search in Rotated Sorted Array/[Leetcode]33. Search in Rotated Sorted Array 本题难度: Medium/Medium Topic: Binary Search Description Search in Rotated Sorted Array Suppose a sorted array is rotated at some pivot unknown to you beforehand. (

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

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 (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 @ 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

33. Search in Rotated Sorted Array &amp;&amp; 81. Search in Rotated Sorted Array II &amp;&amp;

33. Search in Rotated Sorted Array 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

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_81——Search in Rotated Sorted Array II(二分查找)

Search in Rotated Sorted Array II Total Accepted: 38274 Total Submissions: 121824My Submissions Question Solution Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would this affect the run-time complexity? How and w