直接上代码

/**
* 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]在排序旋转后序列中找目标值的更多相关文章

  1. 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. ...

  2. [LeetCode] Search in Rotated Sorted Array II 在旋转有序数组中搜索之二

    Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would this ...

  3. [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. ...

  4. [LeetCode] 81. Search in Rotated Sorted Array II 在旋转有序数组中搜索 II

    Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would this ...

  5. 28.earch in Rotated Sorted Array(排序旋转数组中查找)

    Level:   Medium 题目描述: Suppose an array sorted in ascending order is rotated at some pivot unknown to ...

  6. 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. ...

  7. [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 ...

  8. 【LeetCode-面试算法经典-Java实现】【033-Search in Rotated Sorted Array(在旋转数组中搜索)】

    [033-Search in Rotated Sorted Array(在旋转数组中搜索)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Suppose a sort ...

  9. [LeetCode] Find Minimum in Rotated Sorted Array II 寻找旋转有序数组的最小值之二

    Follow up for "Find Minimum in Rotated Sorted Array":What if duplicates are allowed? Would ...

随机推荐

  1. 【译】理解Rust中的局部移动

    原文标题:Understanding Partial Moves in Rust 原文链接:https://whileydave.com/2020/11/30/understanding-partia ...

  2. Ubuntu\Linux 下编写及调试C\C++

    一.在Ubuntu\Linux 下编写及调试C\C++需要配置基本的环境,即配置gcc编译器.安装vim编译器,具体配置安装步骤我在这里就不多说了. 二.基本环境配置完了我们就可以进入自己的程序编写了 ...

  3. DBeaver连接达梦|虚谷|人大金仓等国产数据库

    前言 工作中有些项目可能会接触到「达梦.虚谷.人大金仓」等国产数据库,但通常这些数据库自带的连接工具使用并不方便,所以这篇文章记录一下 DBeaver 连接国产数据库的通用模版,下文以达梦为例(其他国 ...

  4. .Net Core ABP应用如何在阿里云Linux Docker中开启Https

    .Net Core应用开启Https本身就有很多种方式:1.代码配置2.环境变量3.反向代理 这里主要记录下阿里云的ECS,加阿里云免费的SSL证书,通过程序代码,如何进行配置. 首先从阿里云下载证书 ...

  5. js onreadystatechange 和 onload的区别

    IE的script 元素只支持onreadystatechange事件,不支持onload事件. FF的script 元素不支持onreadystatechange事件,只支持onload事件. 如果 ...

  6. mvvm和mvc区别?

    mvc和mvvm其实区别并不大.都是一种设计思想.主要就是mvc中Controller演变成mvvm中的viewModel. mvvm主要解决了mvc中大量的DOM 操作使页面渲染性能降低,加载速度变 ...

  7. 题解-Bombs

    题解-Bombs 前置知识: 线段树 \(\color{orange}{\texttt{Bombs on luogu}}\) / \(\color{orange}{\texttt{Bombs on C ...

  8. 题解-洛谷P4139 上帝与集合的正确用法

    上帝与集合的正确用法 \(T\) 组数据,每次给定 \(p\),求 \[\left(2^{\left(2^{\left(2^{\cdots}\right)}\right)}\right)\bmod p ...

  9. MySQL技术内幕InnoDB存储引擎(一)——MySQL体系结构和存储引擎

    1.数据库和实例 数据库(database)和实例(instance)不能混淆. 什么是数据库 数据库是物理操作系统文件或其他文件类型的集合.说白了,就是存储着的文件,不会运行起来,只能被实例增删改查 ...

  10. JDK下载与安装

    Java有很多个版本,最新的版本会兼容之前的. 先附上下载地址:https://www.oracle.com/technetwork/java/javase/downloads/jdk8-downlo ...