前言

【LeetCode 题解】系列传送门:

http://www.cnblogs.com/double-win/category/573499.html

题目描述

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. # 题意
假设有一个有序的循环数组, 其起始位置未知。
如 序列```0 1 2 4 5 6 7``` 可能的数组形式为 ```4 5 6 7 0 1 2``` 给定一个目标数据,查找该数据是否在给定的数组中出现。 如果在数组中找到该目标数据,则返回该目标数据所在的数组下标, 否则返回-1. Tips: 假设给定的数组中没有重复的元素 # 思路
根据题意提取关键信息: 在一个***sorted array***中查找给定元素
首先能够想到的是binary search。 由于数组是一个循环数组, 那么数组的最小值和最大值并不一定分别在数组两端。
(1) 思路一: 从头到尾,依次遍历数组, 查找目标元素, 其时间复杂度O(n)
(2) 思路二: 从头到尾寻找min, max, 然后将左侧较大的数据concat到右侧, 然后再用binary search。 时间复杂度 O(n/2) + O(logn) ~ O(n)
(3) 思路三:如果将原始数组分割成左右两半, 可以发现其具有如下几种情形:
> case 1: 最小值和最大值分别在左右两侧:
> ```leftpart: [0 1 2 4] rightpart:[5 6 7]```
> 值得注意的是该情况最大值只能在最右侧, 且最小值只能在最左侧 > case 2:
> 最大值和最小值同侧: 最大值在右侧
> ```leftpart: [ 2 4 5 6] rightpart: [7 0 1]```
> 可以发现 leftValue = 2 medianValue = 6 rightValue = 1
> medianValue > leftValue && medianValue > rightValue:
> 如果 target > medianValue 或者 target < rightValue, 那么必在rightpart
> 否则,必在leftpart > case 3:
> 最大值和最小值同侧: 最小值在左侧
> ```leftpart: [6 7 0 1] rightpart: [2 4 5]```
> 可以发现 leftValue = 6 medianValue = 1 rightValue = 5
> medianValue < leftValue && medianValue < rightValue:
> 如果 target < medianValue 或者 target > rightValue, 那么必在leftpart
> 否则,必在rightpart 加上一些边界条件,即得结果。 # 解法
```python
class Solution(object):
def findTarget(self, nums, minIndex, maxIndex, target):
"""
:type nums: List[index]
:type minIndex: int
:type maxIndex: int
:type target: int
:rtype: int
"""
if nums[minIndex] == target:
return minIndex
if nums[maxIndex] == target:
return maxIndex if maxIndex == minIndex:
return 0 if target == nums[minIndex] else -1 median = (minIndex + maxIndex) / 2
if nums[median] == target:
return median if nums[median] > nums[minIndex] and nums[median] > nums[maxIndex]:
# maxValue and minValue is in right part
if target > nums[median] or target < nums[minIndex]:
return self.findTarget(nums, median + 1, maxIndex, target)
else:
return self.findTarget(nums, minIndex, median, target)
elif nums[median] < nums[minIndex] and nums[median] < nums[maxIndex]:
# maxValue is in left part
if target < nums[median] or target > nums[maxIndex]:
return self.findTarget(nums, minIndex, median, target)
else:
return self.findTarget(nums, median + 1, maxIndex, target)
else:
# maxValue is in maxIndex and minValue is in minIndex
if target < nums[minIndex] or target > nums[maxIndex]:
return -1
elif target > nums[median]:
return self.findTarget(nums, median + 1, maxIndex, target)
else:
return self.findTarget(nums, minIndex, median, target) def search(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: int
"""
numSize = len(nums)
# the array is empty
if numSize == 0:
return -1
minIndex = 0
maxIndex = numSize - 1
return self.findTarget(nums, minIndex, maxIndex, target)

声明

作者 Double_Win
出处 http://www.cnblogs.com/double-win/p/7966913.html
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文链接,否则作者保留追究法律责任的权利。 若本文对你有所帮助,您的关注和推荐是我们分享知识的动力!

[LeetCode 题解] Search in Rotated Sorted Array的更多相关文章

  1. [LeetCode] 033. Search in Rotated Sorted Array (Hard) (C++)

    指数:[LeetCode] Leetcode 解决问题的指数 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 033. ...

  2. [array] leetcode - 33. Search in Rotated Sorted Array - Medium

    leetcode - 33. Search in Rotated Sorted Array - Medium descrition Suppose an array sorted in ascendi ...

  3. LeetCode 81 Search in Rotated Sorted Array II [binary search] <c++>

    LeetCode 81 Search in Rotated Sorted Array II [binary search] <c++> 给出排序好的一维有重复元素的数组,随机取一个位置断开 ...

  4. LeetCode 33 Search in Rotated Sorted Array [binary search] <c++>

    LeetCode 33 Search in Rotated Sorted Array [binary search] <c++> 给出排序好的一维无重复元素的数组,随机取一个位置断开,把前 ...

  5. [leetcode]81. Search in Rotated Sorted Array II旋转过有序数组里找目标值II(有重)

    This is a follow up problem to Search in Rotated Sorted Array, where nums may contain duplicates. 思路 ...

  6. Java for LeetCode 081 Search in Rotated Sorted Array II

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

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

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

  8. Leetcode系列-Search in Rotated Sorted Array

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

  9. LeetCode 81. Search in Rotated Sorted Array II(在旋转有序序列中搜索之二)

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

随机推荐

  1. Codeforces Beta Round #12 (Div 2 Only)

    Codeforces Beta Round #12 (Div 2 Only) http://codeforces.com/contest/12 A 水题 #include<bits/stdc++ ...

  2. 使用Maven部署构件至私服

    --------------------siwuxie095                                 使用 Maven 部署构件至私服         1.部署构件到 Nexu ...

  3. tp中url地址大小写问题

    在tp配置文件中有一个URL_CASE_INSENSITIVE选项,设置为true,表示大小写不敏感. 'URL_CASE_INSENSITIVE' => true

  4. Spring框架的JDBC模板技术概述

    1. Spring框架中提供了很多持久层的模板类来简化编程,使用模板类编写程序会变的简单 2. 提供了JDBC模板,Spring框架提供的 * JdbcTemplate类 3. Spring框架可以整 ...

  5. 开发中经典sql总结

    1.说明:显示文章.提交人和最后回复时间 select a.title,a.username,b.adddate ,(select max(adddate) from table where tabl ...

  6. script标签的type="test/html"时

    们可以在<script>片断中定义一个被JS调用的代码,但代码又不在页面上显示,这时,我们可以使用下面的方法: 1 <script id="commentTemplate& ...

  7. tp5.1注册路由后接收不到参数

  8. url传递数据

    一.post传递数据 $ci = curl_init($url); curl_setopt($ci, CURLOPT_HEADER, 0); curl_setopt($ci, CURLOPT_RETU ...

  9. abp 的坑

    多数据库连接问题 viewmodel的验证问题 发布后,一段查找sln查找代码无法适用生产环境问题 多语言问题,默认中文设置与模板配置文件不统一

  10. part1:11-linux在线安装工具yum

    第三方的免费软件仓库安装包 1.Linux安装软件: rpm方式:rpm(Red Hat Package Manager)现在是Linux standard Base(LSB)中采用的包管理系统. 优 ...