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.

Your algorithm's runtime complexity must be in the order of O(log n).

Example 1:

Input: nums = [4,5,6,7,0,1,2], target = 0
Output: 4

Example 2:

Input: nums = [4,5,6,7,0,1,2], target = 3
Output: -1

题目大意:有序数组旋转,寻找其中是否有某个特定的值,时间复杂度O(log n)。

思路:旋转后的数组,旋转后的后一部分肯定比前一部分所有元素都小;所以数组从中间分开,至少有一半是有序的。

那么可以得到,如果中间的元素(也就是nums[mid])比nums[0]大,那么前半部分有序,如果中间的元素比nums[len-1]小,那么后半部分有序,这两种情况只会出现一种。这样就可以先看这个数在不在有序的这一半数组中,在的话没什么好说的,二分查找;不在的话,继续找另一半RotatedArray。思路就是这么个思路,然而想把题目A掉还要考虑很多细节,尤其是边界值,> or >= ,< or <=等等,极易出错。

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

Search in Rotated Sorted Array——LeetCode的更多相关文章

  1. Search in Rotated Sorted Array leetcode java

    题目: Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 ...

  2. search in rotated sorted array leetcode

    原题链接 题意:给你一个目标值,或者返回其在数组中的下标位置,或者返回-1(表示不存在,查找失败). 例如 0 1 2 4 5 6 7 可能成为 4 5 6 7 0 1 2. 思路分析: 用二分搜索来 ...

  3. Find Minimum in Rotated Sorted Array leetcode

    原题链接 直接贴代码,这道题是 search in rotated sorted array leetcode 的前面部分! class Solution { public: int findMin( ...

  4. [LeetCode] Search in Rotated Sorted Array II 在旋转有序数组中搜索之二

    Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would this ...

  5. [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 migh ...

  6. LeetCode:Search in Rotated Sorted Array I II

    LeetCode:Search in Rotated Sorted Array Suppose a sorted array is rotated at some pivot unknown to y ...

  7. [LeetCode]题解(python):081 - Search in Rotated Sorted Array II

    题目来源 https://leetcode.com/problems/search-in-rotated-sorted-array-ii/ Follow up for "Search in ...

  8. Leetcode系列-Search in Rotated Sorted Array

    做Leetcode题有一段时间了,但都是断断续续的,到现在才做了30题左右,感觉对自己来说还是有点难度的.希望自己能继续坚持下去,在校招前能解决超过一百题吧. 其实这些题就是用来训练你的解题思路的,做 ...

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

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 33: Search in Rotated Sorted Arrayhttps ...

随机推荐

  1. c++中代理类的学习

    https://blog.csdn.net/lcg910978041/article/details/51468680 C++代理类是为了解决这样的问题: 容器通常只能包含一种类型的对象,所以很难在容 ...

  2. java编程--03介绍关于日期常用的计算

    /** * 获取2个日期之间的天数差 * d2-d1 * @return * @throws Exception * @Description: */ public static int getDif ...

  3. Android官方架构组件介绍之ViewModel(三)

    ViewModel 像Activity,Fragment这类应用组件都有自己的生命周期并且是被Android的Framework所管理的.Framework可能会根据用户的一些操作和设备的状态对Act ...

  4. 卸载 maya

    AUTO Uninstaller 更新下载地址 1.选择MAYA 2.选择版本 3.点击[开始卸载]

  5. Unity String 转换成 Vector3

  6. (转)AIX 5.3安装SSH .

    AIX 5.3安装SSH . 原文:http://blog.csdn.net/chunhua_love/article/details/12004845 环境:OS:AIX 5.3SSH: opens ...

  7. PHP substr()函数

    PHP substr()函数可以分割文字,但要分割的文字如果包括中文字符往往会遇到问题,这时可以用mb_substr()/mb_strcut这个函数,mb_substr() /mb_strcut的用法 ...

  8. 管理员账户权限不足 解决方案 类似没有XXX权限之类的问题解决方法

  9. vue的计算和监视属性,附一小实例

    1. 计算属性 在computed属性对象中定义计算属性的方法 在页面中使用{{方法名}}来显示计算的结果 2. 监视属性: 通过通过vm对象的$watch()或watch配置来监视指定的属性 当属性 ...

  10. js随堂初体验(一)

    Js初体验(-) 1 js的基础知识 A web三大标准:1 html:结构标准    2 css:表现标准  3 javascript:行为标准 B js三种书写方式:1 行内js:onclick ...