leetcode — search-in-rotated-sorted-array
/**
* 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的更多相关文章
- 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 ...
- LeetCode: Search in Rotated Sorted Array II 解题报告
Search in Rotated Sorted Array II Follow up for "LeetCode: Search in Rotated Sorted Array 解题报告& ...
- [LeetCode] Search in Rotated Sorted Array II 在旋转有序数组中搜索之二
Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would this ...
- [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 ...
- LeetCode——Search in Rotated Sorted Array II
Follow up for "Search in Rotated Sorted Array": What if duplicates are allowed? Would this ...
- [leetcode]Search in Rotated Sorted Array II @ Python
原题地址:https://oj.leetcode.com/problems/search-in-rotated-sorted-array-ii/ 题意: Follow up for "Sea ...
- LeetCode: Search in Rotated Sorted Array 解题报告
Search in Rotated Sorted Array Suppose a sorted array is rotated at some pivot unknown to you before ...
- LeetCode Search in Rotated Sorted Array 在旋转了的数组中查找
Search in Rotated Sorted Array Suppose a sorted array is rotated at some pivot unknown to you before ...
- [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 ...
- [LeetCode] Search in Rotated Sorted Array II [36]
称号 Follow up for "Search in Rotated Sorted Array": What if duplicates are allowed? Would t ...
随机推荐
- Java实现AES加密,异常java.security.InvalidKeyException: Illegal key size 的解决
Java实现AES加密,抛出异常如下:java.security.InvalidKeyException: Illegal key size 代码参考 http://my.oschina.net/Ja ...
- 学习Acegi应用到实际项目中(7)- 缓存用户信息
在默认情况下,即在用户未提供自身配置文件ehcache.xml或ehcache-failsafe.xml时,EhCache会依据其自身Jar存档包含的ehcache-failsafe.xml文件所定制 ...
- go mysql insert变量到数据库
result, err1 := db.Exec("insert ignore into dish(name,calorie,confidence) values('"+str1+& ...
- 大数据项目测试<二>项目的测试工作
大数据的测试工作: 1.模块的单独测试 2.模块间的联调测试 3.系统的性能测试:内存泄露.磁盘占用.计算效率 4.数据验证(核心) 下面对各个模块的测试工作进行单独讲解. 0. 功能测试 1. 性能 ...
- 基于接口的 InvocationHandler 动态代理(换种写法)
InvocationHandler is the interface implemented by the invocation handler of a proxy instance. Each p ...
- numpy 库简单使用
numpy 库简单使用 一.numpy库简介 Python标准库中提供了一个array类型,用于保存数组类型的数据,然而这个类型不支持多维数据,不适合数值运算.作为Python的第三方库numpy便有 ...
- java面试问题收集(2)
1 Integer int相等问题 Integer对象和int比较的时候会有一个拆箱的过程,始终相等 Integer和new Integer对象不会相等,引用不同 两个Integer对象比较,Inte ...
- poj 1026
这题一开始没看清楚 等级差距不超过1 1->2->3 就是错误的,因为3-1==2 ,意思是间接的也不行 其次等级最小是1,最大是n 你要到达1号首领的位置 假设1号等级x,限制m,最大上 ...
- centos7.2安装图文详解
centos镜像下载地址 https://www.centos.org/download/ Install CentOS 7 ----直接安装Test this media & instal ...
- 项目设计day1
项目内容:一个实时监控斗鱼TV某个主播弹幕的设计 通过python爬虫获取当前弹幕,通过flume采集数据,接下来数据分为线上和线下两种方案: 线上:实时分析,分为两种方案:(1) flume+kaf ...