【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
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。
- 如果A[l] < A[mid],说明mid以左有序且都小于mid,如下图所示:这种情况下如果target在l和mid之间,那么需要把r重新置为mid;其他情况都需要到mid右端继续搜索。
2.如果A[l] >= A[mid], 说明mid以右有序且都大于mid,如下图所示,如果target在mid和r之间,那么需要把l重新置为mid;其他情况都需要到mid左端继续搜索。
当l + 1 = r的时候,只要检查l和r所指向的元素是否等于target即可。
代码如下:
public class Solution {
public int search(int[] A, int target) {
int l = 0;
int r = A.length - 1; while(l + 1< r){
int mid = l + (r-l)/2;
if(A[mid] == target)
return mid;
if(A[l]< A[mid] ){
if(A[mid] >= target && A[l] <= target)
r = mid;
else {
l = mid;
}
}
else {
if(target >= A[mid] && target <= A[r])
l = mid;
else {
r = mid;
}
}
} if(target == A[l])
return l;
if(target == A[r])
return r;
return -1;
}
}
【leetcode刷题笔记】Search in Rotated Sorted Array的更多相关文章
- 刷题33. Search in Rotated Sorted Array
一.题目说明 这个题目是33. Search in Rotated Sorted Array,说的是在一个"扭转"的有序列表中,查找一个元素,时间复杂度O(logn). 二.我的解 ...
- LeetCode 新题: Find Minimum in Rotated Sorted Array 解题报告-二分法模板解法
Find Minimum in Rotated Sorted Array Question Solution Suppose a sorted array is rotated at some piv ...
- LeetCode 新题: Find Minimum in Rotated Sorted Array II 解题报告-二分法模板解法
Find Minimum in Rotated Sorted Array II Follow up for "Find Minimum in Rotated Sorted Array&quo ...
- LeetCode(33)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 m ...
- 【LeetCode每天一题】Search in Rotated Sorted Array(在旋转数组中搜索)
Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand.(i.e., ...
- 【leetcode刷题笔记】Merge k Sorted Lists
Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 题 ...
- leetcode个人题解——#33 Search in Rotated Sorted Array
思路:每次取中间元素,一定有一半有序,另一半部分有序,有序的部分进行二分查找,部分有序的部分递归继续处理. class Solution { public: ; int middleSearch(in ...
- LeetCode: Search in Rotated Sorted Array II 解题报告
Search in Rotated Sorted Array II Follow up for "LeetCode: Search in Rotated Sorted Array 解题报告& ...
- LeetCode: Search in Rotated Sorted Array 解题报告
Search in Rotated Sorted Array Suppose a sorted array is rotated at some pivot unknown to you before ...
- [LeetCode] Search in Rotated Sorted Array II [36]
称号 Follow up for "Search in Rotated Sorted Array": What if duplicates are allowed? Would t ...
随机推荐
- xcode ERROR ITMS
1.ERROR ITMS-90046 /90085: "Invalid Code Signing Entitlements. Your application bundle's signat ...
- 小团队Git协作流程
git和svn 最大的差异在于git是分布式的管理方式而svn是集中式的管理方式. 集中式 集中式代码管理的核心是服务器,所有开发者在开始coding之前必须从服务器获取代码,然后开发,最后解决冲突, ...
- org.apache.poi3.1.7 Excle并发批量导入导出
org.apache.poi3.1.7 升级,需要修改设置方式: 1.org.apache.poi3.1.4 的设置单元格: XSSFCellStyle cellStyle = wb.createCe ...
- 九度OJ 1002:Grading
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:18410 解决:4753 题目描述: Grading hundreds of thousands of Graduate Entrance ...
- 获取exe文件窗口抓图,将memo转化为JPG输出
unit Unit1; interface uses Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System ...
- linux c编程:Posix消息队列
Posix消息队列可以认为是一个消息链表. 有足够写权限的线程可以往队列中放置消息, 有足够读权限的线程可以从队列中取走消息 在某个进程往一个队列写入消息前, 并不需要另外某个进程在该队列上等待消息的 ...
- Symfony 安装FOUSerBundle
第一按照官网安装 : https://symfony.com/doc/current/bundles/FOSUserBundle/index.html#main 可能版本无法安装 : $ compos ...
- Python赋值原理:Python无变量,万物皆对象
有几个和以前的常见语言,比如c语言不同 改变变量数据不覆盖原来的 name = '苍老师' print(id(name)) name = '志玲' print(id(name)) 运行结果 73955 ...
- SQL优化小结
一 背景 客户数据库经常出现死锁.超时.查询慢等问题,数据库mssql,数据量主要表大概上千W. 二 收集信息 首先是要找出IO大.查询慢.使用频率高的脚本.直接用Profiler ...
- Pinpoint扩展插件实践笔记
为链路(spanEvent)添加tag 背景 我们可能需要想在代码中写入特定的信息到调用链中,并且希望对里面的特定key做检索 实现思路 创建一个特定的类,只需要一个方法,再对这个类的方法进行增强,这 ...