[LeetCode] Search in Rotated Sorted Array II 二分搜索
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.
class Solution {
public:
bool search(int A[], int n, int target) {
if(n==) return A[]==target?true:false;
return help(A,,n-,target);
}
int help(int* & A,int l,int r,int &target)
{
if(target<A[l]&&target>A[r]) return false;
if(target==A[l]) return true;
if(target==A[r]) return true;
if(r-l==) return false;
int m = (l+r) /;
if(target==A[m]) return true;
if(A[l]<A[m]){
if(A[l]<target&&target<A[m]) return help(A,l,m,target);
return help(A,m,r,target);
}
else if(A[l]>A[m]){
if(A[m]<target&&target<A[r]) return help(A,m,r,target);
return help(A,l,m,target);
}
else{
return help(A,l,m,target)||help(A,m,r,target);
}
}
};
[LeetCode] Search in Rotated Sorted Array II 二分搜索的更多相关文章
- 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 [36]
称号 Follow up for "Search in Rotated Sorted Array": What if duplicates are allowed? Would t ...
- [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 ...
随机推荐
- PHP 工厂模式介绍
工厂模式,顾名思义,如同工厂一样,你把原材料放入工厂中,出来的是成品,而你并不需要知道工厂里做了什么.代码中也类似,把主要参数放入一个工厂里,返回的是处理好的数据,我们并不需要工厂里做了什么,只需要知 ...
- Vue钩子函数生命周期实例详解
vue生命周期简介 Vue实例有一个完整的生命周期,也就是从开始创建.初始化数据.编译模板.挂载Dom.渲染→更新→渲染.卸载等一系列过程,我们称这是Vue的生命周期.通俗说就是Vue实例从创建到销毁 ...
- CentOS6.7下的软件安装
一.JDK安装及其环境变量的配置 **创建一个专门安装软件的文件夹:mkdir /root/apps **解压安装包:tar -zxvf jdk-7u45-linux-x64.tar.gz -C /r ...
- web前端的环境配置
1.1.WEB开发的相关知识 WEB,在英语中web即表示网页的意思,它用于表示Internet主机上供外界访问的资源. Internet上供外界访问的Web资源分为: 静态web资源(如html 页 ...
- Python While循环、运算符以及一些基础运用
1.循环语句 循环打印"人生苦短,我用python" while True: print("人生苦短,我用python") 利用While循环,打印1~10 c ...
- Python学习笔记(五)之Python操作Redis、mysql、mongodb数据库
操作数据库 一.数据库 数据库类型主要有关系型数据库和菲关系型数据库. 数据库:用来存储和管理数的仓库,数据库是通过依据“数据结构”将数据格式化,以记录->表->库的关系存储.因此数据查询 ...
- manjaro中文输入法已安装但切换不了解决方法
情况如图所示,输入法安装了,但Ctrl+空格键或者鼠标选择切换都不行 解决方法: 打开家目录下面的.xprofile文件,如果没有这个文件就新建一个,加入下面内容 保存文件,退出. 重启电脑就可以了
- C++构造函数实例——拷贝构造,赋值
#define _CRT_SECURE_NO_WARNINGS //windows系统 #include <iostream> #include <cstdlib> #incl ...
- 指针的操作 p*++
int x, y, *px = &x, *py = &y; y = *px + ; //表示把x的内容加5并赋给y,*px+5相当于(*px)+5 y = ++*px; //px的内容 ...
- sql优化系列1
sql中索引是否会用到,进而影响查询效率. 带通配符(%)的like语句 1.不能用null作索引,任何包含null值的列都将不会被包含在索引中.即使索引有多列这样的情况下,只要这些列中有一列含有nu ...