[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 ...
随机推荐
- [LeetCode] Search in Rotated Array II
Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would this ...
- java学习 猜数字
package study; import java.util.Scanner; /** * 猜数字小游戏 * * @author carry * */ public class GuessNumbe ...
- 第209天:jQuery运动框架封装(二)
运动框架 一.函数------单物体运动框架封装 1.基于时间的运动原理 动画时间进程 动画距离进程 图解: 物体从0移动到400 当物体移动到200的时候 走了50% 同样的,物体总共运行需要4秒 ...
- ubuntu16.04上安装配置DHCP服务的详细过程
DHCP服务器是为客户端机器分配IP地址的,所有分配的IP地址都保存在DHCP服务器的数据库中.为了在子网中实现DHCP分配IP地址,需要在目标主机上安装配置DHCP服务 1. 安装DHCP服务 安装 ...
- 快速傅里叶变换FFT / NTT
目录 FFT 系数表示法 点值表示法 复数 DFT(离散傅里叶变换) 单位根的性质 FFT(快速傅里叶变换) IFFT(快速傅里叶逆变换) NTT 阶 原根 扩展知识 FFT 参考blog: 十分简明 ...
- HDU.1166 敌兵布阵 (线段树 单点更新 区间查询)
HDU.1166 敌兵布阵 (线段树 单点更新 区间查询) 题意分析 加深理解,重写一遍 代码总览 #include <bits/stdc++.h> #define nmax 100000 ...
- 循环神经网络 RNN
随着科学技术的发展以及硬件计算能力的大幅提升,人工智能已经从几十年的幕后工作一下子跃入人们眼帘.人工智能的背后源自于大数据.高性能的硬件与优秀的算法的支持.2016年,深度学习已成为Google搜索的 ...
- vs下取得资源文件中的版本信息
在Windows Mobile和Wince(Windows Embedded CE)下开发的产品,有时候需要显示当前产品的版本信息.一般来说,版本信息是保存在资源文件里面的,例如下图: 为了保持一致, ...
- HDU--1874
原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=1874 分析:SPFA|Dijkastra. #include<iostream> #inc ...
- lnmp集成环境Access Denied的问题
在你的php.ini配置文件中,设置cgi.fix_pathinfo=1