Leetcode#81 Search in Rotated Sorted Array II
如果不存在重复元素,仅通过判断数组的首尾元素即可判断数组是否连续,但是有重复元素的话就不行了,最坏情况下所有元素都一样,此时只能通过线性扫描确定是否连续。
设对于规模为n的问题的工作量为T(n),则有T(n) = T(n/2) + O(n),根据主定理,可以求得T(n) = O(n)。和之前的O(logn)相比还是退化了不少。
代码:
bool monop(int A[], int l, int r) {
while (l < r && A[l] <= A[r])
l++;
return A[l] <= A[r];
} bool search(int A[], int n, int target) {
int l = ;
int r = n - ; while (l <= r) {
int m = (l + r) / ;
if (A[m] == target)
return true;
if (monop(A, l, m)) {
if (A[l] <= target && target < A[m])
r = m - ;
else
l = m + ;
}
else {
if (target >= A[l] || target < A[m])
r = m - ;
else
l = m + ;
}
} return false;
}
Leetcode#81 Search in Rotated Sorted Array II的更多相关文章
- LeetCode 81 Search in Rotated Sorted Array II [binary search] <c++>
LeetCode 81 Search in Rotated Sorted Array II [binary search] <c++> 给出排序好的一维有重复元素的数组,随机取一个位置断开 ...
- [leetcode]81. Search in Rotated Sorted Array II旋转过有序数组里找目标值II(有重)
This is a follow up problem to Search in Rotated Sorted Array, where nums may contain duplicates. 思路 ...
- [LeetCode] 81. Search in Rotated Sorted Array II 在旋转有序数组中搜索 II
Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would this ...
- [LeetCode] 81. Search in Rotated Sorted Array II 在旋转有序数组中搜索之二
Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. (i.e. ...
- LeetCode 81. Search in Rotated Sorted Array II(在旋转有序序列中搜索之二)
Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would this ...
- leetCode 81.Search in Rotated Sorted Array II (旋转数组的搜索II) 解题思路和方法
Follow up for "Search in Rotated Sorted Array": What if duplicates are allowed? Would this ...
- LeetCode 81.Search in Rotated Sorted Array II(M)
题目: Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. ( ...
- LeetCode 81 Search in Rotated Sorted Array II(循环有序数组中的查找问题)
题目链接:https://leetcode.com/problems/search-in-rotated-sorted-array-ii/#/description 姊妹篇:http://www. ...
- leetcode 153. Find Minimum in Rotated Sorted Array 、154. Find Minimum in Rotated Sorted Array II 、33. Search in Rotated Sorted Array 、81. Search in Rotated Sorted Array II 、704. Binary Search
这4个题都是针对旋转的排序数组.其中153.154是在旋转的排序数组中找最小值,33.81是在旋转的排序数组中找一个固定的值.且153和33都是没有重复数值的数组,154.81都是针对各自问题的版本1 ...
随机推荐
- iOS 数据模型 的 一般设计
- Professional iOS Network Programming Connecting the Enterprise to the iPhone and iPad
Book Description Learn to develop iPhone and iPad applications for networked enterprise environments ...
- Xcode7,消失的pin菜单(Editor->pin)
用过autolayout的对pin都应该不陌生,通过这个选项可以设置控件的四周的外边距:Top Space.Leading Space.Trailing Space.Bottom Space. 在Xc ...
- zz linux 下查看局域网内所有存活主机和MAC进址
用namp对局域网扫描一遍,然后查看arp缓存表就可以知道局域内ip-mac的对应了namp比较强大也可以直接扫描mac地址和端口 进行ping扫描,打印出对扫描做出响应的主机: nmap -sP 1 ...
- 【Qt】Qt实战一二三【转】
简介 “我们来自Qt分享&&交流,我们来自Qt Quick分享&&交流”,不管你是笑了,还是笑了,反正我们是认真的.我们就是要找寻一种Hold不住的状态,来开始每一天的 ...
- Nginx之负载均衡
转自:http://www.360doc.com/content/13/1114/12/7694408_329125489.shtml 注,大家可以看到,由于我们网站是发展初期,nginx只代理了后端 ...
- eclipse juno版本中没用 ant
下载了谷歌提供的Android集成开发工具ADT,里面封装了Eclipse,但是很奇怪的是竟然没有Ant插件在里面 标准的Eclipse一般都是内置集成了Ant的. 然后到eclipse的plugin ...
- 七天学会NodeJS-学习笔记
在网上发现一篇nodeJS教程,名为七天学会NodeJS,标题很有吸引力.我不指望七天能学会,只希望可以入门,下面是我的学习笔记和遇到的问题. 教程网址:http://nqdeng.github.io ...
- mongodb的常用操作(二)
继续mongodb的学习: 9.mongodb条件查询 假设有user集合,里面结构如下:{ "_id" : ObjectId("52ab35d281181f853264 ...
- 1014. Waiting in Line (30)
Suppose a bank has N windows open for service. There is a yellow line in front of the windows which ...