题目

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. 使用selesium和pytesseract识别验证码,达到登录网页目的

    关于验证码问题,大多可以在网上了解到目前有四种解决方案:1.开发注释验证码2.开发开一个“后门”,设置一个万能码,输入万能码则通过3.通过cookies绕过验证码4.图形识别技术 前三种是比较快速也是 ...

  2. 找不到javax.servlet.Filter的类文件

    在这里我是用IDEA来开发的,Tomcat用的maven插件 原因:没有相应在jar包 解决:导入相应在jar的依赖,在pom文件中添加 <dependency> <groupId& ...

  3. AWVS11使用教程——Acunetix Web Vulnerability Scanner 11.x

    AWVS11使用教程 一:普通扫描. 二:报告生成. 三:登陆扫描. Acunetix Web Vulnerability Scanner(简称AWVS)是一款知名的网络漏洞扫描工具,它通过网络爬虫测 ...

  4. android动画(2)自定义动画

    public class CustomAnimation extends Animation { // 这个方法可以获得动画view的width,height,以及它父view的width @Over ...

  5. 正则表达式test报错 is not a function

    var reg = "/^1[34578]\d{9}$/"; //错误格式,这是一个字符串 var reg2 = /^1[34578]\d{9}$/; //正确格式 reg .te ...

  6. AJPFX总结OpenJDK 和 HashMap大量数据处理时,避免垃圾回收延迟的技巧二

    HashMap简史 “Hash Code”这个概念第一次出现是在1953年1月的<Computing literature>中,H. P. Luhn  (1896-1964) 在一篇 IB ...

  7. Maximal Discount

    Description: Linda is a shopaholic. Whenever there is a discount of the kind where you can buy three ...

  8. android studio MQTT测试成功

    package myself.mqtt.wenzheng.studio.mqtt; import android.app.Notification; import android.app.Notifi ...

  9. 初试springWebMVC

    最近在尝试配置SpringMVC,发现各种坑. 首先遇到了这个问题. 'component-scan' and its parser class [org.springframework.contex ...

  10. 登录脚本重构Element

    登录脚本重构Element package com.gubai.selenium; import org.openqa.selenium.By; import org.openqa.selenium. ...