33. Search in Rotated Sorted Array(二分查找)
(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(二分查找)的更多相关文章
- [Leetcode][Python]33: Search in Rotated Sorted Array
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 33: Search in Rotated Sorted Arrayhttps ...
- [array] leetcode - 33. Search in Rotated Sorted Array - Medium
leetcode - 33. Search in Rotated Sorted Array - Medium descrition Suppose an array sorted in ascendi ...
- LeetCode 33 Search in Rotated Sorted Array [binary search] <c++>
LeetCode 33 Search in Rotated Sorted Array [binary search] <c++> 给出排序好的一维无重复元素的数组,随机取一个位置断开,把前 ...
- 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 ...
- 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 ...
- 刷题33. Search in Rotated Sorted Array
一.题目说明 这个题目是33. Search in Rotated Sorted Array,说的是在一个"扭转"的有序列表中,查找一个元素,时间复杂度O(logn). 二.我的解 ...
- 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 ...
- 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 ...
- 【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 ...
随机推荐
- MySQL对数据表进行分组查询(GROUP BY)
MySQL对数据表进行分组查询(GROUP BY) GROUP BY关键字可以将查询结果按照某个字段或多个字段进行分组.字段中值相等的为一组.基本的语法格式如下: GROUP BY 属性名 [HAVI ...
- 在Linux下面的某一个文件的查找命令
借鉴文章:https://www.kafan.cn/edu/60044166.html Linux查找包含特定字符串的文件名的方法:http://www.jbxue.com/LINUXjishu/97 ...
- shell 中的$0 $1 $* $@ $# $$ $? $() $(())
$0: 脚本本身文件名称 : 命令行第一个参数,$2为第二个,以此类推 $*: 所有参数列表 $@: 所有参数列表 $#: 参数个数 $$: 脚本运行时的PID $?: 脚本退出码 ∗与@的区别 当命 ...
- 使用zsh 替换 bash
摘自:http://macshuo.com/?p=676#wechat_redirect Shell是Linux/Unix的一个外壳,你理解成衣服也行.它负责外界与Linux内核的交互,接收用户或其他 ...
- 常见的几个js疑难点,match,charAt,charCodeAt,map,search
JavaScript match() 方法 定义和用法 match() 方法可在字符串内检索指定的值,或找到一个或多个正则表达式的匹配. 该方法类似 indexOf() 和 lastI ...
- Logstash自带正则表达式
USERNAME [a-zA-Z0-._-]+ USER %{USERNAME} INT (?:[+-]?(?:[-]+)) BASE10NUM (?<![-.+-])(?>[+-]?(? ...
- RGB颜色值与十六进制颜色码对照表
颜色码对照表 颜色 英文代码 形象描述 十六进制 RGB LightPink 浅粉红 #FFB6C1 255,182,193 Pink 粉红 #FFC0CB 255,192,203 Crimson 猩 ...
- SQL Fundamentals || Single-Row Functions || 日期函数date functions
SQL Fundamentals || Oracle SQL语言 SQL Fundamentals: Using Single-Row Functions to Customize Output使 ...
- 数据恢复:AMDU数据抽取恢复
今天我们通过一则真实的案例来认识oracle 自带工具AMDU,无需将磁盘组mount即可实现数据分析,轻松进行数据恢复.某日,我们收到了一则香港用户ASM破坏案例,请求数据恢复.灾难描述:这则案例是 ...
- ios -RunTi me(相关知识)
一,推荐两片比较好的介绍RunTime的博文 1>runtime运行时官方文档翻译 2>runtime的学习总结 1.类和对象 2. 成员变量和属性 3.方法与消息 4.Method ...