题目:

Follow up for "Search in Rotated Sorted Array":
What if duplicates are allowed?

Would this affect the run-time complexity? How and why?

Suppose an array sorted in ascending order 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).

Write a function to determine if a given target is in the array.

The array may contain duplicates.

题解:

Solution 1

class Solution {
public:
bool search(vector<int>& nums, int target) {
int n = nums.size();
int begin = , end = n;
while(begin < end){
int mid = begin + (end - begin) / ;
if(nums[mid] == target)
return true;
if(nums[begin] < nums[mid]){
if(nums[begin] <= target && target < nums[mid])
end = mid;
else
begin = mid + ;
} else if(nums[begin] > nums[mid]){
if(nums[mid] < target && target <= nums[end - ])
begin = mid + ;
else
end = mid;
} else {
++begin;
}
}
return false;
}
};

【LeetCode】081. Search in Rotated Sorted Array II的更多相关文章

  1. 【LeetCode】81. Search in Rotated Sorted Array II (2 solutions)

    Search in Rotated Sorted Array II Follow up for "Search in Rotated Sorted Array":What if d ...

  2. 【LeetCode】81. Search in Rotated Sorted Array II 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/search-in ...

  3. 【Leetcode】81. Search in Rotated Sorted Array II

    Question: Follow up for "Search in Rotated Sorted Array": What if duplicates are allowed? ...

  4. 【一天一道LeetCode】#81. Search in Rotated Sorted Array II

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Follow ...

  5. 【LeetCode】33. Search in Rotated Sorted Array (4 solutions)

    Search in Rotated Sorted Array Suppose a sorted array is rotated at some pivot unknown to you before ...

  6. 【leetcode】Find Minimum in Rotated Sorted Array II JAVA实现

    一.题目描述 Follow up for "Find Minimum in Rotated Sorted Array":What if duplicates are allowed ...

  7. 【LeetCode】33. Search in Rotated Sorted Array 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  8. 【Leetcode】33. Search in Rotated Sorted Array

    Question: Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforeh ...

  9. 【LeetCode】033. Search in Rotated Sorted Array

    题目: Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. ( ...

随机推荐

  1. centos7.0 安装redis 3.2.9

    wget http://download.redis.io/releases/redis-3.2.9.tar.gz tar xzf redis-3.2.9.tar.gz cd redis-3.2.9 ...

  2. easyUI参数传递Long型时,前台解析出错的问题——SKY

    果发现datagrid在显示Long类型数据时有问题.问题如下:比如一个数据ID为20121229101239002,经过转换之后的JSON数据也没有问题,但是在显示的时候就会显示为201212291 ...

  3. UITableView 右侧索引

    1.设置右侧索引字体颜色 self.tabView.sectionIndexColor = [UIColor blackColor]; 2.设置右侧索引背景色 self.cityTabView.sec ...

  4. What is the difference between iterations and epochs in Convolution neural networks?

    https://stats.stackexchange.com/questions/164876/tradeoff-batch-size-vs-number-of-iterations-to-trai ...

  5. 【python】-- 递归函数、高阶函数、嵌套函数、匿名函数

    递归函数 在函数内部,可以调用其他函数.但是在一个函数在内部调用自身,这个函数被称为递归函数 def calc(n): print(n) if int(n/2) == 0: #结束符 return n ...

  6. IO多路复用的作用?并列举实现机制以及区别?

    I/O多路复用是用于提升效率,单个进程可以同时监听多个网络连接IO. 举例:通过一种机制,可以监视多个文件描述符,一旦描述符就绪(读就绪和写就绪),能通知程序进行相应的读写操作,I/O多路复用避免阻塞 ...

  7. genymotion device manager列表没有

    1.第一种原因:链接Genymotion官网的网络超时,无法加载Genymotion device列表,解决办法百度一下:配置Genymotion代理服务器,联网下载 2.第二种可能:检查是否正确安装 ...

  8. linux c编程:进程控制(二)_竞争条件

    前面介绍了父子进程,如果当多个进程企图对共享数据进行处理.而最后的结果又取决于进程运行的顺序时,就认为发生了竞争关系.通过下面的例子来看下 在这里标准输出被设置为不带缓冲的,于是父子进程每输出一个字符 ...

  9. mongodb 的注意点

    昨天同事安装mongodb遇到了些问题,问了下我,后拉发现都是些细节没注意(讲道理这应该是很简单,一顿操作就ok的事情) 首先,下载 mongo包, 然后 ,解压安装, 启动之. 问题就出现在他后台启 ...

  10. Swap file "/etc/.hosts.swp" already exists! [O]pen Read-Only, (E)dit anyway, (R)ecover, (D)elete it,

    非正常关闭vi编辑器时会生成一个.swp文件 非正常关闭vi编辑器时会生成一个.swp文件 关于swp文件 使用vi,经常可以看到swp这个文件,那这个文件是怎么产生的呢,当你打开一个文件,vi就会生 ...