【一天一道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 search. If found in the array return its index, otherwise return -1.
You may assume no duplicate exists in the array.
(二)解题
一个排好序的数组,经过一定的旋转之后得到新的数组,在这个新的数组中查找一个目标数。
那么,首先我们需要在旋转后的数组中找到原数组的起始点,并将数组分成两部分。
例如:4,5,6,7,0,1,2分为4,5,6,7和0,1,2
然后,确定目标数在哪一个部分,
最后,采用二分法来进行加速搜索!
具体看代码:
未优化版本
class Solution {
public:
int search(vector<int>& nums, int target) {
int len = nums.size();
int i = 0 ;
int j = len-1;
int p = 0 ;
while(p+1<len&&nums[p]<nums[p+1]) p++;//找出第一个破坏升序的数即为旋转中心
if(nums[0]<=target) j=p;//确定被查找的数在哪个部分
else if(nums[len-1]>=target) i=p+1;
while(i<=j)//二分搜索
{
int mid = (i+j)/2;
if(nums[mid] == target) return mid;
else if(nums[mid] >target) j= mid-1;
else i = mid+1;
}
return -1;//未找到则返回-1
}
};
AC之后一看运行时间8ms,看来代码还有待优化,于是在查找旋转中心的方法上进行优化,采用二分法进行搜索旋转中心。
优化版本
class Solution {
public:
int search(vector<int>& nums, int target) {
int len = nums.size();
int i = 0;
int j = len - 1;
int p = 0;
bool isfind = false;
if (nums[0]>nums[len-1])//如果进行了旋转
{
while (i < j)//二分搜索旋转中心
{
if (i == j - 1) {
isfind = true;
break;
}
int mid = (i + j) / 2;
if (nums[i] < nums[mid]) i = mid;
if (nums[j] > nums[mid]) j = mid;
}
}
if (isfind)//找到了旋转中心
{
if (nums[0] <= target) { j = i; i = 0; }
if (nums[len - 1] >= target) { i = j; j = len - 1;}
}
while (i<=j)二分搜索目标数
{
int mid = (i + j) / 2;
if (nums[mid] == target) return mid;
else if (nums[mid] >target) j = mid-1;
else i = mid+1;
}
return -1;
}
};
优化后的版本运行时间4ms,快了一半!
【一天一道LeetCode】#33. Search in Rotated Sorted Array的更多相关文章
- [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 33 Search in Rotated Sorted Array [binary search] <c++>
LeetCode 33 Search in Rotated Sorted Array [binary search] <c++> 给出排序好的一维无重复元素的数组,随机取一个位置断开,把前 ...
- [LeetCode] 33. Search in Rotated Sorted Array 在旋转有序数组中搜索
Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. (i.e. ...
- LeetCode 33. Search in Rotated Sorted Array(在旋转有序序列中搜索)
Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. (i.e. ...
- leetCode 33.Search in Rotated Sorted Array(排序旋转数组的查找) 解题思路和方法
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(M)
题目: Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. ( ...
- 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 migh ...
- Java [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 ...
- [leetcode]33. Search in Rotated Sorted Array旋转过有序数组里找目标值
Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. (i.e. ...
- LeetCode 33 Search in Rotated Sorted Array(循环有序数组中进行查找操作)
题目链接 :https://leetcode.com/problems/search-in-rotated-sorted-array/?tab=Description Problem :当前的数组 ...
随机推荐
- 天梯赛-L1-018. 大笨钟
L1-018. 大笨钟 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 微博上有个自称"大笨钟V"的家伙,每 ...
- Errors running builder 'DeploymentBuilder' on project '工程名'
打开myEclipse就会报 Errors running builder 'DeploymentBuilder' on project '工程名' xxxNullpointException 的错误 ...
- win 10 和 CentOS 7 双系统安装
工具及材料 1.一台PC 2.一个U盘,8G以上 3.需要的文件:CentOS-7-x86_64-DVD-1511.iso 4.需要的软件:UltraI ...
- Docker 控制组
控制组(cgroups)是 Linux 内核的一个特性,主要用来对共享资源进行隔离.限制.审计等.只有能控制分配到容器的资源,才能避免当多个容器同时运行时的对系统资源的竞争. 控制组技术最早是由 Go ...
- 巧用第三方快速开发Android App 热门第三方SDK及框架
巧用第三方快速开发Android App 热门第三方SDK及框架 历经大半年的时间,终于是把这门课程给录制出来了,也就在今天,正式在慕课网上上线了 项目地址:巧用第三方快速开发Android App ...
- vue 2.0 scopedSlots和slots在render函数中的应用示例
渲染内容为: hello from functional render scopedSlots render scopedSlots named slot of render hello from f ...
- 用豆瓣镜像解决pip安装慢的问题
pip3 install django==1.9 -i http://pypi.douban.com/simple/
- 制定一个apk路径 然后跳出安装界面
制定一个apk的路径 然后跳出界面让用户选择是否安装 我们系统有一个写好的Activity来协助我们完成这一功能 我们来看看它的清单文件 <?xml version="1.0" ...
- Swift:Minimizing Annotation with Type Inference
许多程序猿更喜欢比如Python和Javascript这样的动态语言,因为这些语言并不要求程序猿为每个变量声明和管理它们的类型. 在大多数动态类型的语言里,变量可以是任何类型,而类型声明是可选的或者根 ...
- Redis工作系列之一 与 Memcached对比理解
近期公司项目在使用Redis,这几年Redis很火,Redis也常常被当作Memcached的挑战者被提到桌面上来.关于Redis与Memcached的比较更是比比皆是.然而,Redis真的 ...