/**
* Source : https://oj.leetcode.com/problems/search-in-rotated-sorted-array/
*
* Created by lverpeng on 2017/7/13.
*
* 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.
*
*/
public class SearchInRotatedSortedArray { /**
* 有序数组以某一个支点被翻转过,在数组中查找某一个元素
*
* 数组是局部有序的,使用二分查找的过程中使用这个特点
*
* @param num
* @return
*/
public int search (int[] num, int target) {
int left = 0;
int right = num.length - 1;
int mid = 0;
while (left <= right) {
mid = (left + right) / 2;
if (num[mid] == target) {
return mid;
}
// left- mid之间是局部有序的
if (num[left] <= num[mid]) {
if (num[left] <= target && target < num[mid]) {
right = mid - 1;
} else {
left = mid + 1;
}
} else {
// mid - right是有序的
if (mid < target && target <= num[right]) {
left = mid + 1;
} else {
right = mid - 1;
}
}
}
return -1;
} public static void main(String[] args) {
SearchInRotatedSortedArray searchInRotatedSortedArray = new SearchInRotatedSortedArray();
int[] arr = new int[]{4, 5, 6, 7, 0, 1, 2};
System.out.println(searchInRotatedSortedArray.search(arr, 0));
System.out.println(searchInRotatedSortedArray.search(arr, 5));
System.out.println(searchInRotatedSortedArray.search(arr, 7));
}
}

leetcode — search-in-rotated-sorted-array的更多相关文章

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

  2. LeetCode: Search in Rotated Sorted Array II 解题报告

    Search in Rotated Sorted Array II Follow up for "LeetCode: Search in Rotated Sorted Array 解题报告& ...

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

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

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

  5. LeetCode——Search in Rotated Sorted Array II

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

  6. [leetcode]Search in Rotated Sorted Array II @ Python

    原题地址:https://oj.leetcode.com/problems/search-in-rotated-sorted-array-ii/ 题意: Follow up for "Sea ...

  7. LeetCode: Search in Rotated Sorted Array 解题报告

    Search in Rotated Sorted Array Suppose a sorted array is rotated at some pivot unknown to you before ...

  8. LeetCode Search in Rotated Sorted Array 在旋转了的数组中查找

    Search in Rotated Sorted Array Suppose a sorted array is rotated at some pivot unknown to you before ...

  9. [LeetCode] Search in Rotated Sorted Array I (33) && II (81) 解题思路

    33. Search in Rotated Sorted Array Suppose a sorted array is rotated at some pivot unknown to you be ...

  10. [LeetCode] Search in Rotated Sorted Array II [36]

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

随机推荐

  1. javafx安装

    可在官网http://efxclipse.bestsolution.at/ 下载 其中http://efxclipse.bestsolution.at/install.html#all-in-one ...

  2. 第三次OO总结

    规格化设计的调研 随着50年代高级语言的出现,编译技术不断完善,涌现出多种流派的语言,其中就有里程碑式的Pascal语言:进入70年代,由于众多语言造成的不可移植.难于维护,Ada程序设计语言诞生了, ...

  3. vi中删除所有查找到的行

    vi中删除所有查找到的行 在linux中查找文件,结果中有很多是.svn目录里的,把查找结果放到一个文件里. 用vi打开该文件,按ESC,进入命令行模式,输入 :g/\.svn/d 就可以把所有含”. ...

  4. python计时器类

    import time as t class MyTimer(): def __init__(self): self.unit = ['年', '月', '日', '时', '分', '秒'] sel ...

  5. hdu 1086 You can Solve a Geometry Problem too [线段相交]

    题目:给出一些线段,判断有几个交点. 问题:如何判断两条线段是否相交? 向量叉乘(行列式计算):向量a(x1,y1),向量b(x2,y2): 首先我们要明白一个定理:向量a×向量b(×为向量叉乘),若 ...

  6. CSU1333最短路问题SPFA

    fastvj.rainng.com/contest/236779#problem/I Description: n个点m条路每条路 l,r,t:表示这条路开l秒,关r秒,通过要t秒,问你车辆从s到t最 ...

  7. 常用的头文件—— common.h

    #include <stdio.h> #include <stdlib.h> #include <sys/types.h> #include <sys/sta ...

  8. 00SQL表字段说明

    SELECT d.name 表名 , a.colorder 字段序号 , a.name 字段名 , ISNULL(g.[value], '') AS 字段说明 , ( CASE WHEN COLUMN ...

  9. 一、Java和JavaScript

    JavaScript诞生于1995年,所以他得叫我一声姐姐,(*^__^*) .当时它的主要任务就是表单验证,在还没JavaScript的时候,进行表单验证的时候必须要把数据提交到服务器,才能进行表单 ...

  10. Oracle Data Guard配置

    Oracle Data Guard 的配置在网上有很多资料,但是没有一个完整的,配置下来多少有些问题.在踩了各种坑之后,自己终于配置成功,就想把这过程记录下来. 1   测试环境 主数据库:windo ...