Search in rotated array two
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的更多相关文章
- [LeetCode] Search in Rotated Array II
Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would this ...
- LeetCode(81) Search in Rotated Array II
题目 Follow up for "Search in Rotated Sorted Array": What if duplicates are allowed? Would t ...
- [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 ...
- 【leetcode】Search in Rotated Sorted Array II(middle)☆
Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would this ...
- 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 ...
- [LeetCode] Search in Rotated Sorted Array II 在旋转有序数组中搜索之二
Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would this ...
- [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 ...
- 【leetcode】Search in Rotated Sorted Array II
Search in Rotated Sorted Array II Follow up for "Search in Rotated Sorted Array":What if d ...
- 【leetcode】Search in Rotated Sorted Array
Search in Rotated Sorted Array Suppose a sorted array is rotated at some pivot unknown to you before ...
随机推荐
- SpringMVC项目中启动自加载Listener
package com.kuman.cartoon.listener; import java.util.List; import org.springframework.beans.factory. ...
- ACM竞赛:立方和问题
例如: 输入: n 代表多组数组 num1 , num 2 ep: 1 3 这时的算法结果应当为: 1 ^ 3 + 2 ^ 3 + 3 ^ 3 ep : 2 5 这时的算法结果应当为: ...
- 【leetcode】经典算法题-Counting Bits
题目描述: 给定一个数字n,统计0-n之间的数字二进制的1的个数,并用数组输出 例子: For num = 5 you should return [0,1,1,2,1,2]. 要求: 算法复杂复o( ...
- C语言实现万年历
给出你想知道的年份,便可以计算出该年对应的每个月每个日所对应的星期数,是不是感觉很好玩 ? #include <stdio.h> #include<stdlib.h> long ...
- ISLR系列:(4.2)模型选择 Ridge Regression & the Lasso
Linear Model Selection and Regularization 此博文是 An Introduction to Statistical Learning with Applicat ...
- AngularJS进阶(二十九)AngularJS项目开发技巧之localStorage存储
AngularJS项目开发技巧之localStorage存储 注: localStorage深度学习 绪 项目开发完毕,测试阶段发现后台管理端二维码生成有问题,问题在于localStora ...
- AngularJS进阶(二十四)AngularJS与单选框及多选框的双向动态绑定
AngularJS与单选框及多选框的双向动态绑定 赠人玫瑰,手留余香.若您感觉此篇博文对您有用,请花费2秒时间点个赞,您的鼓励是我不断前进的动力,共勉! AngularJS 在 <in ...
- window环境下搭建react native及相关插件
可以先浏览一下中文翻译的开发文档具体了解一下关于React Native,想要查看官方文档可以点http://facebook.github.io/react-native/docs/getting- ...
- 优雅的App完全退出方案(没有任何内存泄漏隐患)
在Android开发过程中,特别是界面比较多的情况下,用平常的退出方式往往是不能完全退出这个应用,网络上也好多各种退出方案.其中一种应该是被广大开发者采纳使用,也非常的清晰方便,就是在Applicat ...
- C语言之数值计算--级数算法
在编程语言的学习中,我们学习过不少的算法,比如累加,累乘,数值交换,排序等等.在一些软件比赛和面试题中,有一类算法不容忽视,属于高频题目,我之前去企业面试的时候就遇到这样的一类题目,题目不算难,掌握方 ...