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 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 Solution {
public int search(int[] nums, int target) {
if(nums==null || nums.length==0){
return -1;
}
int low=0;
int high=nums.length-1; while(low<=high){
int mid=low+(high-low)/2;
if(target<nums[mid]){
if(nums[mid] < nums[high]){//right side is sorted
high=mid-1;//target must in the left side
}
else{
if(target<nums[low]){//target<nums[mid]&&target<nums[low]==>means,target cannot be in [low,mid] since this side is sorted
low=mid+1;
}
else{
high=mid-1;
}
}
}
else if(target>nums[mid]){ if(nums[mid]> nums[low]){//left side is sorted
low=mid+1;
}
else{
if(target>nums[high]){//target>nums[mid] && target>nums[high]==>means,target cannot be in [mid, high] since this side is sorted
high=mid-1;
}
else{
low=mid+1;
}
}
}
else{
return mid;
}
}
return -1;
}
}
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 ...
随机推荐
- Ubuntu下freeradius-server的安装与mysql-server的关联
1.创建freeradius数据库 #service mysql start ... #mysql -u root -p Enter password: 456456 ... mysql> cr ...
- matlab初学之roundn和round
文章出处: http://evaevazhuxun.blog.sohu.com/154543859.html http://blog.sina.com.cn/s/blog_a4034b2801012o ...
- 关于mybatis一对多关联时
一对多关联时注:collection标签,property属性名称,column参数字段, ofType查询返回类型,select查询方法,javaType方法的返回类型
- 面试题12:打印1到最大的n位数
// 面试题12_打印1到最大的n位数.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include <iostream> ...
- JSP 甜点
JSP cookies Cookies是存储在客户机的文本文件,它们保存了大量轨迹信息.在servlet技术基础上,JSP显然能够提供对HTTP cookies的支持. 通常有三个步骤来识别回头客: ...
- (spring-第20回【AOP基础篇】)Spring与事务
要想了解Spring的事务,首先要了解数据库事务的基本知识,数据库并发会产生很多问题,Spring使用ThreadLocal技术来处理这些问题,那么我们必须了解Java的ThreadLocal技术.下 ...
- UIKit框架之UIEvent
1.继承链:NSObject 2.事件大致可以分为三种事件:触摸事件.动作事件.遥控事件 3.获取事件的touches (1)- (NSSet<UITouch *> *)allTouche ...
- 写简单游戏,学编程语言-python篇
好吧, 首先得承认这个题目写的夸大了,人才菜鸟一枚,游戏相关编程也是知道点概念.但是本人对游戏开发比较感兴趣,相信大多数喜欢玩玩游戏,因为它给人确实带来很多乐趣,而编程语言的学习最少对于我来说比较乏味 ...
- 【转】javascript变量声明 及作用域
javascript变量声明提升(hoisting) javascript的变量声明具有hoisting机制,JavaScript引擎在执行的时候,会把所有变量的声明都提升到当前作用域的最前面. 先看 ...
- 在Android Studio和Android Eclipse 更改现有项目里的SDK版本
一,在Eclipse下改项目里的SDK的版本方法有几种,都比较简单:1.右键单击项目--->properties---->Resource----->Android在Project ...