33. Search in Rotated Sorted Array

来源:互联网 发布:xin域名值钱吗 编辑:程序博客网 时间:2024/06/05 09:42

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.

1.我的解答

就是找最大的那个值,然后一分为二,然后target就从这两半中找

class Solution {public:    int search(int target, vector<int>& num, int begin, int end){        int b = begin, e = end, m = 0;        if((begin > end) || (begin < 0))        return -1;        while(b < e){            m = b + (e - b)/2;            if(target > num[m])             b = m + 1;             else if(target < num[m])             e = m - 1;             else             return m;        }        if(num[b] == target)        return b;        else        return -1;    }    int search(vector<int>& nums, int target) {        int n = nums.size();        int b = 0, e = n - 1, m = 0;        while(e - b > 1){            m = b + (e - b)/2;            if((nums[m] > nums[b]) && (nums[m]) > nums[e])                b = m;                else if((nums[m] < nums[b]) && (nums[m] < nums[e]))                e = m;                else                break;        }        int big = 0;        if(nums[b] > nums[e])            big = b;        else            big = e;        if(target > nums[0])            return search(target,nums,0,big);            else if(target < nums[0])            return search(target,nums,big+1,n-1);            else            return 0;    }};


2.当然。。。。大神的思路永远比我的好,这里贴上他的解题思路和代码,供以后自己看

永远尊重别人的成果!

Remember the array is sorted, except it might drop at one point.

  • If nums[0] <= nums[i], then nums[0..i] is sorted (in case of "==" it's just one element, and in case of "<" there must be a drop elsewhere). So we should keep searching in nums[0..i] if the target lies in this sorted range, i.e., if nums[0] <= target <= nums[i].

  • If nums[i] < nums[0], then nums[0..i] contains a drop, and thus nums[i+1..end] is sorted and lies strictly between nums[i] and nums[0]. So we should keep searching in nums[0..i] if the target doesn't lie strictly between them, i.e., if target <= nums[i] < nums[0] ornums[i] < nums[0] <= target

Those three cases look cyclic:

    nums[0] <= target <= nums[i]               target <= nums[i] < nums[0]                         nums[i] < nums[0] <= target

So I have the three checks (nums[0] <= target)(target <= nums[i]) and (nums[i] < nums[0]), and I want to know whether exactly two of them are true. They can't all be true or all be false (check it), so I just need to distinguish between "two true" and "one true". Parity is enough for that, so instead of adding them I xor them, which is a bit shorter and particularly helpful in Java and Ruby, because those don't let me add booleans but do let me xor them.


代码:

int search(vector<int>& nums, int target) {    int lo = 0, hi = int(nums.size()) - 1;    while (lo < hi) {        int mid = (lo + hi) / 2;        if ((nums[0] > target) ^ (nums[0] > nums[mid]) ^ (target > nums[mid]))            lo = mid + 1;        else            hi = mid;    }    return lo == hi && nums[lo] == target ? lo : -1;}


0 0