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的更多相关文章

  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. AX多线程编译

    1.在命令行里先定位到AOS sever的BIN文件夹下(CD "AOS sever的BIN路径") CD C:\Program Files\Microsoft Dynamics ...

  2. 浅谈 Struts2 面试题收藏

    Struts2面试题 一.工作原理 一个请求在Struts2框架中的处理大概分为以下几个步骤 1 客户端初始化一个指向Servlet容器(例如Tomcat)的请求 2 这个请求经过一系列的过滤器(Fi ...

  3. cocostudio 骨骼动画 setContentScaleFactor

    最近在看骨骼动画,看上去挺容易的,但是照着例子做的时候却出现问题了,骨骼之间出现了很大的缝隙,找了很久才发现原来setContentScaleFactor的原因, 出现间隙,是因为各个骨骼是单独在做缩 ...

  4. java语法基础

    Java的基本符号(token) Java的单词符号有五种:关键字.标识符.常量.分隔符和操作符. Java的字符集 Java 采用一种称为unicode的字符集,该字符集合是一种新的编码标准,与常见 ...

  5. PLS入门

    PLS入门: 1,两篇关键文章 [1] de Jong, S. "SIMPLS: An Alternative Approach to Partial Least Squares Regre ...

  6. AC中保存数据与查询数据

    //保存数据 hui.ajax(function (ret, err) { }, url, {values: {t:"test",m:"Search",c:&q ...

  7. const,static,extern简介

    一.const与宏的区别: const简介:之前常用的字符串常量,一般是抽成宏,但是苹果不推荐我们抽成宏,推荐我们使用const常量. 编译时刻:宏是预编译(编译之前处理),const是编译阶段. 编 ...

  8. ResultSet 结果集带回来的一些信息

    ResultSet.getMetaData() 得到结果集的结构信息,比如字段数.字段名等. ResultSet.getMetaData().getTableName(1) 就可以返回表名. Resu ...

  9. codeforces 734E(DFS,树的直径(最长路))

    题目链接:http://codeforces.com/contest/734/problem/E 题意:有一棵黑白树,每次操作可以使一个同色连通块变色,问最少几次操作能使树变成全黑或全白. 思路:先进 ...

  10. Angularjs select的使用

    实例一:基本下拉效果 usage: label for value in array <!-- lang: html --> <select ng-model="selec ...