【题目】

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.

【二分思路】

分情况讨论,数组可能有以下三种情况:

然后,再看每一种情况中,target在左边还是在右边,其中第一种情况还可以直接判断target有可能不在数组范围内。

 public class Solution {
public int search(int[] A, int target) {
int len = A.length;
if (len == 0) return -1;
return binarySearch(A, 0, len-1, target);
} public int binarySearch(int[] A, int left, int right, int target) {
if (left > right) return -1; int mid = (left + right) / 2;
if (A[left] == target) return left;
if (A[mid] == target) return mid;
if (A[right] == target) return right; //图示情况一
if (A[left] < A[right]) {
if (target < A[left] || target > A[right]) { //target不在数组范围内
return -1;
} else if (target < A[mid]) { //target在左边
return binarySearch(A, left+1, mid-1, target);
} else { //target在右边
return binarySearch(A, mid+1, right-1, target);
}
}
//图示情况二
else if (A[left] < A[mid]) {
if (target > A[left] && target < A[mid]) { //target在左边
return binarySearch(A, left+1, mid-1, target);
} else { //target在右边
return binarySearch(A, mid+1, right-1, target);
}
}
//图示情况三
else {
if (target > A[mid] && target < A[right]) { //target在右边
return binarySearch(A, mid+1, right-1, target);
} else{ //target在左边
return binarySearch(A, left+1, mid-1, target);
}
}
}
}

我的解法,不是最优解

 class Solution {
public:
int search(int A[], int n, int target) {
if(A==NULL||n<) return -;
int index=;
for(int i=;i<n;i++){
if(A[i-]>A[i]){
index=i;
break;
}
}
int left,right;
if(target>=A[]&&target<=A[index-]){
left=;
right=index-;
}else if(target>=A[index]&&target<=A[n-]){
left=index;
right=n-;
}else
return -;
while(left<=right){
int mid=(left+right)/;
if(target==A[left])
return left;
if(target==A[right])
return right;
if(target==A[mid])
return mid;
if(target>A[left]&&target<A[mid]){
left++;
right=mid-;
}else{
right--;
left=mid+;
}
}
return -;
}
};

【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 在旋转有序数组中搜索

    Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 migh ...

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

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

  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 -- 有重复的旋转序列搜索

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

  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 II

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

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

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

随机推荐

  1. Ubuntu下建立Pycharm快捷方式

    修改 Exec, Icon 路径,将文件保存 pycharm.desktop,拖到unity侧边栏 [Desktop Entry]Categories=Development;Comment[zh_C ...

  2. CSU 1809 Parenthesis(RMQ-ST+思考)

    1809: Parenthesis Submit Description Bobo has a balanced parenthesis sequence P=p1 p2…pn of length n ...

  3. [hdu5307] He is Flying [FFT+数学推导]

    题面 传送门 思路 看到这道题,我的第一想法是前缀和瞎搞,说不定能$O\left(n\right)$? 事实证明我的确是瞎扯...... 题目中的提示 这道题的数据中告诉了我们: $sum\left( ...

  4. linux系统——机制与策略(二)

    策略与机制 大部分策略与机制的区别定义是,策略是描述如何实现什么功能,机制则是需要实现怎样的功能.在"The Art of Unix Programming" 中Raymond通过 ...

  5. shell if 条件语句实践

    对于if 语法 我们不过多做介绍,这里直接上实例,以开发rsync服务启动脚本为例,先对rsync做个简单介绍 [root@backup ~]# rpm -qa|grep rsync rsync--. ...

  6. react 基础语法复习2- react入门以及JSX

    引入 react 以及 ReactDom import React from 'react'; import ReactDOM from 'react-dom'; 将react组件渲染到真实dom节点 ...

  7. 【02】 Vue 之 数据绑定

    2.1. 什么是双向绑定? Vue框架很核心的功能就是双向的数据绑定. 双向是指:HTML标签数据 绑定到 Vue对象,另外反方向数据也是绑定的.通俗点说就是,Vue对象的改变会直接影响到HTML的标 ...

  8. HDOJ 2222: Keywords Search

    Keywords Search Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others ...

  9. 洛谷P2168 荷马史诗

    哈夫曼树原理. k=2时,和合并果子一样一样的. 由此思考,k>2时,应该也有相似的原理.确实如此,k进制哈夫曼树,每个结点最多会有k-1个子结点,对应k-1个元素(“元素”可以是更深层的子树) ...

  10. .net连接mysql

    首先在官网下载,mysql-connect-net,用于使用mysql的驱动程序,我在下载mysql-connect-net.msi. installer后,执行安装程序的时候一直无法安装成功,最简单 ...