题目:

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

Would this affect the run-time complexity? How and why?

Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand.

(i.e., 0 1 2 4 5 6 7 might become 4 5 6 7 0 1 2).

Write a function to determine if a given target is in the array.

The array may contain duplicates.

题解:

Solution 1

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

【LeetCode】081. Search in Rotated Sorted Array II的更多相关文章

  1. 【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 ...

  2. 【LeetCode】81. Search in Rotated Sorted Array II 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/search-in ...

  3. 【Leetcode】81. Search in Rotated Sorted Array II

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

  4. 【一天一道LeetCode】#81. Search in Rotated Sorted Array II

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Follow ...

  5. 【LeetCode】33. Search in Rotated Sorted Array (4 solutions)

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

  6. 【leetcode】Find Minimum in Rotated Sorted Array II JAVA实现

    一.题目描述 Follow up for "Find Minimum in Rotated Sorted Array":What if duplicates are allowed ...

  7. 【LeetCode】33. Search in Rotated Sorted Array 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  8. 【Leetcode】33. Search in Rotated Sorted Array

    Question: Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforeh ...

  9. 【LeetCode】033. Search in Rotated Sorted Array

    题目: Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. ( ...

随机推荐

  1. 【BZOJ3302】[Shoi2005]树的双中心 DFS

    [BZOJ3302][Shoi2005]树的双中心 Description Input 第一行为N,1<N<=50000,表示树的节点数目,树的节点从1到N编号.接下来N-1行,每行两个整 ...

  2. H - Funny Car Racing

    H - Funny Car Racing Time Limit:1000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Desc ...

  3. ABAP xml

    [转]Part 1 - Expressiveness of Simple TransformationsSimple Transformations are a SAP proprietary pro ...

  4. 磁盘分区对齐详解与配置 – Linux篇

    在之前一篇<磁盘分区对齐详解与配置 – Windows篇>中,我介绍了磁盘分区对齐的作用和适用于MBR和GPT的两种磁盘类型的配置,以及Windows平台设置磁盘分区对齐的方法. 本文作为 ...

  5. Python 不可变对象

    str是不变对象,而list是可变对象. 对于可变对象,比如list,对list进行操作,list内部的内容是会变化的,比如: >>> a = ['c', 'b', 'a'] > ...

  6. 每天一个Linux命令(28)df命令

    报告文件系统磁盘空间的使用情况.获取硬盘被占用了多少空间,目前还剩下多少空间等信息.       (1)用法:       用法:  df [选项] [文件]       (2)功能: 功能:  显示 ...

  7. Data Structure Linked List: Detect and Remove Loop in a Linked List

    http://www.geeksforgeeks.org/detect-and-remove-loop-in-a-linked-list/ #include <iostream> #inc ...

  8. 《程序员代码面试指南》第一章 栈和队列 设计一个有getMin功能的栈

    题目 实现一个特殊的栈,在实现栈的基本功能上,再实现返回栈中最小的元素的操作 要求 1. pop.push.getMin操作时间复杂度都是O(1) 2. 设计的栈类型可以使用现成的栈结构 java代码 ...

  9. 奥森图标和CSS特殊字体使用方法

    作为第一篇博文,写这个 我快要被气炸,好吧,废话不说了 昨天在项目中发现有很多这些Awesome图标 也在网上找了下Font Awesome下载后这些文件,现在的版本是4.2,Font Awesome ...

  10. 【leetcode刷题笔记】Evaluate Reverse Polish Notation

    Evaluate the value of an arithmetic expression in Reverse Polish Notation. Valid operators are +, -, ...