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) {
int l=,r=n-;
while(l<=r){
if(target==A[l]||target==A[r])
return true;
int m =(l+r)/;
if(target==A[m])
return true;
if(A[l]<A[m]){
if(target<A[m]&&target>A[l])
r=m-;
else
l=m+;
}else if(A[l]>A[m]){
if(target>A[m]&&target<A[r])
l=m+;
else
r=m-;
}else
l++;
}
return false;
}
};
这道题是二分查找Search Insert Position的变体,思路在Search in Rotated Sorted Array中介绍过了,不了解的朋友可以先看看那道题哈。和Search in Rotated Sorted Array唯一的区别是这道题目中元素会有重复的情况出现。不过正是因为这个条件的出现,出现了比较复杂的case,甚至影响到了算法的时间复杂度。原来我们是依靠中间和边缘元素的大小关系,来判断哪一半是不受rotate影响,仍然有序的。而现在因为重复的出现,如果我们遇到中间和边缘相等的情况,我们就丢失了哪边有序的信息,因为哪边都有可能是有序的结果。假设原数组是{1,2,3,3,3,3,3},那么旋转之后有可能是{3,3,3,3,3,1,2},或者{3,1,2,3,3,3,3},这样的我们判断左边缘和中心的时候都是3,如果我们要寻找1或者2,我们并不知道应该跳向哪一半。解决的办法只能是对边缘移动一步,直到边缘和中间不在相等或者相遇,这就导致了会有不能切去一半的可能。所以最坏情况(比如全部都是一个元素,或者只有一个元素不同于其他元素,而他就在最后一个)就会出现每次移动一步,总共是n步,算法的时间复杂度变成O(n)。代码如下:
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 ...
随机推荐
- centos6.5 mysql忘记登入密码
1.修改文件目录为/etc/my.cnf的文件; 在[mysqld]的段中加上一句:skip-grant-tables,保存文件重启数据库: 例如: [mysqld] skip-grant-table ...
- Canvas与Image互相转换示例以及利用该技术实现微信长按自动识别二维码功能
现在扫描二维码已经很普遍,微信扫一扫即可,但是如果二维码是在自己的手机上呢?那就要用到微信里的一个功能了,手指长按二维码,会弹出自动识别的选项,点确定就可以看到二维码的内容了.那么怎么通过前端实现这个 ...
- jsp导出table数据excel表
<html> <head> <meta http-equiv="content-Type" content="text/html;chars ...
- MySQL服务器端&客户端常见错误
目录(?)[+] 客户端 服务器端 客户端 1.ERROR 2013 (HY000) at line 1: Lost connection to MySQL server during query 使 ...
- cf 235C 后缀自动机
题目大意 给定字符串\(S\)与\(n<=10^5\)个串\(x_1,x_2...x_n\)(总长\(\le10^6\)) 对于每个\(x_i\),输出有多少个\(S\)的子串与\(x_i\)循 ...
- spring rest 请求怎样添加Basic Auth请求頭
请自行揣摩代码 package com.hudai.platform.manager.util; import java.net.URI; import java.net.URISyntaxExcep ...
- 【HDOJ5528】Count a * b(积性函数)
题意:设f(i)为0<=x,y<=i-1且xy%i=0的(x,y)对数,g(i)为sigma f(j) [i%j==0] 给定n,求g(n),答案对2^64取模 T<=2e4,n&l ...
- 【HDOJ5951】Winning an Auction(博弈DP)
题意:A和B两个人做一个拍卖游戏.每一轮两人分别给出一个价格,出价高者获得该轮的物品,出价相同则奇数轮A优先,偶数轮B优先. 两个人的目标都是最大化自己的商品数量,给定轮数n与两人分别的总资金a,b, ...
- 《Linux命令行与shell脚本编程大全 第3版》Linux命令行---56
以下为阅读<Linux命令行与shell脚本编程大全 第3版>的读书笔记,为了方便记录,特地与书的内容保持同步,特意做成一节一次随笔,特记录如下:
- linux内核之情景分析mmap操作
进程可以通过mmap把一个已打开文件映射到用户空间. mmap(void*start,size_t length,int prot,int flags,int fd,off_t offset) sta ...