Suppose an array sorted in ascending order 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.

Your algorithm's runtime complexity must be in the order of O(log n).

Example 1:

Input: nums = [4,5,6,7,0,1,2], target = 0
Output: 4

Example 2:

Input: nums = [4,5,6,7,0,1,2], target = 3
Output: -1 法1:将数组一分为二,其中一定有一个是有序的,另一个可能是有序,也能是部分有序。此时有序部分用二分法查找。无序部分再一分为二,其中一个一定有序,另一个可能有序,可能无序。就这样循环.
class Solution {
public:
int search(vector<int>& nums, int target) {
if(nums.size()==0) return -1;
return bt_search(nums, 0,nums.size()-1,target);
} int bt_search(vector<int>& a, int low, int high, int target) {
int mid = low + (high - low) / 2;
if(a[low]==target) return low;
if(a[high]==target) return high;
if(low>=high) return -1; if (a[low] < a[mid]) {
if (a[low] <target && target< a[mid]) {
return bt_search(a, low+1, mid-1, target);
}
else {
return bt_search(a,mid, high, target);
}
} else {
if (a[mid] <target && target <a[high]) {
return bt_search(a, mid+1, high-1, target);
}
else {
return bt_search(a,low, mid, target);
}
}
return -1;
}
};

  

  

从左向右,如果左边的点比右边的点小,说明这两个点之间是有序的。
     如果左边的点比右边的点大,说明中间有个旋转点,所以一分为二后,肯定有一半是有序的。所以还可以用二分法。
        不过先要判断左边有序还是右边有序,如果左边有序,则直接将目标与左边的边界比较,就知道目标在不在左边,
        如果不在左边肯定在右边。
 class Solution {
public int search(int[] a, int target) {
int n = a.length;
int lo = 0;
int hi = n - 1;
while(lo<=hi){
int mid = lo+(hi-lo)/2;
if(a[mid]== target)
return mid; if(a[lo]<=a[mid]){//左半边有序
if(a[lo]<=target && target<=a[mid])//目标值在左半边
hi = mid - 1;
else
lo = mid + 1;
}
else{//右半边有序
if(a[mid]<=target && target<=a[hi])
lo = mid + 1;
else
hi = mid - 1;
}
}
return -1; }
}

33. Search in Rotated Sorted Array(二分查找)的更多相关文章

  1. [Leetcode][Python]33: Search in Rotated Sorted Array

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 33: Search in Rotated Sorted Arrayhttps ...

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

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

  3. LeetCode 33 Search in Rotated Sorted Array [binary search] <c++>

    LeetCode 33 Search in Rotated Sorted Array [binary search] <c++> 给出排序好的一维无重复元素的数组,随机取一个位置断开,把前 ...

  4. LeetCode题解33.Search in Rotated Sorted Array

    33. Search in Rotated Sorted Array Suppose an array sorted in ascending order is rotated at some piv ...

  5. leetcode 153. Find Minimum in Rotated Sorted Array 、154. Find Minimum in Rotated Sorted Array II 、33. Search in Rotated Sorted Array 、81. Search in Rotated Sorted Array II 、704. Binary Search

    这4个题都是针对旋转的排序数组.其中153.154是在旋转的排序数组中找最小值,33.81是在旋转的排序数组中找一个固定的值.且153和33都是没有重复数值的数组,154.81都是针对各自问题的版本1 ...

  6. 刷题33. Search in Rotated Sorted Array

    一.题目说明 这个题目是33. Search in Rotated Sorted Array,说的是在一个"扭转"的有序列表中,查找一个元素,时间复杂度O(logn). 二.我的解 ...

  7. 33. Search in Rotated Sorted Array & 81. Search in Rotated Sorted Array II

    33. Search in Rotated Sorted Array Suppose an array sorted in ascending order is rotated at some piv ...

  8. leetCode 33.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】33. Search in Rotated Sorted Array (4 solutions)

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

随机推荐

  1. [工具] 各种主流 SQLServer 迁移到 MySQL 工具对比

    我之所以会写这篇对比文章,是因为公司新产品研发真实经历过这个痛苦过程(传统基于SQL Server开发的C/S产品转为MySQL云产品). 首次需要数据转换是测试环节,当时为了快速验证新研发云产品性能 ...

  2. [通信] C#多线程Socket-文件传输

    FileSendClient : Form1.cs using System; using System.IO; using System.Net; using System.Net.Sockets; ...

  3. CentOS6 防火墙配置

    清空现有的规则 iptables -F iptables -P INPUT DROP iptables -I INPUT -m state --state RELATED , ESTABLISHED ...

  4. php---进行签名验证

    为什么要进行签名,我们在进行数据请求的时候,为了防止数据被人截取,造成不好的影响,所以我们在进行数据请求的时候,需要进行签名验证,进行签名的原理是:客户端和服务端使用同样的签名算法,来计算签名,当客户 ...

  5. 9.7 Django

    2018-9-7 14:37:35 这次是 图书  出版社  作者 的连表 2018-9-7 16:56:36

  6. 记H5单页遇到的几个ios兼容问题

    最近写一个H5活动页,安卓里的表现很不错,写下来很少出现兼容性问题,ios就不一样了,好多问题都出现在ios上(手动狗头)

  7. App开发如何制作测试数据

    OHHTTPStubs 使用第三方请求库模拟返回json数据 https://github.com/AliSoftware/OHHTTPStubs 使用青花瓷maplocal制造假数据 https:/ ...

  8. 获取验证码随机字符串@return string $captcha,随机验证码文字

    <?php//验证码工具类class Captcha{//属性private $width;private $height;private $fontsize;private $pixes;pr ...

  9. http://www.cnblogs.com/linxiyue/p/8244724.html

    http://www.cnblogs.com/linxiyue/p/8244724.html

  10. Animator Override Controller学习及性能测试

    本文由博主(YinaPan)原创,转载请注明出处: http://www.cnblogs.com/xsln/p/Animator_Override_Controller.html 一.Animator ...