【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 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.
与Search in Rotated Sorted Array II,Find Minimum in Rotated Sorted Array,Find Minimum in Rotated Sorted Array II对照看
解法一:顺序查找 just a joke :D
class Solution {
public:
int search(int A[], int n, int target) {
for(int i = ; i < n; i ++)
{
if(A[i] == target)
return i;
}
return -;
}
};

解法二:二分查找
先用二分法找到最大元素,将数组切分成两个有序数组,再进行二分查找
class Solution {
public:
int search(int A[], int n, int target) {
if(n==)
return (target==A[])?:-;
//find the maximum first
int low = ;
int high = n-;
while(low < high)
{
int mid = (low+high)/;
if(A[mid] < A[low])
high = mid-;
else if(A[mid] > A[low])
low = mid;
else
{//low+1==high
if(A[high]>A[low])
low = high;
break;
}
}
int ind = low;
//to here, low is the index of maximum
//0~ind, ind+1~n-1 are two sorted arrays
if(target >= A[])
{//first array: 0~ind
low = ;
high = ind;
while(low <= high)
{
int mid = (low+high)/;
if(target == A[mid])
return mid;
else if(target > A[mid])
low = mid+;
else
high = mid-;
}
return -;
}
else
{//second array: ind+1, n-1
low = ind+;
high = n-;
while(low <= high)
{
int mid = (low+high)/;
if(target == A[mid])
return mid;
else if(target > A[mid])
low = mid+;
else
high = mid-;
}
return -;
}
}
};

解法三:可处理重复元素的二分查找,即不断去掉low与high元素
class Solution {
public:
int search(int A[], int n, int target) {
int low = ;
int high = n-;
while (low <= high)
{
int mid = (low+high)/;
if(A[mid] == target)
return mid;
if (A[low] < A[mid])
{
if(A[low] <= target && target < A[mid])
//binary search in sorted A[low~mid-1]
high = mid - ;
else
//subproblem from low to high
low = mid + ;
}
else if(A[mid] < A[high])
{
if(A[mid] < target && target <= A[high])
//binary search in sorted A[mid+1~high]
low = mid + ;
else
//subproblem from low to mid-1
high = mid - ;
}
else if(A[low] == A[mid])
low += ; //A[low]==A[mid] is not the target, so remove it
else if(A[mid] == A[high])
high -= ; //A[high]==A[mid] is not the target, so remove it
}
return -;
}
};

解法四:
二分查找,先对mid元素处于前半段还是后半段分情况讨论,再对target元素处于前半段还是后半段分情况讨论。
class Solution {
public:
int search(int A[], int n, int target) {
return search(A, , n-, target);
}
int search(int A[], int left, int right, int target)
{
if(left > right)
return -;
if(A[left] < A[right])
// one part, binary search
return binarySearch(A, left, right, target);
// else, two part
int mid = left + (right-left) / ; //prevent overflow
if(A[mid] == target)
return mid;
else if(A[left] > A[mid])
{// mid is in second part
if(target > A[mid])
{// target may be in the first part (case1), or second part after mid(case2)
if(target == A[left])
return left;
else if(target > A[left])
{// case1
return search(A, left, mid-, target);
}
else
{// case2
return search(A, mid+, right, target);
}
}
else
{// target is in the second part before mid
return search(A, left, mid-, target);
}
}
else
{// mid is in first part
if(target > A[mid])
{// target is in first part after mid
return search(A, mid+, right, target);
}
else
{// target may be in the first part before mid (case1), or second part
if(target == A[left])
return left;
else if(target > A[left])
{// case1
return search(A, left, mid-, target);
}
else
{// case2
return search(A, mid+, right, target);
}
}
}
}
int binarySearch(int A[], int left, int right, int target)
{
while(left <= right)
{
int mid = left + (right-left) / ; //prevent overflow
if(A[mid] == target)
return mid;
else if(A[mid] > target)
right = mid - ;
else
left = mid + ;
}
return -;
}
};

【LeetCode】33. Search in Rotated Sorted Array (4 solutions)的更多相关文章
- 【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】81. Search in Rotated Sorted Array II 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/search-in ...
- 【Leetcode】81. Search in Rotated Sorted Array II
Question: Follow up for "Search in Rotated Sorted Array": What if duplicates are allowed? ...
- 【LeetCode】81. Search in Rotated Sorted Array II (2 solutions)
Search in Rotated Sorted Array II Follow up for "Search in Rotated Sorted Array":What if d ...
- 【LeetCode】081. Search in Rotated Sorted Array II
题目: Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would t ...
- 【一天一道LeetCode】#33. Search in Rotated Sorted Array
一天一道LeetCode 本系列文章已全部上传至我的github,地址: https://github.com/Zeecoders/LeetCode 欢迎转载,转载请注明出处 (一)题目 Suppos ...
- 【LeetCode】033. Search in Rotated Sorted Array
题目: Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. ( ...
- 【一天一道LeetCode】#81. Search in Rotated Sorted Array II
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Follow ...
随机推荐
- LINUX 高可用群集之 ~Corosync~
Corosync:它属于OpenAIS(开放式应用接口规范)中的一个项目corosync一版本中本身不具 备投票功能,到了corosync 2.0之后引入了votequorum子系统也具备了投票功能了 ...
- 模板引擎之hogan.js
hogan.js 语法简单,且支持循环数据: 基本语法: 1. 标签可以嵌套使用 2. {{data}} 转义的变量,不会渲染html标签 3. {{{data}}} 不转义的变量,会渲染html标签 ...
- Windows 配置 Apache Python CGI
提示:安装Apache可参考 https://jingyan.baidu.com/article/0eb457e53c019f03f1a905c7.html 1. 打开URL: https://ww ...
- System.Threading.Tasks并发和异步代码使用
main.cs System.Threading.Tasks.Parallel.For(0, 10, i => { TestLock test ...
- Git 学习(二)版本库创建
Git 版本库创建 什么是版本库(repository)? 可理解为文件仓库.由Git管理每个文件的新增.修改及删除,但这个仓库可以追溯历史.可还原至任意历史节点. 版本库创建 创建一个版本库非常简单 ...
- 在Ubuntu 13.04下的安装eclipse
来源:http://www.cnblogs.com/lanxuezaipiao/p/3325628.html 一.eclipse安装过程 首先确保在安装eclipse之前已经安装好Java虚拟机 1. ...
- 实现SQL Server中的切割字符串SplitString函数
有时我们要用到批量操作时都会对字符串进行拆分,可是SQL Server中却没有自带Split函数,所以要自己来实现了.没什么好说的,需要的朋友直接拿去用吧 SET ANSI_NULLS ON GO S ...
- MS SQL 中判断 数据库, 存储过程,表,临时表,视图,函数,用户,用户创建对象 等是否存在 SQL脚本
摘自: http://www.111cn.net/database/mssqlserver/39107.htm sql判断存储过程是否存在 判断数据库教程是否存在 Sql代码 if exists (s ...
- 每日一小练——Armstrong数
上得厅堂.下得厨房,写得代码,翻得围墙,欢迎来到睿不可挡的每日一小练! 题目:Armstrong数 内容: 在三位的正整数中,比如abc.有一些能够满足a^3+b^3+c^3=abc的条件,也就是说各 ...
- android4.0 USB Camera实例(三)UVC
前面我写了两篇文章说明了zc301的实现 详细请看 http://blog.csdn.net/hclydao/article/details/21235919 以下顺便把通用的USB也写上 前面的ZC ...