LeetCode-Search in Rotated Sorted Array-旋转数组中的搜索-二分搜索+代数逻辑

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

一个被旋转的数组,要求二分搜索查询一个数。修改二分搜索可以完成。注意可以通过A[l]<A[r]来判断l,r中间数字分布的情况。在A[l]>A[r]时,中间有一个间断点。可以通过A[mid]>A[r]来判断中点与旋转中心的位置关系。然后通过逻辑比较移动l或r。画出一个折线图能够更好的理解。

注意二分搜索终止条件为l<r,更新l位置时始终用l=mid+1。这样能够避免死循环。在退出循环后通过判断A[l]与target关系来确定是否查询到。

class Solution {
public:
    int search(int A[], int n, int target) {
        int l=0;
        int r=n-1;
        int x=target;
        while(r>l){
            int mid=(l+r)/2;
            if (x==A[mid]) return mid;
            if (A[l]<A[r]) {
                if (x<A[mid]) r=mid;
                else l=mid+1;
            }
            else { //Al>Ar
                if (A[mid]<A[r]) {
                    if (x<A[mid]) r=mid;
                    else {
                        if (x>A[r]) r=mid;
                        else l=mid+1;
                    }
                }
                else {
                    if (x>A[mid]) l=mid+1;
                    else {
                        if (x>A[r]) r=mid;
                        else l=mid+1;
                    }
                }
            }
        }
        if (A[l]==x) return l;
        return -1;
    }
};
时间: 2024-08-08 09:04:49

LeetCode-Search in Rotated Sorted Array-旋转数组中的搜索-二分搜索+代数逻辑的相关文章

(每日算法)LeetCode --- Search in Rotated Sorted Array(旋转数组的二分检索)

Search in Rotated Sorted Array I && II Leetcode 对有序数组进行二分查找(下面仅以非递减数组为例): int binarySort(int A[], int lo, int hi, int target) { while(lo <= hi) { int mid = lo + (hi - lo)/2; if(A[mid] == target) return mid; if(A[mid] < target) lo = mid + 1;

Find Minimum in Rotated Sorted Array 旋转数组中找最小值 @LeetCode

O(n)的算法就不说了,这题主要考查的是 O(logn)的算法. 有序数组容易想到使用二分查找解决,这题就是在二分基础上做一些调整.数组只有一次翻转,可以知道原有序递增数组被分成两部分,这俩部分都是有序递增的(这题只需要考虑有序数组的递增情况). 假如翻转后的数组以第 x 个结点分为两部分 A[0..x] 和 A[x+1..n].则 A[0..x] 这一段是有序递增的, A[x+1..n] 这一段也是有序递增的.并且因为原数组是有序递增的,A[0..x] 中所有数都会大于 A[x+1..n] 中

LeetCode: Search in Rotated Sorted Array

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, othe

[LeetCode] Search in Rotated Sorted Array [35]

题目 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 dup

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. 原题链接:https://oj.leetcode.com/problems/search

Leetcode | Search in Rotated Sorted Array I &amp; II

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

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 duplic

LeetCode: Search in Rotated Sorted Array [032]

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

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