[Leetcode] Search In Rotated Sorted Array (C++)
题目:
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.
Tag:
Array; Binary Search
体会:
这道题和Find the Minimum In Rotated Sorted Array肯定是有联系啦,毕竟给的都是Rotated Sorted Array这种结构。思路是:从middle处开始左右两部分,一部分是sorted的了,另一部分rotated或者sorted。我们要去分析sorted的那个部分。假设左边是sorted部分,如果A[left] <= target < A[mid],那么target一定只可能出现在这个部分,otherwise,出现在另外一部分。如果右边是sorted,同理。
class Solution {
public:
int search (int A[], int n, int target) {
int low = ;
int high = n - ;
while (low <= high) {
int mid = low + (high - low) / ;
if (A[mid] == target) {
return mid;
}
if (A[mid] < A[high]) {
// right part is sorted, left is rotated part
if (A[mid] < target && target <= A[high]) {
// target must be in right part
low = mid + ;
} else {
// target must be left part
high = mid - ;
}
} else {
// left part is sorted
if (A[low] <= target && target < A[mid]) {
high = mid - ;
} else {
low = mid + ;
}
}
}
return -;
}
};
[Leetcode] Search In Rotated Sorted Array (C++)的更多相关文章
- 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 ...
- 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 II 在旋转有序数组中搜索之二
Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would this ...
- [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 ...
- LeetCode——Search in Rotated Sorted Array II
Follow up for "Search in Rotated Sorted Array": What if duplicates are allowed? Would this ...
- [leetcode]Search in Rotated Sorted Array II @ Python
原题地址:https://oj.leetcode.com/problems/search-in-rotated-sorted-array-ii/ 题意: Follow up for "Sea ...
- 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 在旋转了的数组中查找
Search in Rotated Sorted Array Suppose a sorted array is rotated at some pivot unknown to you before ...
- [LeetCode] Search in Rotated Sorted Array I (33) && II (81) 解题思路
33. Search in Rotated Sorted Array Suppose a sorted array is rotated at some pivot unknown to you be ...
- [LeetCode] Search in Rotated Sorted Array II [36]
称号 Follow up for "Search in Rotated Sorted Array": What if duplicates are allowed? Would t ...
随机推荐
- 【7】python核心编程 第十一章-函数和函数式编程
1.*函数(与方法)装饰器 装饰器背后的主要动机源自python 面向对象编程.装饰器是在函数调用之上的修饰.这些修饰 仅是当声明一个函数或者方法的时候,才会应用的额外调用. 装饰器的语法以@开头,接 ...
- 免费馅饼(HDU 1176 DP)
免费馅饼 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submis ...
- [Codeforces Round #186 (Div. 2)] B. Ilya and Queries
B. Ilya and Queries time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- Using ROWNUM in Oracle
ROWNUM is an Oracle pseudo column which numbers the rows in a result set. SELECT rownum, table_nameF ...
- C51 函数/程序段的定位
在Keil C中可能需要指定某个函数或者某段程序链接后存放在程序区中的位置. 1. 如何指定某个函数在程序区中的位置. QUESTION How do I locate a C function at ...
- 关于导入oracle10g的数据到sqlserver2005里的方案总结
由于项目需求,现需要将oracle的数据全部导入到sqlserver中,一下算是自己的总结小计吧. sqlserver有自己的导入数据的功能,其中就有提供两种方式从oracle导入数据. 两种方式就不 ...
- unix c 07
进程的结束函数 (exit._Exit) exit 并不是立即退出,退出前执行 用atexit/on_exit函数 注册的函数. exit(int status)中的status可以用 w ...
- yumdebug
- hdu 5312 Sequence(数学推导+线性探查(两数相加版))
Problem Description Today, Soda has learned a sequence whose n-th (n≥) item )+. Now he wants to know ...
- 学艺不精,又被shell的管道给坑了
我用过bash shell,而且时间不短了.但我从来没学过shell,至少没有像C++这么认真去学.平时写些基本的脚本没问题,不懂也可以google.百度.可在2014最后一天,掉坑里了. 其实脚本也 ...