【Leetcode】33. Search in Rotated Sorted Array

Question:

Suppose an array sorted in ascending order 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.

Tips:

给定一个数组,该数组是由一个有序的数组经过旋转(即将前面一段数字接到整个数组之后)得到的。判断target是否存在于该数组之中。

数组中不存在重复数字,如果target存在,返回他的index不存在则返回-1.

思路:

查找某数字是否存在,可以使用二分查找,但是由于该数组不是正常的有序,不能使用正常的BS。

设置两个指针,low high分别指向数组的首尾,mid=(high+low)/2;旋转后的数组用mid分为俩半,必然有一半是有序的,先判断target是不是在有序的一半内,如果在直接进行二分搜索,如果不在那么就在另外一部分里面。

代码:

public int search(int[] nums, int target) {
        if (nums == null || nums.length == 0)
            return -1;
        int len = nums.length ;
        int low = 0;
        int high = len - 1;
        while (low <= high) {
            int mid = low + (high - low) / 2;
            if (nums[mid] == target)
                return mid;
            if (nums[low] <= nums[mid]) {
                if(target<nums[mid] && target>=nums[low]){
                    high=mid-1;
                }else{
                    low=mid+1;
                }

            }else{
                if(target>nums[mid] && target<=nums[high]){
                    low=mid+1;
                }else
                    high=mid-1;
            }

        }
        return -1;
    }

原文地址:https://www.cnblogs.com/yumiaomiao/p/8530313.html

时间: 2024-10-29 05:18:48

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

【LeetCode】33. 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 du

【LeetCode】81. 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. 这题跟上一题一样,都是二分查找法,但是需要思考,对于时间复杂度,存在重复元素会有影

【一天一道LeetCode】#33. Search in Rotated Sorted Array

一天一道LeetCode 本系列文章已全部上传至我的github,地址: https://github.com/Zeecoders/LeetCode 欢迎转载,转载请注明出处 (一)题目 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

[Leetcode][Python]33: Search in Rotated Sorted Array

# -*- coding: utf8 -*-'''__author__ = '[email protected]' 33: Search in Rotated Sorted Arrayhttps://oj.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

【LeetCode题意分析&amp;解答】33. 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

【Lintcode】062.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 du

【LeetCode】Find Minimum in Rotated Sorted Array 在旋转数组中找最小数

Add Date 2014-10-15 Find Minimum 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). Find the minimum element. You may assume no duplicate exists in the

【leetcode】Find Minimum in Rotated Sorted Array II JAVA实现

一.题目描述 Follow up for "Find Minimum in Rotated Sorted Array":What if duplicates are allowed? Would this affect the run-time complexity? How and why? Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 m

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

Find Minimum in Rotated Sorted Array II Follow up for "Find Minimum in Rotated Sorted Array": What if duplicates are allowed? Would this affect the run-time complexity? How and why? Suppose a sorted array is rotated at some pivot unknown to you