[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.

Hide Tags

Array Binary Search


  这个是在 前一道的基础上改过来的,主要是 left middle right 相同的时候,递归为左右部分的并,其他部分不变:

class Solution {
public:
    bool search(int A[], int n, int target) {
        if(n==1)    return A[0]==target?true:false;
        return help(A,0,n-1,target);
    }
    int help(int* & A,int l,int r,int &target)
    {
        if(target<A[l]&&target>A[r])    return false;
        if(target==A[l])    return true;
        if(target==A[r])    return true;
        if(r-l==1)  return false;
        int m = (l+r) /2;
        if(target==A[m])    return true;
        if(A[l]<A[m]){
            if(A[l]<target&&target<A[m])    return help(A,l,m,target);
            return help(A,m,r,target);
        }
        else if(A[l]>A[m]){
            if(A[m]<target&&target<A[r])    return help(A,m,r,target);
            return help(A,l,m,target);
        }
        else{
            return help(A,l,m,target)||help(A,m,r,target);
        }
    }
};
时间: 2024-12-20 14:03:48

[LeetCode] Search in Rotated Sorted Array II 二分搜索的相关文章

[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——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

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(duplicated)

题目要求: 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://leetcode.com/problems/sear

[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&q

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 I (33) &amp;&amp; II (81) 解题思路

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