称号

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.

原题链接(点我)

解题思路

这题和Search in Rotated Sorted Array问题类似。只是这个题同意元素反复。假设有元素反复,此时採取最保守的策略。一次缩小一个范围。

代码实现

class Solution {
public:
bool search(int A[], int n, int target) {
if(A==NULL || n<=0) return false;
int begin = 0, end = n-1, mid = 0;
while(begin<=end){
mid = (begin+end)/2;
if(A[mid] == target) return true;
if(A[mid] < A[end]){
//后半段有序
if(A[end]>=target && A[mid]<target)
begin = mid+1;
else
end = mid - 1;
}else if(A[mid] > A[end]){
//前半段有序
if(A[begin]<=target && A[mid] > target)
end = mid-1;
else
begin = mid+1;
}else
// 最保守策略,缩小一个范围
--end;
}
return false;
}
};
假设你认为本篇对你有收获。请帮顶。

另外。我开通了微信公众号--分享技术之美,我会不定期的分享一些我学习的东西.

你能够搜索公众号:swalge 或者扫描下方二维码关注我

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvc3dhZ2xl/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="">

(转载文章请注明出处: http://blog.csdn.net/swagle/article/details/30487759
)

版权声明:本文博主原创文章。博客,未经同意不得转载。

[LeetCode] Search in Rotated Sorted Array II [36]的更多相关文章

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

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

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

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

  3. LeetCode——Search in Rotated Sorted Array II

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

  4. [leetcode]Search in Rotated Sorted Array II @ Python

    原题地址:https://oj.leetcode.com/problems/search-in-rotated-sorted-array-ii/ 题意: Follow up for "Sea ...

  5. [Leetcode] search in rotated sorted array ii 搜索旋转有序数组

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

  6. [LeetCode] Search in Rotated Sorted Array II 二分搜索

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

  7. LeetCode Search in Rotated Sorted Array II -- 有重复的旋转序列搜索

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

  8. LeetCode:Search in Rotated Sorted Array I II

    LeetCode:Search in Rotated Sorted Array Suppose a sorted array is rotated at some pivot unknown to y ...

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

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

随机推荐

  1. 我的EJB学习历程

    http://blog.csdn.net/sinapaper/archive/2004/06/28/28659.aspx http://blog.csdn.net/sinapaper/archive/ ...

  2. hdu3555(数位dp)

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=3555 题意:求区间[a,b]内包含有'49'的数的总个数. 分析:dp[pos][0]表示到第pos位 ...

  3. Java正則表達式演示样例

    import java.util.regex.Matcher; import java.util.regex.Pattern; public class RegexMatches { public s ...

  4. Navicat工具破解

        Navicat提供多达 7 种语言供客户选择,被公认为全球最受欢迎的数据库前端用户介面工具.它可以用来对本机或远程的 MySQL.SQL Server.SQLite.Oracle 及 Post ...

  5. 采用keepalived施工可用性MySQL-HA

    描述了使用keepalived施工可用性MySQL-HA,两个保证MySQL数据一致性,然后,keepalived虚拟IP,经keepalived内置的在线监测功能来实现MySQL. AD: 关于My ...

  6. 移动web性能优化笔记

    移动web性能优化 最近看了一些文章,对移动web性能优化方法,做一个简单笔记 笔记内容主要出自 移动H5前端性能优化指南和移动前端系列——移动页面性能优化

  7. hdu 4107当卡段树

    其核心思想是记录最大的节点值和最低值,假设max<p要么min>=p时间,在节点只变化add值,不要子树遍历:否则,就往子树递归. #include<iostream> #in ...

  8. IOS加强知识(1)理解力Objective-C

    一直想写一般Objective-C帖子,总是没时间.所以,我希望有一个巨大的知识更小.温馨提示小的变化.写一点点,每天.东西把他们的学习分享,好了废话不多. 1.一门动态的语言OC Object-C( ...

  9. 通常编译亲测56Y国际象棋源代码,精仿56Y国际象棋完整的源代码下载!

    今天公布亲测通常应编译56Y国际象棋源代码,精仿56Y牌源代码.喜欢的能够拿去研究.论坛资源太多.我们会把好的资源都公布出来,同一时候欢迎很多其它的程序猿增加我们! 增加我们的共同学习交流!     ...

  10. 获取listboxitem在ListBox中的index并转换成abcd

    原文 获取listboxitem在ListBox中的index并转换成abcd 截图如下: 1.实现Converter  获取到listbox,并得到listitem在listbox中的index p ...