[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 ...
随机推荐
- Unity使用transform.Rotate进行三维旋转角度出现偏差
Unity使用transform.Rotate进行三维旋转角度出现偏差 情形 最开始遇到该问题的情况比较复杂,另写了一个例子.情形如下: 一个立方体上挂载脚本: transform.Rotate(25 ...
- 老猿Python博客文章目录索引
本目录提供老猿Python所有相关博文的一级目录汇总,带星号的为收费专栏: 一.专栏列表 本部分为老猿所有专栏的列表,每个专栏都有该专栏置顶的博文目录: 专栏:Python基础教程目录 专栏:* 使用 ...
- 老猿学5G随笔:RAN、RAT以及anchor移动性锚点的概念
最近在学习UPF的功能时,有这样一句话"用户平面功能(UPF)包括以下功能. 用于RAT内/ RAT间移动性的锚点(适用时)",这句话不理解,后来看到了<关于移动锚点的理解! ...
- PyQt(Python+Qt)学习随笔:Qt Designer中图像资源的使用及资源文件的管理
一.概述 在Qt Designer中要使用图片资源有三种方法:通过图像文件指定.通过资源文件指定.通过theme主题方式指定,对应的设置界面在需要指定图像的属性栏如windowIcon中通过点击属性设 ...
- PyQt(Python+Qt)学习随笔:Qt Designer中连接Action和槽函数
在Designer中试了半天,终于找到了Action添加槽函数的方法,操作步骤: 在Designer右边界面中点击鼠标右键 确保信号/槽编辑被勾选,如图是未勾选的情况:. 勾选后会出现信号和槽的编辑界 ...
- 推荐系统实践 0x0c FM系列
逻辑回归(LR) 在介绍FM系列之前,我想首先简单介绍一下逻辑回归.通常来说,逻辑回归模型能够综合利用更多的信息,如用户.物品.上下文等多种不同的特征,生成更为全面的结果.另外,逻辑回归将推荐问题看成 ...
- Redis数据库简介
最近的项目需要用到Redis数据库和MySQL,恶补学习. Redis的使用手册可以看: https://redis.io/ https://www.runoob.com/redis/redis-tu ...
- 极光实验室 第一次考核wp
第一道题: 上来就让我买flag,用御剑扫目录,发现了这道题有源码index.php.bak!直接下载. <meta charset='UTF-8'> <title>极光实验室 ...
- Leetcode学习笔记(3)
题目1 ID88 给你两个有序整数数组 nums1 和 nums2,请你将 nums2 合并到 nums1 中,使 num1 成为一个有序数组. 说明: 初始化 nums1 和 nums2 的元素数量 ...
- aspnetcore webapi 解决发布以后每隔一段时间请求变缓慢
项目:netcore webapi 3.1 平台:windows server 2008 r2 服务器:IIS 7.5 项目发布到IIS以后第一次请求特别慢大概7.8秒,然后每隔5分钟请求一次大概2. ...