Question:

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.

Solution:

 class Solution {
public:
bool search(vector<int>& nums, int target)
{
int left=;
int right=nums.size()-;
if(left==right && nums[left]==target)
return true;
while(left!=right)
{
int mid=(left+right)/;
if(nums[mid]==target) return true;
if(nums[left]<nums[mid])
{
if(nums[left]<=target && target<=nums[mid])
right=mid;
else
left=mid+;
}
else if(nums[left]>nums[mid])
{
if(nums[mid]<=target && target<=nums[right])
left=mid+;
else
right=mid;
}
else
left++;
if(left==right && nums[left]==target)
return true;
}
return false;
}
};

Search in Rotated Sorted Array II的更多相关文章

  1. 【leetcode】Search in Rotated Sorted Array II

    Search in Rotated Sorted Array II Follow up for "Search in Rotated Sorted Array":What if d ...

  2. 49. Search in Rotated Sorted Array && Search in Rotated Sorted Array II

    Search in Rotated Sorted Array Suppose a sorted array is rotated at some pivot unknown to you before ...

  3. LeetCode 81 Search in Rotated Sorted Array II [binary search] <c++>

    LeetCode 81 Search in Rotated Sorted Array II [binary search] <c++> 给出排序好的一维有重复元素的数组,随机取一个位置断开 ...

  4. 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 ...

  5. 33. Search in Rotated Sorted Array & 81. Search in Rotated Sorted Array II

    33. Search in Rotated Sorted Array Suppose an array sorted in ascending order is rotated at some piv ...

  6. LeetCode: Search in Rotated Sorted Array II 解题报告

    Search in Rotated Sorted Array II Follow up for "LeetCode: Search in Rotated Sorted Array 解题报告& ...

  7. 【LeetCode】81. Search in Rotated Sorted Array II (2 solutions)

    Search in Rotated Sorted Array II Follow up for "Search in Rotated Sorted Array":What if d ...

  8. LeetCode OJ:Search in Rotated Sorted Array II(翻转排序数组的查找)

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

  9. [LeetCode] Search in Rotated Sorted Array II 在旋转有序数组中搜索之二

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

  10. 【leetcode】Search in Rotated Sorted Array II(middle)☆

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

随机推荐

  1. 安装wps for linux无法启动

    我下载的是deb包,双击安装完之后,打开wps没有反应,重启了一下 计算机也不行. 改从命令行出现如下信息: /opt/kingsoft/wps-office/office6/wps: error w ...

  2. WCF分布式开发步步为赢(5)服务契约与操作重载

    继上一节WCF分布式开发步步为赢系列的(4):WCF服务可靠性传输配置与编程开发,本节我们继续学习WCF分布式开发步步为赢的第(5)节:服务契约与操作重载.这里我们首先讲解OOP面向对象的编程中方法重 ...

  3. (3)初次接触off

    boss布置任务了,要读入off文件,生成能显示出来的可执行文件,完成不了就要滚蛋 目前的东西还是不用保密的,到后面我就要设密码了 好,.off文件是什么? OFF,Object File Forma ...

  4. proc插入数据到数据库

    #include<stdio.h>EXEC SQL INCLUDE SQLCA; void insert (char password_[6],char id_[20],int balan ...

  5. iOS开发之都兴忱小结

    1.NSArray/NSDictionary ------> strong temp和self.arr是同一地址. 2.NSArray/NSDictionary ------->copy ...

  6. lintcode:玩具工厂

    题目 工厂模式是一种常见的设计模式.请实现一个玩具工厂 ToyFactory 用来产生不同的玩具类.可以假设只有猫和狗两种玩具.   样例 ToyFactory tf = ToyFactory(); ...

  7. ubuntu挂载磁盘

    1.首先查磁盘UUID:sudo blkid 2.打开挂载文件:sudo /etc/fstab 3.写挂载文件: UUID=000860AE000FDD66 /mnt/disk1            ...

  8. 8 simple things that will make you sexy

    8 simple things that will make you sexy8种方法教你不动声色的性感What makes a women sexy? Is it her body? Is it t ...

  9. awk过滤统计不重复的行

    awk以‘\t’为分隔符区分列 cat logs | grep IconsendRedirect | grep 1752 | awk -F'\t' '{print $8}'| wc -l awk过滤统 ...

  10. CentOS编译安装Python3

    前话 最近想学一下一门新的高级语言,无意中看到用python仿AIphaGo的github项目,就决定是他了. AIphaGo的Git传送门: https://github.com/Rochester ...