Search in Rotated Sorted Array II

Total Accepted: 18500 Total
Submissions: 59945My Submissions

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.

题意:一个已经排序好的数组。被按某个位置旋转了一次,给定一个值target,在该旋转后的数组里查找该值。(数组中的元素可能反复)

思路:二分查找

难点在于确定往数组的哪一半段继续二分查找

设起点、中间点、终点分别为 start、middle、end (採用前闭后开的区间表示方法

假设target = A[middle] return middle

假设A[middle] > A[start],则[start,middle)单调递增

1.假设target < A[middle] && target >= A[start],则 end = middle

2.start = middle + 1, otherwise

假设A[middle] < A[start],则[middle,end)单调递增

1.假设target > A[middle] && target <= A[end - 1],则 start = middle + 1

2.end = middle, otherwise

假设A[middle] == A[start]。那A[start]也不会是target,能够通过start++; 去掉A[start]





复杂度:时间O(n),空间O(1)

int search(int A[], int n, int target){
int start = 0, end = n, middle ;
while(start < end){
middle = (start + end) / 2;
if(A[middle] == target) return middle;
if(A[middle] > A[start]){
if(target >= A[start] && target < A[middle]){
end = middle;
}else{
start = middle + 1;
}
}else if(A[middle] < A[start]){
if(target > A[middle] && target <= A[end - 1]){
start = middle + 1;
}else{
end = middle;
}
}
else{
++start;
}
}
return -1;
}

leetcode 二分查找 Search in Rotated Sorted ArrayII的更多相关文章

  1. leetcode 二分查找 Search in Rotated Sorted Array

    Search in Rotated Sorted Array Total Accepted: 28132 Total Submissions: 98526My Submissions Suppose ...

  2. Search in Sorted Array,Search in Rotated Sorted Array,Search in Rotated Sorted ArrayII

    一:Search in Sorted Array 二分查找,可有重复元素,返回target所在的位置,只需返回其中一个位置,代码中的查找范围为[low,high),左闭右开,否则容易照成死循环. 代码 ...

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

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

  4. leetcode 题解:Search in Rotated Sorted Array II (旋转已排序数组查找2)

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

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

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

  6. leetcode解题报告(4):Search in Rotated Sorted ArrayII

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

  7. [Leetcode][Python]33: Search in Rotated Sorted Array

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 33: Search in Rotated Sorted Arrayhttps ...

  8. LeetCode题解33.Search in Rotated Sorted Array

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

  9. LeetCode解题报告—— Search in Rotated Sorted Array & Search for a Range & Valid Sudoku

    1. Search in Rotated Sorted Array Suppose an array sorted in ascending order is rotated(轮流,循环) at so ...

随机推荐

  1. [leetcode greedy]134. Gas Station

    There are N gas stations along a circular route, where the amount of gas at station i is gas[i]. You ...

  2. python 全排列

    itertools模块现成的全排列: for i in itertools.permutations('abcd',4): print ''.join(i) 相关全排列算法: def perm(l): ...

  3. 内容播放colorbox

    1.需要的js (1)jquery (2)colorbox (http://www.jacklmoore.com/colorbox/ 下载文件夹,其中有js.css文件) //加载的时候注意文件的路径 ...

  4. android 数据存储方式

    韩梦飞沙  韩亚飞  313134555@qq.com  yue31313  han_meng_fei_sha 1,文件 2,内容提供者 3,偏好设置 4,数据库 5,网络存储. 网络存储,就是上传到 ...

  5. [Luogu5106]dkw的lcm

    https://minamoto.blog.luogu.org/solution-p5106 容易想到枚举质因子及其次数计算其贡献,容斥计算$\varphi(p^i)$的次方数. #include&l ...

  6. 【初识】KMP算法入门

    举个例子 模式串S:a s d a s d a s d f a s d 匹配串T:a s d a s d f 如果使用朴素匹配算法—— 1 2 3 4 5 6  8 9 a s d a s d a s ...

  7. python开发_tkinter_获取单选菜单值

    在之前的blog中有提到python的tkinter中的菜单操作 python开发_tkinter_窗口控件_自己制作的Python IDEL_博主推荐 python开发_tkinter_窗口控件_自 ...

  8. CI下php操作memcached 的问题

    CI下php操作memcached 的时候,获取memcached里不存在的key时候,会出错,然而放在程序的最后一行就不会出错,不知何故,mark一下.

  9. UploadFileUtil

    package cn.tz.util.file; import java.io.File; import java.io.FileOutputStream; import java.io.InputS ...

  10. 不同的activity使用bundle对象传值给广播接收器

    解决了一下午的问题,广播机制传值,在一个activity中发送广播给广播接收器,使用的是同一个action 在另一个activity中如果也发送广播给同一个广播接收器,使用相同的action,会导致后 ...