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.
ps:这题和剑指offter的题目类似。一次AC
思路,由于没有重复数字,
1.我们先找出最大值在哪个位置。
2.然后拿target和A[0]做比较,若target>A[0],则在0~max_loc之间来二分查找数组;若target<A[0],则在max_loc+1~n-1来查找数组。
代码:
class Solution {
public:
int binarySearch(int start,int end,int A[],int target){
int l=start;int r=end;int mid=;
while(l<=r){
mid=(l+r)/;
if(A[mid]>target) r=mid-;
else if(A[mid]<target) l=mid+;
else return mid;
}
return -;
}
int findMaxValueLoction(int A[],int n){
if(A[n-]>A[]) return n-;
int l=;int r=n-;int mid=;
while(l<=r){
mid=(l+r)/;
if(mid==) return mid;
else if(mid!=&&A[mid]>A[]&&A[mid]>A[mid-]&&A[mid]<A[mid+]){
l=mid+;
}
else if(mid!=&&A[mid]<A[]&&A[mid]>A[mid-]&&A[mid]<A[mid+]){
r=mid-;
}
else if(mid!=&&A[mid]>A[mid-]&&A[mid]>A[mid+]){
return mid;
}else{
return mid-;
}
}
return -;
}
int search(int A[], int n, int target) {
if(A==NULL||n==) return -;
int max_loc=findMaxValueLoction(A,n);
if(target>A[])
return binarySearch(,max_loc,A,target);
else if(target<A[])
return binarySearch(max_loc+,n-,A,target);
else
return ;
}
};
Search in Rotated Sorted Array(二分查找)的更多相关文章
- 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 153. Find Minimum in Rotated Sorted Array -- 二分查找的变种
Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. (i.e. ...
- leetcode 二分查找 Search in Rotated Sorted Array
Search in Rotated Sorted Array Total Accepted: 28132 Total Submissions: 98526My Submissions Suppose ...
- leetcode 题解:Search in Rotated Sorted Array II (旋转已排序数组查找2)
题目: Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would t ...
- 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 Search in Rotated Sorted Array 在旋转了的数组中查找
Search in Rotated Sorted Array Suppose a sorted array is rotated at some pivot unknown to you before ...
- LeetCode OJ:Search in Rotated Sorted Array II(翻转排序数组的查找)
Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would this ...
- 49. Search in Rotated Sorted Array && Search in Rotated Sorted Array II
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 II
LeetCode:Search in Rotated Sorted Array Suppose a sorted array is rotated at some pivot unknown to y ...
- Search in Rotated Sorted Array I II
Search in Rotated Sorted Array Suppose a sorted array is rotated at some pivot unknown to you before ...
随机推荐
- pocket API学习笔记
最近安装了pocket离线阅读软件. 为了收藏需要的URL,每次都要打开浏览器.然后按google工具条上的pocket+. 网页多的时候,这个过程就非常缓慢. 根据pocket网站的API介绍,我可 ...
- Dockerfile 中的 CMD和ENTRYPOINT 两兄弟
CMD 先说老大 CMD 当一个容器准备好运行之后,需要找一个指定命令来创建一个初始进程并运行. 一,/bin/sh -c 因为某种意义上一个Dockerfile其实可以理解是一个简化版bash 脚本 ...
- C#飞行棋总结
以下是掷色子的一个代码,比较有代表性,里面的逻辑和内容都已注释,可通过注释了解这一方法的运作模式. public static void RowTouZi(int playerPos) //掷色子 { ...
- docker的网络配置
Docker的4种网络模式 我们在使用docker run创建Docker容器时,可以用–net选项指定容器的网络模式,Docker有以下4种网络模式: host模式:使用–net=host指定. c ...
- 旅行商问题——状态压缩DP
问题简介 有n个城市,每个城市间均有道路,一个推销员要从某个城市出发,到其余的n-1个城市一次且仅且一次,然后回到再回到出发点.问销售员应如何经过这些城市是他所走的路线最短? 用图论的语言描述就是:给 ...
- dumpkeys - 转储显示键盘翻译表
总览 (SYNOPSIS) dumpkeys [ -hilfn1 -Sshape -ccharset --help --short-info --long-info --numeric --full- ...
- 在CNN网络中roi从原图映射到feature map中的计算方法
在使用fast rcnn以及faster rcnn做检测任务的时候,涉及到从图像的roi区域到feature map中roi的映射,然后再进行roi_pooling之类的操作.比如图像的大小是(600 ...
- vue+Java 前后端分离,多次请求Session不一致的问题(网络上找的)
在vue main.js中增加以下配置: import axios from 'axios'; axios.defaults.withCredentials=true; 请求时:设置 withCred ...
- JavaEE-01 JSP动态网页基础
学习要点 B/S架构的基本概念 Web项目的创建和运行 JSP页面元素 MyEclipse创建和运行Web项目 Web程序调试 Web简史 web前端技术演进三阶段 WEB 1.0:信息广播. WEB ...
- spring mvc poi excel
Util类 package com.common.util; public class ExportUtil { private XSSFWorkbook wb = null; private XSS ...