zoukankan      html  css  js  c++  java
  • 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.
    

      

    class Solution {
    public:
        bool search(int A[], int n, int target) {
            // Start typing your C/C++ solution below
            // DO NOT write int main() function
            if(A == NULL || n <1) return false;
            
            int left = 0;
            int right = n-1;
            while(left <= right){
            
                int mid = (right - left)/2 + left;
                if(A[mid] == target)
                    return true;
                if(A[mid] > A[left])
                {
                    if(target >= A[left] && target < A[mid])
                        right = mid - 1;
                    else 
                        left = mid  + 1;
                }else if(A[mid] < A[left]){
                
                    if(target <= A[right] && target > A[mid])
                        left = mid + 1;
                    else
                        right = mid -1;
                }else{
                    if(A[left] == target)
                        return true;
                    
                    ++left ;
                }
            }
            return false;
        }
    };
  • 相关阅读:
    6554545555
    484844
    学习资料整理
    word加上尾注之后参考文献下面的横线去除
    数据结构+算法
    python编程
    计算机网络(1)
    数据结构
    数据分析笔试(3)
    数据分析笔试(2)
  • 原文地址:https://www.cnblogs.com/graph/p/3331285.html
Copyright © 2011-2022 走看看