题目

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. 改变UITabbar顶部分割线颜色

    项目中是使用UITabbarController 因此改变UITabbar的分割线代码如下 由于美术没提供图片,所以自己创建了个图片 //改变tabbar 线条颜色 CGRect rect = CGR ...

  2. Codeforces Round #321 (Div. 2) A, B, C, D, E

    580A. Kefa and First Steps 题目链接: A. Kefa and First Steps 题意描述: 给出一个序列,求最长不降连续子序列多长? 解题思路: 水题,签到 代码: ...

  3. Codeforces Round #261 (Div. 2) D

    Description Parmida is a clever girl and she wants to participate in Olympiads this year. Of course ...

  4. Mysql常用命令行大全(转)

    第一招.mysql服务的启动和停止 net stop mysql net start mysql 第二招.登陆mysql 语法如下: mysql -u用户名 -p用户密码 键入命令mysql -uro ...

  5. Hadoop调度框架

        大数据协作框架是一个桐城,就是Hadoop2生态系统中几个辅助的Hadoop2.x框架.主要如下: 1,数据转换工具Sqoop 2,文件搜集框架Flume 3,任务调度框架Oozie 4,大数 ...

  6. 【ADO.NET】 基础 (SQL Server)

    一.Web.config配置 <connectionStrings> <add name="constr_name" connectionString=" ...

  7. 如何用sql server数据库恢复.bak数据库备份

    @hcy(黄灿奕) 之前有两次都恢复不了,折腾了很长时间,这一次碰到这样的问题,居然又忘了,又捣鼓了很长时间,现在记下来 1.右击SQL Server 2008实例下的“数据库”文件夹.就是与安全性. ...

  8. makefile vpath变量

    在讲vpath之前,我们首先了解以下makefile文件. 在类Unix系统中,当我们使用源码编译某个软件的时候,我们会使用confiure,make,make install这三个命令,其中cofi ...

  9. leetcode_935. Knight Dialer_动态规划_矩阵快速幂

    https://leetcode.com/problems/knight-dialer/ 在如下图的拨号键盘上,初始在键盘中任意位置,按照国际象棋中骑士(中国象棋中马)的走法走N-1步,能拨出多少种不 ...

  10. DoveCLL and Resistance(湖北省赛)

    题目 构造一个由串联和并联构成的纯电阻电路,使得该电路的等效电阻为p/q,其中(p,q) = 1.要求输出n,表示节点数,m表示电阻个数,m行每行ui,vi,wi,表示ui和vi之间要连上一个电阻为w ...