[Leetcode] Search in Rotated Sorted Array 系列
Search in Rotated Sorted Array 系列题解
题目来源:
Search in Rotated Sorted Array
Search in Rotated Sorted Array II
第一版
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.
Solution
class Solution {
public:
int search(vector<int>& nums, int target) {
if (nums.empty()) return -1;
int size = nums.size();
int low = 0, high = size - 1, mid;
while (low < high) {
mid = (low + high) / 2;
if (nums[mid] > nums[high])
low = mid + 1;
else
high = mid;
}
if (low > 0 && target >= nums[0]) {
high = low - 1;
low = 0;
} else {
high = size - 1;
}
while (low <= high) {
mid = (low + high) / 2;
if (target == nums[mid])
return mid;
else if (target > nums[mid])
low = mid + 1;
else
high = mid - 1;
}
return -1;
}
};
解题描述
这道题题意是,给出一个数组,数组原本是升序的,但是被从中间某个位置截断,后面一段被放到前面,要求在数组中找到目标数字target
的位置。这里首选的做法肯定是二分查找,不过要先用二分查找找到截断的位置,然后根据target
与数组第一个元素的大小关系决定其所在半区,再进行二分查找。
进阶版本
Follow up for "Search in Rotated Sorted Array":
What if duplicates are allowed?
Would this affect the run-time complexity? How and why?
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
).
Write a function to determine if a given target is in the array.
The array may contain duplicates.
Solution
解法一:从左向右遍历数组,找到数组旋转的位置
class Solution {
public:
bool search(vector<int>& nums, int target) {
if (nums.empty()) return false;
int size = nums.size();
int low = 0, high = size - 1, mid;
for (; low < size; low++) {
if (low > 0 && nums[low - 1] > nums[low]) break;
}
if (low > 0 && target >= nums[0]) {
high = low - 1;
low = 0;
} else {
high = size - 1;
}
while (low <= high) {
mid = (low + high) / 2;
if (target == nums[mid])
return true;
else if (target > nums[mid])
low = mid + 1;
else
high = mid - 1;
}
return false;
}
};
解法二:使用二分查找,结合查重夹逼
class Solution {
public:
bool search(vector<int>& nums, int target) {
if (nums.empty()) return false;
int low = 0, high = nums.size() - 1, mid;
while (low <= high) {
mid = (low + high) / 2;
if (nums[mid] == target) return true;
if (nums[low] == nums[mid] && nums[mid] == nums[high]) {
++low;
--high;
} else if (nums[low] <= nums[mid]) {
if (nums[low] <= target && nums[mid] > target)
high = mid - 1;
else
low = mid + 1;
} else {
if (nums[mid] < target && nums[high] >= target)
low = mid + 1;
else
high = mid - 1;
}
}
return false;
}
};
解题描述
这道题题意是,给出一个排好序的数组,在数组中间某个位置截断,将后面一段放到前面,并且数组中可能存在重复的元素,然后要求判断数组中是否存在目标数字target
。上面给出了2种解法,第一种解法相对来说比较暴力,平均的时间复杂度为O(n / 2)。而第二种解法虽然在出现高低位游标元素重复的时候进行了线性移位,但是最优时间复杂度位O(log n),最差时间复杂度是O(n),相对来说还是有一定的优化。
[Leetcode] Search in Rotated Sorted Array 系列的更多相关文章
- 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 ...
- LeetCode: Search in Rotated Sorted Array II 解题报告
Search in Rotated Sorted Array II Follow up for "LeetCode: Search in Rotated Sorted Array 解题报告& ...
- [LeetCode] Search in Rotated Sorted Array II 在旋转有序数组中搜索之二
Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would this ...
- [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 ...
- LeetCode——Search in Rotated Sorted Array II
Follow up for "Search in Rotated Sorted Array": What if duplicates are allowed? Would this ...
- [leetcode]Search in Rotated Sorted Array II @ Python
原题地址:https://oj.leetcode.com/problems/search-in-rotated-sorted-array-ii/ 题意: Follow up for "Sea ...
- LeetCode: Search in Rotated Sorted Array 解题报告
Search in Rotated Sorted Array Suppose a sorted array is rotated at some pivot unknown to you before ...
- LeetCode Search in Rotated Sorted Array 在旋转了的数组中查找
Search in Rotated Sorted Array Suppose a sorted array is rotated at some pivot unknown to you before ...
- [LeetCode] Search in Rotated Sorted Array I (33) && II (81) 解题思路
33. Search in Rotated Sorted Array Suppose a sorted array is rotated at some pivot unknown to you be ...
随机推荐
- SQL优化套路
直奔主题: (1) SELECT(2) DISTINCT <SELECT_LIST>(3) FROM <LEFT_TABLE> <JOIN_TYPE> (4) JO ...
- 【.Net】net 反射15分钟速成
概述 什么是反射 Reflection,中文翻译为反射. 这是.Net中获取运行时类型信息的方式,.Net的应用程序由几个部分:‘程序集(Assembly)’.‘模块(Module)’. ...
- BZOJ 1212 L语言(DP+字典树)
求能被理解的最长前缀. 很显然的dp.令dp[i]=true,表示前缀i能理解.否则不能理解.那么dp[i+len]=dp[i]=true,当s[len]能匹配str[i,i+len]. 由于模式串长 ...
- 转---Android Audio System 之一:AudioTrack如何与AudioFlinger交换音频数据
引子 Android Framework的音频子系统中,每一个音频流对应着一个AudioTrack类的一个实例,每个AudioTrack会在创建时注册到 AudioFlinger中,由AudioFli ...
- C++解析(29):类型识别
0.目录 1.类型识别 2.动态类型识别 3.类型识别关键字 4.小结 1.类型识别 在面向对象中可能出现下面的情况: 基类指针指向子类对象 基类引用成为子类对象的别名 静态类型--变量(对象)自身的 ...
- Find the hotel HDU - 3193(RMQ)
题意: 有n个旅馆,从这n个旅馆中找出若干个旅馆,使得这若干个旅馆满足这样的条件:不能从其它和剩下的旅馆中找到一个价格和距离都小于这个旅馆的旅馆... 解析: 按price 排序,若price相同, ...
- 【转】大数据分析(Big Data OLAP)引擎Dremel, Tenzing 以及Impala
引自:http://blog.csdn.net/xhanfriend/article/details/8434896 对于数据分析师来说,SQL是主要的语言. Hive为Hadoop提供了支持SQL运 ...
- 【洛谷3674】小清新人渣的本愿(莫队,bitset)
[洛谷3674]小清新人渣的本愿(莫队,bitset) 题面 洛谷,自己去看去,太长了 题解 很显然的莫队. 但是怎么查询那几个询问. 对于询问乘积,显然可以暴力枚举因数(反正加起来也是\(O(n\s ...
- python基础---- __getattribute__----__str__,__repr__,__format__----__doc__----__module__和__class__
目录: 一. __getattribute__ 二.__str__,__repr__,__format__ 三.__doc__ 四.__module__和__class__ 一. __getattri ...
- @RequestBody 和@ResponseBody 注解详解
简介: @RequestBody 作用: i) 该注解用于读取Request请求的body部分数据,使用系统默认配置的HttpMessageConverter进行解析,然后把相应的数据绑定到要返回的对 ...