[LeetCode 题解] Search in Rotated Sorted Array
前言
【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的更多相关文章
- [LeetCode] 033. Search in Rotated Sorted Array (Hard) (C++)
指数:[LeetCode] Leetcode 解决问题的指数 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 033. ...
- [array] leetcode - 33. Search in Rotated Sorted Array - Medium
leetcode - 33. Search in Rotated Sorted Array - Medium descrition Suppose an array sorted in ascendi ...
- LeetCode 81 Search in Rotated Sorted Array II [binary search] <c++>
LeetCode 81 Search in Rotated Sorted Array II [binary search] <c++> 给出排序好的一维有重复元素的数组,随机取一个位置断开 ...
- LeetCode 33 Search in Rotated Sorted Array [binary search] <c++>
LeetCode 33 Search in Rotated Sorted Array [binary search] <c++> 给出排序好的一维无重复元素的数组,随机取一个位置断开,把前 ...
- [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. 思路 ...
- 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 ...
- [LeetCode] 81. Search in Rotated Sorted Array II 在旋转有序数组中搜索 II
Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would this ...
- Leetcode系列-Search in Rotated Sorted Array
做Leetcode题有一段时间了,但都是断断续续的,到现在才做了30题左右,感觉对自己来说还是有点难度的.希望自己能继续坚持下去,在校招前能解决超过一百题吧. 其实这些题就是用来训练你的解题思路的,做 ...
- LeetCode 81. Search in Rotated Sorted Array II(在旋转有序序列中搜索之二)
Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would this ...
随机推荐
- Ajax的get方式传值 避免& 与= 号
js代码 例如: var name = $("#name”).value;//为a&b=7 name=encodeURLComponent(name); 可以将a&b=7转化 ...
- myeclipse 10激活,本人已测试过可行
激活步骤: 下载myeclipse 10硬解程序包: ed2k://|file|%5Bmyeclipse.10.0.%E6%9B%B4%E6%96%B0%E5%8F%91%E5%B8%83%28%E7 ...
- Winsock版本的“hello world!”
1.基于TCP协议的“hello world!” 1)服务器端:WSAStartup()->socket()->bind()->listen()->accept()->s ...
- ReportViewer工具栏功能扩展[手动设置打印/导出按钮]
ReportViewer在IE11后打印按钮就存在兼容问题,火狐,谷歌也存在打印按钮显示的兼容性问题,本资料就是解决ReportViewer打印按钮显示的问题, 通过自己写脚本添加到DOM里面让所有浏 ...
- [leetcode]523. Continuous Subarray Sum连续子数组和(为K的倍数)
Given a list of non-negative numbers and a target integer k, write a function to check if the array ...
- 解决ios手机页面overflow scroll滑动很卡的问题
在移动端html中经常出现横向/纵向滚动的效果,但是在iPhone中滚动速度很慢,感觉不流畅,有种卡卡的感觉,但是在安卓设备上没有这种感觉; 要解决这个问题很简单: 一行代码搞定 -webkit-ov ...
- Windows“储存并显示最近在开始菜单和任务栏中打开的项目”显示灰色问题解决
问题截图如下: 解决方法 打开"组策略",依次选择"用户配置"--"管理模板"--"开始菜单和任务栏"--"不 ...
- RNA-seq要做几次生物学重复?找出来的100%都是真正的应答基因
尹师妹:“哈师兄,做验证实验好辛苦,老板让我提高筛选差异基因的条件,尽量降低假阳性,我该怎么筛?” 小哈打开Evernote,给尹师妹看张表: “瞧见那个100%了吗?30 million mappe ...
- 七大排序的个人总结(二) 归并排序(Merge
七大排序的个人总结(二) 归并排序(Merge 归并排序(Merge Sort): 归并排序是一个相当“稳定”的算法对于其它排序算法,比如希尔排序,快速排序和堆排序而言,这些算法有所谓的最好与最 ...
- Ubuntu 网卡多个 IP 地址
临时添加 IP 地址 首先,让我们找到网卡的 IP 地址.在我的 Ubuntu 15.10 服务器版中,我只使用了一个网卡. 运行下面的命令找到 IP 地址: 复制代码 代码如下: sudo ip a ...