【Search In Rotated Sorted Array】cpp

题目

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 return -1.

You may assume no duplicate exists in the array.

 

代码

class Solution {
public:
    int search(int A[], int n, int target) {
        int begin = 0;
        int end = n-1;
        while (begin != end)
        {
            if( begin+1 == end )
            {
                if (A[begin]==target) return begin;
                if (A[end]==target) return end;
                return -1;
            }
            const int mid = (end+begin)/2;
            if (A[mid]==target) return mid;
            if(target<A[mid])
            {
                if(A[begin]<A[mid])
                {
                    if(target>=A[begin])
                    {
                        end = mid-1;
                    }
                    else
                    {
                        begin = mid+1;
                    }
                }
                else
                {
                    end = mid-1;
                }
            }
            else
            {
                if(A[begin]<A[mid])
                {
                    begin = mid+1;
                }
                else
                {
                    if(target<=A[end])
                    {
                        begin = mid+1;
                    }
                    else
                    {
                        end = mid-1;
                    }
                }
            }
        }
        if (A[begin]==target) return begin;
        return -1;
    }
};

 

Tips:

1. 分target与A[mid]大小情况先讨论

2. 由于前半截或后半截至少一个是有序的,再按照这个来分条件讨论

if else代码中有一些逻辑可以合并,但是考虑到保留原始逻辑更容易被理解,就保留现状了

===================================

第二次过这道题,还是费了一些周折,主要是在于begin+1==end和begin==end这样case的处理。刷了几次,修改了一些细节,AC了。

class Solution {
public:
    int search(vector<int>& nums, int target) {
            int begin=0, end=nums.size()-1;
            while ( begin<end )
            {
                if ( begin+1==end )
                {
                    if ( nums[begin]==target ) return begin;
                    if ( nums[end]==target ) return end;
                    return -1;
                }
                int mid = (begin+end)/2;
                if ( nums[mid]==target ) return mid;
                // first half sorted
                if ( nums[begin]<nums[mid] )
                {
                    if ( target>nums[mid] )
                    {
                        begin = mid+1;
                    }
                    else
                    {
                        if ( target>=nums[begin] )
                        {
                            end = mid-1;
                        }
                        else
                        {
                            begin = mid+1;
                        }
                    }
                    continue;
                }
                // second half sorted
                if ( nums[mid]<nums[end] )
                {
                    if ( target<nums[mid])
                    {
                        end = mid-1;
                    }
                    else
                    {
                        if ( target<=nums[end])
                        {
                            begin = mid+1;
                        }
                        else
                        {
                            end = mid-1;
                        }
                    }
                }

            }
            return nums[begin]==target?begin:-1;
    }
};

 ============================

学习了一种边界条件更简洁的写法,这里能简洁主要是因为把begin+1==end和begin==end的情况都融进了 nums[begin]<=nums[mid]的条件;多了一个等号,就把这些case都给融进去了,提高了代码的效率。

class Solution {
public:
    int search(vector<int>& nums, int target) {
            int begin=0, end=nums.size()-1;
            while ( begin<=end )
            {
                int mid = (begin+end)/2;
                if ( nums[mid]==target ) return mid;
                // first half sorted
                if ( nums[begin]<=nums[mid] )
                {
                    if ( target>nums[mid] )
                    {
                        begin = mid+1;
                    }
                    else
                    {
                        if ( target>=nums[begin] )
                        {
                            end = mid-1;
                        }
                        else
                        {
                            begin = mid+1;
                        }
                    }
                    continue;
                }
                // second half sorted
                if ( nums[mid]<nums[end] )
                {
                    if ( target<nums[mid])
                    {
                        end = mid-1;
                    }
                    else
                    {
                        if ( target<=nums[end])
                        {
                            begin = mid+1;
                        }
                        else
                        {
                            end = mid-1;
                        }
                    }
                }

            }
            return -1;
    }
};

 

posted on 2015-04-15 22:51  承续缘  阅读(165)  评论(0编辑  收藏  举报

导航