[leetcode33Search in Rotated Sorted Array]在排序旋转后序列中找目标值
直接上代码
/**
* Created by lvhao on 2017/6/30.
* 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.
思路是目标值肯定在一个升序子序列中(可能子序列很短,但是肯定存在),目标就是找到这个子序列然后用二分法找到值
找目标序列的时候也是用二分法,判断的依据是mid值比fin值大的话说明左边是升序,小的话,右边是升序,然后在利用升序序列的的
首尾值判断目标值是否在里边,在里边就用二分法找值,不在里边就改变fin或者sta值,继续循环找,注意二分法的时候while的判断
条件是sta<= fin,如果没有=的话,两个数的情况判断不出来。
*/
public class Q33SearchRotatedArray {
public static void main(String[] args) {
int[] nums = new int[]{3,1};
int target = 1;
int res = search(nums,target);
System.out.println(res); }
public static int search(int[] nums, int target) {
int len = nums.length;
//判断特殊情况
if(len == 1 && nums[0] == target)
{
return 0;
}
int sta = 0,fin = len-1,mid,index = -1;
//注意等号
while(sta <= fin)
{
mid = (sta + fin)/2;
if(nums[mid] < nums[fin])
{
if(target <= nums[fin] && target >= nums[mid])
{
index = binarySearch(nums,mid,fin,target);
//找到值之后立即break
break;
} else
fin = mid -1;
}
else
{
if(target <= nums[mid] && target >= nums[sta])
{
index = binarySearch(nums,sta,mid,target);
break;
} else
sta = mid + 1;
}
}
return index;
}
public static int binarySearch(int[] nums,int sta,int fin,int target)
{
int mid,index = -1;
//注意等号
while (sta <= fin)
{
mid = (sta + fin)/2;
if(target == nums[mid])
{
index = mid;
break;
}
else if (target < nums[mid])
fin = mid-1;
else
sta = mid+1;
}
return index;
}
}
[leetcode33Search in Rotated Sorted Array]在排序旋转后序列中找目标值的更多相关文章
- LeetCode 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] Search in Rotated Sorted Array II 在旋转有序数组中搜索之二
Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would this ...
- [LeetCode] 81. Search in Rotated Sorted Array II 在旋转有序数组中搜索之二
Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. (i.e. ...
- [LeetCode] 81. Search in Rotated Sorted Array II 在旋转有序数组中搜索 II
Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would this ...
- 28.earch in Rotated Sorted Array(排序旋转数组中查找)
Level: Medium 题目描述: Suppose an array sorted in ascending order is rotated at some pivot unknown to ...
- 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] 154. Find Minimum in Rotated Sorted Array II 寻找旋转有序数组的最小值 II
Follow up for "Find Minimum in Rotated Sorted Array":What if duplicates are allowed? Would ...
- 【LeetCode-面试算法经典-Java实现】【033-Search in Rotated Sorted Array(在旋转数组中搜索)】
[033-Search in Rotated Sorted Array(在旋转数组中搜索)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Suppose a sort ...
- [LeetCode] Find Minimum in Rotated Sorted Array II 寻找旋转有序数组的最小值之二
Follow up for "Find Minimum in Rotated Sorted Array":What if duplicates are allowed? Would ...
随机推荐
- 生成微博授权URL及回调地址
1.创建apps/oauth模块进行oauth认证 '''2.1 在apps文件夹下新建应用: oauth''' cd syl/apps python ../manage.py startapp oa ...
- SpringBoot集成FastDFS依赖实现文件上传
前言 对FastDFS文件系统安装后的使用. FastDFS的安装请参考这篇:Docker中搭建FastDFS文件系统(多图) 本文环境:IDEA + JDK1.8 + Maven 本文项目代码:ht ...
- DockerFile理解与应用
1.DockerFile是什么? DockerFile是用来构建Docker镜像的构建文件,一般分为四部分:基础镜像信息.维护者信息.镜像操作指令和容器启动时执行指令,'#' 为 Dockerfile ...
- Qt模型视图结构遇见的小问题
在本文的最开始,我们来看两个帮助文档内容: selectionMode : SelectionMode This property holds which selection mode the vie ...
- 老猿学5G扫盲贴:移动边缘计算(Mobile Edge Computing, MEC)
版权声明:本文为CSDN博主「魏晓蕾」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明. 原文链接:https://blog.csdn.net/gongxifaca ...
- 第11.5节 Python正则表达式搜索任意字符匹配及元字符“.”(点)功能介绍
在re模块中,任意字符匹配使用"."(点)来表示, 在默认模式下,点匹配除了换行的任意字符.如果指定了搜索标记re.DOTALL ,它将匹配包括换行符的任意字符.关于搜索标记的含义 ...
- 第11.24节 Python 中re模块的其他函数
一. re.compile函数 正则表达式编译函数,在后面章节专门介绍. 二. re.escape(pattern) re.escape是一个工具函数,用于对字符串pattern中所有可能被视为正则表 ...
- PyQt(Python+Qt)学习随笔:信号签名中的万能Python类型PyQt_PyObject
老猿Python博文目录 专栏:使用PyQt开发图形界面Python应用 老猿Python博客地址 信号在定义时参数如果使用字符串'PyQt_PyObject'表示参数可以是任何Python类型,这通 ...
- CF1320 Div1 D.Reachable Strings 题解
题目大意 给定一个长为\(n\)的01串\(S\),每次你可以对一个串的三个连续位置做:\(011 \rightarrow 110\),\(110 \rightarrow 011\)的操作. 有\(q ...
- 使用pip安装pymysql出错;Could not find a version that satisfies the requirement cryptography (from pymysql) (from versions: ) No matching distribution found for cryptography (from pymysql)
今天使用pip安装pymysql时出现如下错误: Could not find a version that satisfies the requirement cryptography (from ...