【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 the array return its index, otherwise return -1.

You may assume no duplicate exists in the array.

思路:

1、找到旋转点即第一个逆序,然后将数组分两半,分别用二分查找

2、二分查找 根据左右跟中间比较的结果 对有序的部分采用二分查找。

算法

public int search(int[] nums, int target) {
    int left = 0,right = nums.length-1,mid = 0;
    while(left<=right){
        mid = left+(right-left)/2;
        if(nums[mid]==target){
            return mid;
        }
        if(nums[left]<nums[mid]){ //则left到mid有序
            if(nums[left]==target){
                return left;
            }
            if(nums[left]<=target&&target<nums[mid]){ //若target在left到mid之间
                right = mid;
            }else{
                left = mid + 1;
            }
        }else{//则mid到right之间有序
            if(target==nums[right]){
                return right;
            }
            if(nums[mid]<target&&target<=nums[right]){ //若target在mid到right之间
                left = mid + 1;
            }else{
                right = mid-1;
            }
        }
    }
    return -1;
}  
时间: 2024-10-15 06:51:04

【Leetcode】 Search in Rotated Sorted Array的相关文章

【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 (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 retu

【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 (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