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.

33题的区别就是http://www.cnblogs.com/li-daphne/p/5566373.html

就是数组中的数可以是重复的。

比如

/**
1. 1.. 1... 3 4 5
5 1. 1.. 1... 2 3 4
4 5 1. 1.. 1... 2 3
3 4 5 1. 1.. 1... 2
2 3 4 1. 1.. 1...
1... 2 3 5 1. 1..
1.. 1... 2 4 5 1.
*/

1 3 1 1

1 1 1 1

1 1 3 1

1 1 1 3

3 1 1 1

图画的不好,完美的避开了所有演示。

在将nums利用mid分为左右两部分后,你不能通过nums[mid]<num[right]来判断右边是有序的,或利用nums[mid]>nums[right]判断左边是有序的。

如果nums[mid]<=nums[right]分为两部分:

  如果nums[mid]<nums[right],则右边是有序,利用排除法现在有序的部分搜索;后再无序的部分搜索。

  如果nums[mid]==nums[right],right--后再看下一步就行了。

-----

bool search_2(vector<int> &nums,int target){
if(nums.size()==) return false;
int left = ;
int right = nums.size()-;
while(left<=right){
int mid = (left+right)/;
if(nums[mid]==target) return true;
else if(nums[mid]<nums[right]){
            //我们已经直到数组的右边是有序的,
if(nums[mid]<target && target <=nums[right])//判断有序的数组是不是存在target,方法是?
left = mid+;
else
right = mid-;
}else if(nums[mid]>nums[right]){
            //这里我们假定数组左边的是有序的
if(target >=nums[left] && target<=nums[mid])///判断左边 有序的数组是不是存在target,方法是?
right = mid-;
else
left = mid+;
}else{
            ///nums[mid]==nums[right], right左移一位,再试一试
right--;
}
}
return false;
}

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

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

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

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

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

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

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

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

  6. LeetCode 81. Search in Rotated Sorted Array II(在旋转有序序列中搜索之二)

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

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

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

  8. [leetcode]81. Search in Rotated Sorted Array II旋转过有序数组里找目标值II(有重)

    This is a follow up problem to Search in Rotated Sorted Array, where nums may contain duplicates. 思路 ...

  9. 81. Search in Rotated Sorted Array II (Array; Divide-and-Conquer)

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

随机推荐

  1. 如何在ALV_Grid的函数中定义下拉列表

    转自 http://www.cnblogs.com/VerySky/articles/2392262.htmlABAP--如何在ALV_Grid的函数中定义下拉列表 REPORT. ********* ...

  2. POJ 3268 Silver Cow Party (双向dijkstra)

    题目链接:http://poj.org/problem?id=3268 Silver Cow Party Time Limit: 2000MS   Memory Limit: 65536K Total ...

  3. hdu2546 饭卡    01背包

    link:http://acm.hdu.edu.cn/showproblem.php?pid=2546 也算一个贪心的想法吧. 先把总钱数减去5,再把价值最大的挑出来.然后用01背包.最终买下挑出来的 ...

  4. eBay_Relist(退换刊登费)

    如果物品首次刊登结束时没有人中标,或是买家并没有付款完成交易,那么卖家以通过重新刊登的方法来再次销售.如果该物品在第二次刊登时成功售出,且满足eBay的重新刊登退费条件,那么eBay便会退还重新刊登的 ...

  5. JS初学之-js的a:hover

    <!doctype html><html><head><meta charset="utf-8"><title>无标题文 ...

  6. linux中进程控制

    1.进程标识 每个进程都有一个非负整型表示的唯一的进程ID.进程ID标识符总是唯一的.  虽然进程ID是唯一的,但某个ID被回收后,ID号是可以复用的. ID为0的进程通常是调度进程(其常常被称交换进 ...

  7. UVa 二叉树的编号(二叉树)

    原题链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...

  8. 越狱Season 1-Episode 8: The Old Head

    Season 1, Episode 8: The Old Head -Michael: 17 days from now they strap my brother to an electric ch ...

  9. GDI+中GIF图片的显示

    某位网友曾经问过我GDI+中Gif图像显示的问题,一直没时间给你写,在此致歉.我把这篇文章送给他. 一.GIF格式介绍 1.概述 GIF(Graphics Interchange Format,图形交 ...

  10. [原创]cocos2d-x研习录-第一阶 背景介绍 之 cocos2d家族史

    Cocos2D是一个2D开源游戏引擎,它最早是由Ricardo Quesada(阿根廷人,社区简称Riq)和他的朋友们用Python开发的,用于开发2D游戏和基于2D图形的任何应用.最早引擎的名字源自 ...