[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 ...
随机推荐
- Android开展:ADT+Eclipse使用错误:Text editor does not have a document provider
Eclipse参加Android sdk源代码 正在使用Eclipse进行Android开发时间,我们经常需要导入sdk源代码来Eclipse中,方便api阅读和查询,详细操作为:ctrl+鼠标左键. ...
- Java流读写
写: package com.wjy.write; import java.io.BufferedWriter; import java.io.FileOutputStream; import jav ...
- U9文件与文件系统的压缩和打包
1.在Linux的环境中,压缩文件的扩展名大多为:*.tar,*.tar.gz,*.tgz,*.bz2. 2.gzip可以说是应用最广的压缩命令了.目前gzip可以揭开compress,zip和gzi ...
- WPF动态改变主题颜色
原文:WPF动态改变主题颜色 国内的WPF技术先行者周银辉曾介绍过如何动态改变应用程序的主题样式,今天我们来介绍一种轻量级的改变界面风格的方式--动态改变主题色. 程序允许用户根据自己的喜好来对界面进 ...
- android细节之禁用activity的系统的默认切换效果
网上有非常多方法来禁用系统的默认效果,这里贴上来我觉得最简单的方法. overridePendingTransition(Animation.INFINITE, Animation.INFINITE) ...
- JQuery日记_5.13 Sizzle选择器(六)选择器的效率
当选择表达式不符合高速匹配(id,tag,class)和原生QSA不可用或返回错误时,将调用select(selector, context, results, seed)方法,此方法迭代DO ...
- cisco(思科)交换机配置篇【两】
上一页大家津津乐道cisco基本操作命令开关,而端午假期,该cisco简单的开关配置,并希望请您分享"端午节快乐"!Ok,配置交换机,首先,你必须进入全局配置模式Switch,在成 ...
- RMQ之ST算法
#include <stdio.h> #include <string.h> ; int a[N]; ]; inline int min(const int &a, c ...
- JBOSS EAP6.2.0的下载安装、环境变量配置以及部署
JBOSS EAP6.2.0的下载安装.环境变量配置以及部署 JBoss是纯Java的EJB(企业JavaBean)server. 第一步:下载安装 1.进入官网http://www.jboss.or ...
- 《Head First 设计模式》学习笔记——迭代模式 + 组合模式
迭代模式设置共生死亡,一般来说.我们只是想实现一个集,我们需要的同时提供这个集合的迭代器,喜欢java中间Collection.List.Set.Map等,这些集合都有自己的迭代器.假如我们要实现一个 ...