[LeetCode] Search in Rotated Sorted Array II [36]
称号
Follow up for "Search in Rotated Sorted Array":
What if duplicates are allowed?
Would this affect the run-time complexity? How and why?
Write a function to determine if a given target is in the array.
解题思路
这题和Search in Rotated Sorted Array问题类似。只是这个题同意元素反复。假设有元素反复,此时採取最保守的策略。一次缩小一个范围。
代码实现
class Solution {
public:
bool search(int A[], int n, int target) {
if(A==NULL || n<=0) return false;
int begin = 0, end = n-1, mid = 0;
while(begin<=end){
mid = (begin+end)/2;
if(A[mid] == target) return true;
if(A[mid] < A[end]){
//后半段有序
if(A[end]>=target && A[mid]<target)
begin = mid+1;
else
end = mid - 1;
}else if(A[mid] > A[end]){
//前半段有序
if(A[begin]<=target && A[mid] > target)
end = mid-1;
else
begin = mid+1;
}else
// 最保守策略,缩小一个范围
--end;
}
return false;
}
};
另外。我开通了微信公众号--分享技术之美,我会不定期的分享一些我学习的东西.
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvc3dhZ2xl/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="">
)
版权声明:本文博主原创文章。博客,未经同意不得转载。
[LeetCode] Search in Rotated Sorted Array II [36]的更多相关文章
- 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 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 ii 搜索旋转有序数组
Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would this ...
- [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 -- 有重复的旋转序列搜索
Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would this ...
- 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 OJ:Search in Rotated Sorted Array II(翻转排序数组的查找)
Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would this ...
随机推荐
- 最牛B的编程套路
最近,我大量阅读了Steve Yegge的文章.其中有一篇叫“Practicing Programming”(练习编程),写成于2005年,读后令我惊讶不已: 与你所相信的恰恰相反,单纯地每天埋头于工 ...
- WP开发使用BingMaps地图服务
原文:WP开发使用BingMaps地图服务 WP8使用BingMaps地图在 SOAP服务如何计算路径 首先需要用到3个服务 1.GeoCode服务-转换地址到地理的经纬度(WebServices地址 ...
- Java 实现二分(折半)插入排序
设有一个序列a[0],a[1]...a[n];当中a[i-1]前是已经有序的,当插入时a[i]时,利用二分法搜索a[i]插入的位置 效率:O(N^2),对于初始基本有序的序列,效率上不如直接插入排序: ...
- [欧拉] poj 2513 Colored Sticks
主题链接: http://poj.org/problem? id=2513 Colored Sticks Time Limit: 5000MS Memory Limit: 128000K Tota ...
- 用XAML做网页!!—终结篇
原文:用XAML做网页!!-终结篇 迄今为止的设计都很顺利,但这次就不得不接触我前面所说的非常糟糕的流文档了,但在此之前先来把标题弄好: <Border BorderBrush="#6 ...
- 在配置文件(.settings、.config)中存储自定义对象
原文:在配置文件(.settings..config)中存储自定义对象 引言 我前面曾写过一篇<使用配置文件(.settings..config)存储应用程序配置>,我在其中指出“sett ...
- 工厂方法模式(factory method pattern)
工厂方法模式相对于简单工厂模式的长处是为了更好的拓展,当假设再新加一种产品,对于简单工厂模式来说须要改动核心的工厂类,但对于工厂方法模式则不须要,在工厂方法模式中核心的工厂类不再负责创建全部产品的创建 ...
- java正則表達式的坑
java中正則表達式比較有意思,这里列举几个常见的坑 1.[]符号,中括号表示当中的数据都是或的关系 假设[\\w+]是匹配条件 abc能否够匹配的到呢? 首先\\w(注意这里没有中括号)表示a-z ...
- HDU 1661 Assigments 贪心法题解
Problem Description In a factory, there are N workers to finish two types of tasks (A and B). Each t ...
- hdu3037(lucas定理)
给定n,m,p 表示<=m个豆子放在n棵树上,一共有多少种方案数, 总的方案书mod p 如果将m个豆子放在n棵树上, 可以使用插板法 得到方案数是C(n+m-1,n-1) 那么将0< ...