【LeetCode】Search in Rotated Sorted Array (3 solutions)

Search in Rotated Sorted Array

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) {
        for(int i = 0; i < n; i ++)
        {
            if(A[i] == target)
                return i;
        }
        return -1;
    }
};

解法二:二分查找

先用二分法找到最大元素,将数组切分成有序数组,再进行二分查找

class Solution {
public:
    int search(int A[], int n, int target) {
        if(n==1)
            return (target==A[0])?0:-1;

        //find the maximum first
        int low = 0;
        int high = n-1;
        while(low < high)
        {
            int mid = (low+high)/2;
            if(A[mid] < A[low])
                high = mid-1;
            else if(A[mid] > A[low])
                low = mid;
            else
            {//low+1==high
                if(A[high]>A[low])
                    low = high;
                break;
            }
        }
        int ind = low;
        //to here, low is the index of maximum
        //0~ind, ind+1~n-1 are two sorted arrays
        if(target >= A[0])
        {//first array: 0~ind
            low = 0;
            high = ind;
            while(low <= high)
            {
                int mid = (low+high)/2;
                if(target == A[mid])
                    return mid;
                else if(target > A[mid])
                    low = mid+1;
                else
                    high = mid-1;
            }
            return -1;
        }
        else
        {//second array: ind+1, n-1
            low = ind+1;
            high = n-1;
            while(low <= high)
            {
                int mid = (low+high)/2;
                if(target == A[mid])
                    return mid;
                else if(target > A[mid])
                    low = mid+1;
                else
                    high = mid-1;
            }
            return -1;
        }
    }
};

解法三:可处理重复元素的二分查找

class Solution {

public:
    int search(int A[], int n, int target) {
        int low = 0;
        int high = n-1;
        while (low <= high)
        {
            int mid = (low+high)/2;
            if(A[mid] == target)
                return mid;
            if (A[low] < A[mid])
            {
                if(A[low] <= target && target < A[mid])
                //binary search in sorted A[low~mid-1]
                    high = mid - 1;
                else
                //subproblem from low to high
                    low = mid + 1;
            }
            else if(A[mid] < A[high])
            {
                if(A[mid] < target && target <= A[high])
                //binary search in sorted A[mid+1~high]
                    low = mid + 1;
                else
                //subproblem from low to mid-1
                    high = mid - 1;
            }
            else if(A[low] == A[mid])
                low += 1;    //A[low]==A[mid] is not the target, so remove it
            else if(A[mid] == A[high])
                high -= 1;  //A[high]==A[mid] is not the target, so remove it
        }
        return -1;
    }
};

时间: 2024-10-11 04:19:29

【LeetCode】Search in Rotated Sorted Array (3 solutions)的相关文章

【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. [解析] 相比Search in Rotated Sorted Array,在

【leetcode】Search in Rotated Sorted Array II

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. 与I类似,只是在二分搜

【leetcode】Search in Rotated Sorted Array

Search in Rotated Sorted Array 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 retu

【LeetCode】Search in Rotated Sorted Array II (2 solutions)

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. 解法一:顺序查找 cl

【leetcode】Search in Rotated Sorted Array II(middle)☆

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. 我的思路: 太混乱了 不提了.注意关键区分依据 排好序的一定是从小到大的 看大神的吧: b

【LeetCode】Search in Rotated Sorted Array——旋转有序数列找目标值

[题目] 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 d

【LeetCode】Search in Rotated Sorted Array 解题报告

[题目] 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 d

【Leetcode】 Search in Rotated Sorted Array

题目链接:https://leetcode.com/problems/search-in-rotated-sorted-array/ 题目: 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 th

【leetcode】Search in Rotated Sorted Array (hard)

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 duplic