description:

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.

Thoughts:

1.按照写rotated sorteg array时的思路,我写这个问题的时候,在重新设置low和high的时候,跳过了和middle一样的重复部分。另外要注意的一个点就是当nums[low]和nums[high]相等的时候我们要重新设置low和high

public boolean search(int[] nums, int target){
int low = 0;
int high = nums.length-1;
if(nums[high] == nums[low]&&nums.length>2){
for(int i = high-1;i>=low;i--){
if(nums[i] == nums[high]){
high--;
}
}
}
while(low <= high){
int middle = (low+high)/2;
if(nums[middle] == target){
return true;
}else if(nums[middle] >=nums[low]){
if(target>=nums[low] && target<nums[middle]){
for(int i = middle-1;i>=low;i--){
if(nums[i] == nums[middle]){
middle--;
}else{
break;
}
}
high = middle -1;
}else{
for(int i = middle+1;i<=high;i++){
if(nums[i] == nums[middle]){
middle++;
}else{
break;
}
}
low = middle + 1;
}
}else{
if(target>nums[middle]&&target<=nums[high]){
for(int i =middle+1;i<=high;i++){
if(nums[i] == nums[middle]){
middle++;
}else{
break;
} }
low = middle +1;
}else{
for(int i = middle-1;i>=low;i--){
if(nums[i] == nums[middle]){
middle--;
}else{
break;
} }
high = middle - 1;
}
}
}
return false;
}

2.前面的解法,思路是清晰的过程是麻烦的,需要加很多的判断条件,为了避免这个问题,我们不讲middle和low进行比较,而是让它和high进行比较,这样就能够避免之前的nums[low]和nums[high]相等的情况,所带来的麻烦。

public boolean search2(int[] nums, int target){
if(nums.length == 0){
return false;
}
int low = 0;
int high = nums.length - 1;
while(low < high){
int middle = (low +high)/2;
if(nums[middle] == target){
return true;
}else{
if(nums[middle] < nums[high]){
if(target > nums[middle] && target <= nums[high]){
low = middle + 1;
}else{
high = middle - 1;
}
}else if(nums[middle] > nums[high]){
if(target >= nums[low] & target < nums[middle]){
high = middle -1;
}else{
low = middle +1;
}
}else{
high--;
}
}
}
return nums[low]==target;
}

Search in rotated array two的更多相关文章

  1. [LeetCode] Search in Rotated Array II

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

  2. LeetCode(81) Search in Rotated Array II

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

  3. [LeetCode] Search in Rotated Array

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

  4. 【leetcode】Search in Rotated Sorted Array II(middle)☆

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

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

  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 在旋转有序数组中搜索

    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 II

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

  9. 【leetcode】Search in Rotated Sorted Array

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

随机推荐

  1. TensorFlow安装配置,茫茫人海中一瞥

    深度学习的框架,我们熟知的有caffe,torch和convnet.最近,Google又搞了一个TensorFlow,已经开源:http://www.tensorflow.org/.据说,谷歌的深度学 ...

  2. HTML5 classList API接口

    原文地址:HTML5 classList API 原文日期: 2010年07月13日 翻译日期: 2013年08月23日 当我陷入JavaScrip和JavaScript类库框架之中时,我总是有种希望 ...

  3. Linux C系统编程:信号与定时器的使用

    #include <stdio.h> #include <signal.h> void do_alarm(int num); int main(void) { //注册一个定时 ...

  4. 【Python】Shell MD5使用的那些事

    MD5 应该是用的非常多的算法,就自己使用经验说说吧. 场景 算法层面不多说了,维基百科,还有很多文章都有说明. 主要用过的场景 密码存储,现在基本没怎么有使用的了,毕竟破解容易了很多 API校验,现 ...

  5. Java进阶(五十一)Could not create the view: An unexpected exception was thrown

    Java进阶(五十一)Could not create the view: An unexpected exception was thrown 今天打开Myeclipse10的时候,发现server ...

  6. mysql进阶(十二)常见错误汇总

    原因:外键名不能重复

  7. android报错 Expected BEGIN_OBJECT but was STRING at line 1 column 39 path $

    我在使用retrofit和Gson配合时,出现了这个问题,疑惑中乱七八糟瞎搞了一个下午没有解决.期间怀疑Gson解析不能使用泛型(因为我的解析使用了泛型),后来又觉得可能是我的关键字正好是解析器的某个 ...

  8. 生产者消费者的java实现

    先看最简单的,也就是缓冲区的容量为1 缓冲区容量为1 import java.util.List; public class ProducerAndConsumer2 { static class A ...

  9. iOS中UIKit的外观属性及方法汇总

    这里将UIKit的外观属性及方法从头文件中抽取出来,以便查找及熟悉.(更新到iOS 8.0,从A-Z排序) UIActivityIndicatorView @property (readwrite,  ...

  10. Material Design5.x动画实现解析篇一

    Material Design设计语言动画篇共推出六种类型的动画效果: 1.Touch feedback(触摸反馈) 2.Reveal effect(揭露效果) 3.Activity transiti ...