/**
* 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. ConcurrentDictionary

    ConcurrentDictionary ConcurrentDictionary一大特点是线程安全,在没有ConcurrentDictionary前 在多线程下用Dictionary,不管读写都要加 ...

  2. JSOI2018 简要题解

    潜入行动 复杂度分析题. 定义状态fi,j,0/1,0/1f_{i,j,0/1,0/1}fi,j,0/1,0/1​表示以iii为根子树放jjj个机器iii这个放不放,iii这个是否已放来进行dpdpd ...

  3. Linux环境(Centos7)下部署.NetCore2.0的Web应用

    Web应用基于Windows环境下开发,然后部署到Linux 1.进入VS2017,点击新建->项目->.NetCore->ASP.NET Core Web应用程序,确定 2.选择W ...

  4. Docker优势以及与传统虚拟机对比(1)

    docker优势 1.更快速地交付和部署: 2.更高的虚拟化(不需要额外的hypervisor支持,是内核级的虚拟化,实现更高的性能呢和效率): 3.更轻松的迁移和扩展: 4.更简单的管理  与传统的 ...

  5. 再探haproxy

    一 设置haproxy输出log 1.1 调整配置文件 默认haproxy是不会输出log到文件的,这样很大程度在查询问题时会很不方便,haproxy是可以输出日志到文件的,配置文档类似于如下: ]# ...

  6. [swarthmore cs75] inlab1 — Tiny Compiler

    课程回顾 Swarthmore学院16年开的编译系统课,总共10次大作业.本随笔记录了inlab1的实践过程. tiny compiler 这个迷你的编译器可以将一个源文件,编译成可执行的二进制代码. ...

  7. django中使用memcache的一些注意事项

    最近写django项目时在保存验证码方面要用到memcached,于是便查看了一些教程进行操作,结果确遇到了一系列问题,以下是一些容易遇到的雷区: 1.windows下memcached安装: -wi ...

  8. jasperreports实现pdf文档的生成

    1.导入jar包(pom.xml构建) <dependencies> <dependency> <groupId>com.lowagie</groupId&g ...

  9. spring配置问题

    产生原因缺少包common-logging-1.2.jar 在该字段所在的类中没有提供该字段的set方法.

  10. OOP随笔

    父类为普通类: 内部可声明虚方法(virtual),并包含代码实现,子类中可以重写(override),也可以不重写直接用. 父类为(不可实例化的)抽象类: 可声明虚方法,同上. 也可以声明抽象方法( ...