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

[LeetCode]81 Search in Rotated Sorted Array II

时间:2015-01-04 19:31:08      阅读:133      评论:0      收藏:0      [点我收藏+]

标签:leetcode

https://oj.leetcode.com/problems/search-in-rotated-sorted-array-ii/

http://blog.csdn.net/linhuanmars/article/details/20588511

public class Solution {
    public boolean search(int[] A, int target) {
        if (A == null || A.length == 0)
            return false;
        return find(A, 0, A.length - 1, target);
    }
    
    // O(n)
    private boolean find(int[] A, int low, int high, int t)
    {
        if (low > high)
            return false;
            
        if (low == high)
            return A[low] == t;
            
        int mid = (low + high) / 2;
        
        if (A[mid] == t)
            return true;
            
        if (A[mid] > A[low])
        {
            if (A[low] <= t && t < A[mid])
                return find(A, low, mid - 1, t); // Left part
            else
                return find(A, mid + 1, high, t);
        }
        else if (A[mid] < A[low])
        {
            if (t > A[mid] && t <= A[high])
                return find(A, mid + 1, high, t); // Right part
            else
                return find(A, low, mid - 1, t);
        }
        else
        {
            return find(A, low + 1, high, t); // Just shift left.
        }
    }
}


[LeetCode]81 Search in Rotated Sorted Array II

标签:leetcode

原文地址:http://7371901.blog.51cto.com/7361901/1599018

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