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.

 
 
利用二分法,
如果left<A[mid],说明从左边left开始到mid是有序的
 
如果left>A[mid],说明右边从mid开始,到right是有序的
 
如果left=A[mid],则说明left和mid在同一个位置,如果A[mid]!=target则下一步中left=left+1;
 
 class Solution {
public:
int search(int A[], int n, int target) { int left=;
int right=n-;
int mid;
while(left<=right)
{
mid=(left+right)/; if(A[mid]==target) return mid; if(A[left]<A[mid])//left
{
if(A[left]<=target&&target<A[mid])
{
right=mid-;
}
else
{
left=mid+;
}
}
else if(A[left]>A[mid])//right
{
if(A[mid]<target&&target<=A[right])
{
left=mid+;
}
else
{
right=mid-;
}
}
else
{
//最后一个判断语句可以一起放到第一个语句里,if(A[left]<=A[mid])
left=mid+;
} }
return -;
}
};

【leetcode】Search in Rotated Sorted Array的更多相关文章

  1. 【LeetCode】Search in Rotated Sorted Array II(转)

    原文链接 http://oj.leetcode.com/problems/search-in-rotated-sorted-array-ii/ http://blog.csdn.net/linhuan ...

  2. 【leetcode】Search in Rotated Sorted Array II

    Search in Rotated Sorted Array II Follow up for "Search in Rotated Sorted Array":What if d ...

  3. 【leetcode】Search in Rotated Sorted Array II(middle)☆

    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——旋转有序数列找目标值

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

  6. 【leetcode】Search in Rotated Sorted Array (hard)

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

  7. 【Leetcode】【Hard】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 ...

  8. [LeetCode] 033. Search in Rotated Sorted Array (Hard) (C++)

    指数:[LeetCode] Leetcode 解决问题的指数 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 033. ...

  9. [array] leetcode - 33. Search in Rotated Sorted Array - Medium

    leetcode - 33. Search in Rotated Sorted Array - Medium descrition Suppose an array sorted in ascendi ...

随机推荐

  1. linux标准输入输出及错误输出

    Linux Shell 环境中支持输入输出重定向,用符号"<"和">"来表示. 0.1和2分别表示标准输入.标准输出和标准错误信息输出,可以用来指定 ...

  2. js字节转换、字节转换GB等

    //文件大小换算 function bytesToSize(bytes) { if (bytes === 0) return '0 B'; var k = 1024; sizes = ['B','KB ...

  3. js的元素对象

    元素对象(element对象)        ** 要操作element对象,首先必须要获取到element,                       - 使用document里面相应的方法获取  ...

  4. tmux常用命令

    tmux命令可以启动一个tmux服务,tmux服务包含多个session,session包含多个window,window包含多个pane. 常用命令tmux ls #显示已有tmux列表(C-b s ...

  5. 存储过程中的when others then 和 raise

    EXCEPTION when others then rollback; dbms_output.put_line('code:' || sqlcode); dbms_output.put_line( ...

  6. hdu4970 Killing Monsters (差分数列)

    2014多校9 1011 http://acm.hdu.edu.cn/showproblem.php?pid=4970 Killing Monsters Time Limit: 2000/1000 M ...

  7. CF459C Pashmak and Buses (构造d位k进制数

    C - Pashmak and Buses Codeforces Round #261 (Div. 2) C. Pashmak and Buses time limit per test 1 seco ...

  8. [译]使用6to5,让今天就来写ES6的模块化开发!

    http://es6rocks.com/2014/10/es6-modules-today-with-6to5/?utm_source=javascriptweekly&utm_medium= ...

  9. php防注入

    引发 SQL 注入攻击的主要原因,是因为以下两点原因: 1. php 配置文件 php.ini 中的 magic_quotes_gpc选项没有打开,被置为 off 2. 开发者没有对数据类型进行检查和 ...

  10. 修改Mysql默认编码

    show variables like 'character%';+--------------------------+----------------------------+| Variable ...