题目

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.

分析

这是一道类似于LeetCode 33的题目,不同的就是这道题有可能出现数字重复,所以,我们不可能像上题那样找到旋转点,然后两次二分查找;

我们可以根据序列特点,以二分查找思想为基础,对该算法进行一定程序的改进。

AC代码

class Solution {
public:
bool search(vector<int>& nums, int target) {
if (nums.empty())
return false; //求所给序列的长度
int len = nums.size(); int lhs = 0, rhs = len - 1;
while (lhs <= rhs)
{
//右移一位减半,提升效能
int mid = (lhs + rhs) >> 1;
if (target == nums[mid])
return true; //若左侧、中间、右侧值相等 则左右同时内移一位
if (nums[lhs] == nums[mid] && nums[mid] == nums[rhs])
{
lhs++;
rhs--;
}//if
else if (nums[lhs] <= nums[mid])
{
if (nums[lhs] <= target && target < nums[mid])
{
rhs = mid - 1;
}
else{
lhs = mid + 1;
}//else
}//elif
else{
if (nums[mid] < target && target <= nums[rhs])
{
lhs = mid + 1;
}//if
else{
rhs = mid - 1;
}//else
}//else
}//while
return false;
}
};

GitHub测试程序源码

LeetCode(81) Search in Rotated Array II的更多相关文章

  1. LeetCode(33)Search in Rotated Sorted Array

    题目 Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 m ...

  2. LeetCode(81): 搜索旋转排序数组 II

    Medium! 题目描述: 假设按照升序排序的数组在预先未知的某个点上进行了旋转. ( 例如,数组 [0,0,1,2,2,5,6] 可能变为 [2,5,6,0,0,1,2] ). 编写一个函数来判断给 ...

  3. [LeetCode] Search in Rotated Array II

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

  4. LeetCode(74) Search a 2D Matrix

    题目 Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the fo ...

  5. LeetCode(35) Search Insert Position

    题目 Given a sorted array and a target value, return the index if the target is found. If not, return ...

  6. LeetCode(34)Search for a Range

    题目 Given a sorted array of integers, find the starting and ending position of a given target value. ...

  7. LeetCode(122):卖股票的最佳时机 II

    Easy! 题目描述: 给定一个数组,它的第 i 个元素是一支给定股票第 i 天的价格. 设计一个算法来计算你所能获取的最大利润.你可以尽可能地完成更多的交易(多次买卖一支股票). 注意:你不能同时参 ...

  8. LeetCode(119):杨辉三角 II

    Easy! 题目描述: 给定一个非负索引 k,其中 k ≤ 33,返回杨辉三角的第 k 行. 在杨辉三角中,每个数是它左上方和右上方的数的和. 示例: 输入: 3 输出: [1,3,3,1] 进阶: ...

  9. LeetCode(154) Find Minimum in Rotated Sorted Array II

    题目 Follow up for "Find Minimum in Rotated Sorted Array": What if duplicates are allowed? W ...

随机推荐

  1. spring boot eureka server

    ServerApplication @EnableEurekaServer @SpringBootApplication public class ServerApplication { public ...

  2. SAE上无法加载css等文件

    如果你的SAE用到了这些文件,你会发现本地虽然能够运行成功,但是SAE上却无法加载. 其实就是地址发生了变化,我们告诉SAE这些东西怎么找就可以了. 例如我的css和js文件放在了app/static ...

  3. adb shell报错:error: insufficient permissions for device的解决办法

    1.错误描述 执行 adb shell 时,报错如下; error: insufficient permissions for device  2.解决办法 1,终端执行 lsusb 结果如下,注意绿 ...

  4. h5-16-插入SVG图片

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  5. ping localhost出现地址::1

    近期发现本机的localhost不好用了. 症状: 自己本机部署服务器时,浏览器地址栏访问localhost:8080不通: 禁用网络连接,未果: 拔出网线,OK. cmd里ping之,返回结果如下: ...

  6. [转]彻底明确怎样设置minSdkVersion和targetSdkVersion

    minSdkVersion和targetSdkVersion相信非常多人都不太理解.我在网上也看了很多关于这两者差别的文章,感觉说的都非常模糊.直到我在stackOverFlow看到Android M ...

  7. 复习-PEP8规范(转)

    PEP8 Python 编码规范 一 代码编排1 缩进.4个空格的缩进(编辑器都可以完成此功能),不使用Tap,更不能混合使用Tap和空格.2 每行最大长度79,换行可以使用反斜杠,最好使用圆括号.换 ...

  8. TabLayout.Tab(自定义)点击事件

    TabLayout是官方design包中的一个布局控件,这里不介绍它的基本使用,只是解决Tab(自定义)点击事件. //获取Tab的数量 Int tabCount = tabLayout.getTab ...

  9. 解决Ueditor在bootstarp 模态框中全屏问题

    基本的一些配置就不说了.先说一下要注意的问题:首先是zIndex的设置.记住最好都显示设置模态框和ueditor的zIndex.理清他们的层叠关系. 特别是用到ueditor里面的图片上传功能的更要设 ...

  10. 07/29/2013 02:10:02 AM - CMDPHP: Poller[0] Host[6] DS[10] WARNING: Result from SNMP not valid. Partial Result: U

    snmpwalk -c public -v2c  客户端ip地址  自定义的oid  能取到数据,但是服务器端就是图片一片空白 rrdtool fetch 文件名.rrd 查看到的全都是nan cac ...