码迷,mamicode.com
首页 > 其他好文 > 详细

[leetcode]Search in Rotated Sorted Array II

时间:2014-12-04 23:21:47      阅读:461      评论:0      收藏:0      [点我收藏+]

标签: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" can be seen here

基本思路:

与"Search in Rotated Sorted Array" 类似,只对返回值做少许修改即可。具体参见《Search in Rotated Sorted Array


代码:

    bool search(int A[], int n, int target) {  //C++
        if(n < 0)
            return false;
        if(n == 1)
        { 
            if(A[0] == target)
                return true;
            else return false;
        }
        
        if(A[0] == target)
            return true;

            
        for(int i = 1; i < n; i++)
        {
            if(A[i] < A[i-1])
            {
                if(A[i] > target || A[i-1] < target || target > A[n-1])
                return false;
            }
            if(A[i] == target )
                return true;
        }
        return false;
    
    }


[leetcode]Search in Rotated Sorted Array II

标签:leetcode   算法   

原文地址:http://blog.csdn.net/chenlei0630/article/details/41730689

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!