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 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.
三个招:前两个很简单,巧妙地是第三个。
//方法一:分批次二分查找 ;查找出分割点需要时间O(n),总时间复杂度 O(n)
int BinaryResearch(int A[],int low,int high,int target)
{
int l = low;
int h = high;
while(l<=h)
{
int mid = (int)((h+l)/2);
if(A[mid]==target) return mid;
else if(A[mid]<target) l = mid+1;
else h = mid-1;
}
return A[h];
}
int search1(int A[], int n, int target) { int index = 0;
for(int i = 0;i<n-1;i++)
{
if(A[i]>A[i+1])
{
index = i;
break;
}
}
int a = BinaryResearch(A,0,index,target);
int b = BinaryResearch(A,index+1,n-1,target);
if(a==-1&&b==-1)
return -1;
else
return a==-1?b:a;
}
int search2(int A[], int n, int target) {
//顺序查找 ,O(n)
int index = -1;
for(int i = 0;i<n;i++)
{
if(A[i]==target)
{
index = i;
}}
return index;
}
//完全的二分查找,O(logn)
int search3(int A[], int n, int target) {
int left = 0;
int right = n-1;
while(left<=right)
{
int mid = (int)((left + right)/2);
if(A[mid] == target) return mid;
if(A[left]<A[mid])//A[mid]在前半部分
{
if(target<A[mid]&&target>=A[left])
right = mid-1;
else left = mid+1;
}
else if(A[left]>A[mid])//A[mid]位于后半段
{
if(target>A[mid]&&target<=A[right])
left = mid+1;
else
right = mid-1;
}
else left++;
}
return -1; }
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] Find Minimum in Rotated Sorted Array II 寻找旋转有序数组的最小值之二
		
Follow up for "Find Minimum in Rotated Sorted Array":What if duplicates are allowed? Would ...
 - [LeetCode] Find Minimum 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 在旋转有序数组中搜索
		
Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 migh ...
 - 【leetcode】Find Minimum in Rotated Sorted Array I&&II
		
题目概述: Suppose a sorted array is rotated at some pivot unknown to you beforehand.(i.e., 0 1 2 4 5 6 7 ...
 - LintCode Find Minimum In Rotated Sorted Array
		
1. 画图, 直观. 2. 讨论数组为空或者个数为零. 3. 讨论首尾, 若为翻转过的则进行查找直到最后两个数进行比较, 取小者. public class Solution { /** * @par ...
 - LeetCode-Search in Rotated Sorted Array II
		
Follow up for "Search in Rotated Sorted Array": What if duplicates are allowed? Would this ...
 - Leetcode Find Minimum in Rotated Sorted Array I and II
		
Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 migh ...
 
随机推荐
- Flashtext 使用文档 大规模数据清洗的利器-实现文本结构化
			
1.1 安装 pip install flashtext 1.2 使用例子 1.2.1 关键字提取 >>> from flashtext import KeywordProcesso ...
 - 用log4j将日志写入数据库
			
以下为log4j中的配置参数: %m 输出代码中指定的消息 %p 输出优先级,即DEBUG,INFO,WARN,ERROR,FATAL %r 输出自应用启动到输出该log信息耗费的毫秒数 %t 输出产 ...
 - WPF集合
			
Dependency Property 依赖属性 http://www.cnblogs.com/HelloMyWorld/archive/2013/02/21/2920149.html Attache ...
 - CSS——你所不知的 CSS ::before 和 ::after 伪元素用法(转)
			
你所不知的 CSS ::before 和 ::after 伪元素用法 CSS 有两个说不上常用的伪类 :before 和 :after,偶尔会被人用来添加些自定义格式什么的,但是它们的功用不仅于此.前 ...
 - Redis常用命令解析——INFO, MONITOR, SLOWLOG
			
1. INFO info指令返回服务器相关信息,包括: server: General information about the Redis server clients: Client conne ...
 - js实现div的置底
			
//-------------置底的div---------------------- <div class="mui-content lv-mrcd" id=" ...
 - 如何从CentOS官网下载我们想要的版本
			
今天想从官网下载6.5版本的CentOS,结果找了好一会儿才找到,赶紧记录下来,以备以后查询. 第一步在百度搜索centos,点击"Download CentOS",如下图所示. ...
 - [浪风分享] PHP开发必看 我现在是这样编程的
			
我在做什么 曾经,我试过接到一些需求.一眼带过后,脑袋马上随着高昂的斗志沉溺在代码的世界中 ,马不停蹄地敲着键盘直到最后测试的完成.我从思绪中恢复过来,乍一看自己写的功能,和需求差了十万八千里,我TM ...
 - noip2014滚粗记
			
滚粗了..伤心. day0:和baba一起去,但是整天都是下雨啊好不爽,鞋子都湿了啊好不爽,注定是要滚粗?在火车站等了1h后上动车走人...在此期间我还天真的认为火车站的wifi可以被我给破解然后上网 ...
 - RabbitMQ OS X下安装及常用命令-1
			
RabbitMQ的主页在http://www.rabbitmq.com/ . 1. 安装Erlang RabbitMQ是用Erlang编写的,所以需要先安装Erlang,如果有的话跳过 ...