leetcode problem 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 duplicate exists in the array.
思路:
给你一个已排序好但移位了的数组,找到特定的数。画一幅图就很容易理解了。一个被旋转的有序数组A[1..n],假定转折点是A[k],那么A[k+1] < A[k+2] < ... < A[n] < A[1] < A[2] < ... < A[k]
可以用二分查找稍微改点型:虽然我们不知道转折点k在哪,但是我们还是可以通过比较A[mid]与A[start],A[end]来确定要找的目标书target是在A[start,...,mid]中,还是在A[mid+1,...,end] 所以时间复杂度还是lg(n)
代码:
Runtime: 12 ms
class Solution{
public:
int search(int A[], int n, int target) {
if (n <= )
return -;
if (n == )
return *A == target ? : -;
int begin = , end = n, mid = (begin + end) / ;
if (A[begin] <= A[mid-]) {
if (target >= A[begin] && target <= A[mid-]) {
auto it = lower_bound(A, A + mid, target);
if (*it == target)
return it - A;
else
return -;
}
int res = search(A + mid, end - mid, target);
return res == - ? - : mid + res;
}
else {
if (target >= A[mid] && target <= A[end-]) {
auto it = lower_bound(A+mid, A+end, target);
if (*it == target)
return it - A;
else
return -;
}
int res = search(A + begin, mid - begin, target);
return res == - ? - : begin + res;
}
}
};
leetcode problem 33 -- Search in Rotated Sorted Array的更多相关文章
- [Leetcode][Python]33: Search in Rotated Sorted Array
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 33: Search in Rotated Sorted Arrayhttps ...
- LeetCode题解33.Search in Rotated Sorted Array
33. Search in Rotated Sorted Array Suppose an array sorted in ascending order is rotated at some piv ...
- 【LeetCode】33. Search in Rotated Sorted Array (4 solutions)
Search in Rotated Sorted Array Suppose a sorted array is rotated at some pivot unknown to you before ...
- 【一天一道LeetCode】#33. Search in Rotated Sorted Array
一天一道LeetCode 本系列文章已全部上传至我的github,地址: https://github.com/Zeecoders/LeetCode 欢迎转载,转载请注明出处 (一)题目 Suppos ...
- LeetCode OJ 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 migh ...
- 【LeetCode】33. Search in Rotated Sorted Array 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【Leetcode】33. Search in Rotated Sorted Array
Question: Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforeh ...
- LeetCode:33. Search in Rotated Sorted Array(Medium)
1. 原题链接 https://leetcode.com/problems/search-in-rotated-sorted-array/description/ 2. 题目要求 给定一个按升序排列的 ...
- Python 解LeetCode:33. Search in Rotated Sorted Array
题目描述:在一个旋转数组中查找给定的值,其中旋转数组中不含重复值: 思路: 第一遍二分遍历,找到数组中最小值的索引: 第二遍分别对最小值左右两边的数组进行二分查找: class Solution(ob ...
随机推荐
- cocos2d-x c++和object-c内存管理比较
转自:http://www.2cto.com/kf/201307/227142.html 既然选择了C++作为游戏开发的语言, 手动的管理内存是难以避免的, 而Cocos2d-x的仿Objctive- ...
- 7 种流行 PHP IDE 的比较
编写关于 PHP 的系列文章让我更加深刻地了解了 PHP 开发人员的世界.我和许多 PHP 程序员交谈过,最令我惊奇的是只有很少的人使用 IDE.大多数程序员使用文本编辑器,比如 Microsoft® ...
- 产生不重复的随机数TGUID
uses ActiveX; procedure TForm1.BtnNewClick(Sender: TObject);var ID: TGUID; S: string;begin if CoC ...
- 再探ASP.NET 5(转载)
就在最近一段时间,微软又有大动作了,在IDE方面除了给我们发布了Viausl Studio 2013 社区版还发布了全新的Visual Studio 2015 Preview. Visual Stud ...
- 浅谈ASP脚本的解释
10多年前,ASP的出现使全世界的WEB设计者摆脱了C/C++的繁杂,大幅提升了页面的开发效率 然而一直到数年之后,asp的解释一直握在微软手里,后来阿帕奇也支持asp了,虽然没有IIS那么强大,但是 ...
- NSURLSession 请求
参考网站:http://ningandjiao.iteye.com/blog/2010753 http://www.cocoachina.com/industry/20131106/7304.html ...
- PHP之ThinkPHP模板标签操作
Action : $User=M("user"); $list=$User->select(); $this->assign("list ...
- oracle中的 exists 和 in 的效率问题
1) select * from T1 where exists(select * from T2 where T1.a=T2.a) ; T1数据量小而T2数据量非常大时,T1<<T2 时 ...
- 【原】Spring与MongoDB集成:配置
MongoDB的API提供了DBObject接口来实现BSONObject的操作方法,BasicDBObject是具体实现.但是并没有提供DBObject与BeanObject的转换.在还没有了解到与 ...
- Apple Watch 使用体验
交互 Apple Watch 支持以下几种交互方式: 按下 Digital Crown (数码表冠),在桌面和表盘之间切换. 长按 Digital Crown (数码表冠),启动 Siri. 双击 D ...