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.

我的思路:

太混乱了 不提了。注意关键区分依据 排好序的一定是从小到大的

看大神的吧:

bool search(int A[], int n, int key) {
int l = , r = n - ;
while (l <= r) {
int m = l + (r - l)/;
if (A[m] == key) return true; //return m in Search in Rotated Array I
if (A[l] < A[m]) { //left half is sorted 排好序的部分一定是从小到大的
if (A[l] <= key && key < A[m]) //在排好序的这部分
r = m - ;
else
l = m + ;
} else if (A[l] > A[m]) { //right half is sorted
if (A[m] < key && key <= A[r])
l = m + ;
else
r = m - ;
} else l++; //A[l] == A[m] 把l增大1个再循环
}
return false;
}

我的代码,把三个数字都相等的情况单独处理,其他就用无重复处理。 其实我的代码看起来长一些,但是在处理1111111111115这种情况时我的方法优势还是有的

bool search(int A[], int n, int target) {
int l = , r = n - ;
while(l <= r)
{
int m = (l + r) / ;
if(target == A[m])
return true;
else if(A[l] == A[m] && A[m] == A[r]) //三个值相等
{
bool isequalleft = true;
for(int i = l; i < m; i++)
{
if(A[i] != A[i + ])
{
isequalleft = false;
break;
}
}
if(isequalleft) //去掉重复的那一半
l = m + ;
else
r = m - ;
}
else //与不重复的方法相同
{
if (A[l] <= A[m]) {
if (target >= A[l] && target < A[m]) {
r = m - ;
} else {
l = m + ;
}
} else {
if (target > A[m] && target <= A[r]) {
l = m + ;
} else {
r = m - ;
}
}
}
} return false;
}

【leetcode】Search in Rotated Sorted Array II(middle)☆的更多相关文章

  1. 【LeetCode】Search in Rotated Sorted Array II(转)

    原文链接 http://oj.leetcode.com/problems/search-in-rotated-sorted-array-ii/ http://blog.csdn.net/linhuan ...

  2. 【leetcode】Search in Rotated Sorted Array II

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

  3. 【leetcode】Remove Duplicates from Sorted List II (middle)

    Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numb ...

  4. 【leetcode】Search in Rotated Sorted Array

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

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

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

  6. leetCode 81.Search in Rotated Sorted Array II (旋转数组的搜索II) 解题思路和方法

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

  7. 【题解】【数组】【查找】【Leetcode】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 migh ...

  8. 【LeetCode】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 ...

  9. 【leetcode】Search in Rotated Sorted Array (hard)

    Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 migh ...

随机推荐

  1. 基于AngularJS的过滤与排序

    前面了解了AngularJS的使用方法,这里就简单的写个小程序,实现查询过滤以及排序的功能. 本程序中可以了解到: 1 angularjs的过滤器 2 ng-repeat的使用方法 3 控制器的使用 ...

  2. 调用shell脚本,IP处理

    //调用shell脚本,IP处理 package com.letv.sdns.web.utils; import org.slf4j.Logger; import org.slf4j.LoggerFa ...

  3. POJ 1947 Rebuilding Roads

    树形DP..... Rebuilding Roads Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 8188 Accepted: ...

  4. PHP魔术方法以及关于独立实例与相连实例的讲解

    <?php //魔术方法 //当包含多个类 //1.自动装载类的魔术方法__autoload() function __autoload($classname){ if (isset($clas ...

  5. 63.Hbase 常用命令

    1.进入 Hbase shell ./hbase shell 2. 命令 1.list 2.truncate 3.scan 4.describe 5.create 'tablename','famil ...

  6. 【C语言入门教程】1.1 基本程序结构

    基本程序结构就是从上至下顺序执行的程序,C语言程序必须有且只有一个主函数,程序从主函数开始执行,直到主函数结束.下例是根据半径求圆形面积的程序源代码. #include <stdio.h> ...

  7. lua 使用

    根据公司自身业务需要,总结常用到的lua语法 Lua中的string库 链接:http://www.jb51.net/article/57613.htm string.len(s)          ...

  8. 解决Button设置disabled后无法执行后台代码问题

    一.开始调式下面的程序,发现Button在js中设置disabled后无法执行后台代码(btnsave_Click)问题 <asp:Button ID="btnsave" r ...

  9. HDU 3911 线段树区间合并、异或取反操作

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=3911 线段树区间合并的题目,解释一下代码中声明数组的作用: m1是区间内连续1的最长长度,m0是区间内连续 ...

  10. Java多线程基础知识(四)

    一. Condition 接口 1. Condition接口也提供了类似Object的监视器方法,与Lock配合可以实现等待/通知模式. 但是这两者在使用方式以及功能特性上还是有差别的. 2. 支持多 ...