81. Search in Rotated Sorted Array II (JAVA)
Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand.
(i.e., [0,0,1,2,2,5,6]
might become [2,5,6,0,0,1,2]
).
You are given a target value to search. If found in the array return true
, otherwise return false
.
Example 1:
Input: nums = [2,5,6,0,0,1,2]
, target = 0
Output: true
Example 2:
Input: nums = [2,5,6,0,0,1,2]
, target = 3
Output: false
Follow up:
- This is a follow up problem to Search in Rotated Sorted Array, where
nums
may contain duplicates. - Would this affect the run-time complexity? How and why?
思路:因为有重复元素,所以不能通过之前的方式判断rotate的地方,需要单独考虑nums[mid] == nums[start]以及nums[mid] == nums[end]的情况,这两种情况分别通过start+1和end-1跳过重复元素。
时间复杂度:在重复元素不多的情况下,并不很影响时间复杂度;担当每次都走的nums[mid] == nums[start]以及nums[mid] == nums[end],那么时间复杂度从O(logn)增至O(n)
class Solution {
public boolean search(int[] nums, int target) {
return binarySearch(nums,target,0,nums.length-1);
} public boolean binarySearch(int[] nums, int target, int start, int end){
if(start > end) return false; int mid = start + ((end-start)>>1);
if(nums[mid] == target) return true; if(nums[mid] < nums[start]){ //rotate in the left part
if(target >= nums[start] || target < nums[mid]) return binarySearch(nums, target, start, mid-1);
else return binarySearch(nums, target, mid+1, end);
}
else if(nums[mid] > nums[end]){ //rotate in the right part
if(target > nums[mid] || target <= nums[end]) return binarySearch(nums, target, mid+1, end);
else return binarySearch(nums, target, start, mid-1);
}
else if(nums[mid] == nums[start]){
return binarySearch(nums, target, start+1, end);
}
else if(nums[mid] == nums[end]){
return binarySearch(nums, target, start, end-1);
}
else{
if(target > nums[mid]) return binarySearch(nums, target, mid+1, end);
else return binarySearch(nums, target, start, mid-1);
}
}
}
81. Search in Rotated Sorted Array II (JAVA)的更多相关文章
- LeetCode 81 Search in Rotated Sorted Array II [binary search] <c++>
LeetCode 81 Search in Rotated Sorted Array II [binary search] <c++> 给出排序好的一维有重复元素的数组,随机取一个位置断开 ...
- leetcode 153. Find Minimum in Rotated Sorted Array 、154. Find Minimum in Rotated Sorted Array II 、33. Search in Rotated Sorted Array 、81. Search in Rotated Sorted Array II 、704. Binary Search
这4个题都是针对旋转的排序数组.其中153.154是在旋转的排序数组中找最小值,33.81是在旋转的排序数组中找一个固定的值.且153和33都是没有重复数值的数组,154.81都是针对各自问题的版本1 ...
- 33. Search in Rotated Sorted Array & 81. Search in Rotated Sorted Array II
33. Search in Rotated Sorted Array Suppose an array sorted in ascending order is rotated at some piv ...
- 【LeetCode】81. Search in Rotated Sorted Array II (2 solutions)
Search in Rotated Sorted Array II Follow up for "Search in Rotated Sorted Array":What if d ...
- LeetCode 81. Search in Rotated Sorted Array II(在旋转有序序列中搜索之二)
Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would this ...
- [LeetCode] 81. Search in Rotated Sorted Array II 在旋转有序数组中搜索之二
Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. (i.e. ...
- [LeetCode] 81. Search in Rotated Sorted Array II 在旋转有序数组中搜索 II
Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would this ...
- 81 Search in Rotated Sorted Array II
Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would this ...
- 【一天一道LeetCode】#81. Search in Rotated Sorted Array II
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Follow ...
随机推荐
- C++入门经典-例3.17-使用while循环进行计算
1:代码如下: // 3.17.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include <iostream> usin ...
- ”锁“-LockSupport深入浅出
LockSupport是Java6引入的一个工具类,它简单灵活,应用广泛. 一.简单 俗话说,没有比较就没有伤害.这里咱们还是通过对比来介绍LockSupport的简单. 在没有LockSupport ...
- cucumber+selenium
工程结构 pom <?xml version="1.0" encoding="UTF-8"?> <project xmlns="ht ...
- golang gRPC初探
gRPC使用protocol buffers作为Interface Definition Language (IDL). gRPC的底层信息交互格式也使用的是protocol buffers. 默认情 ...
- 汽车Lin总线特点
串行通信:线间干扰小,节省线束,传输距离长 单线传输:增强的ISO 9141 (ISO 15765-1),总线电压基于VBAT 最高速率:20kbit/s 单主多从结构,无需仲裁:主节点同时包含主任务 ...
- [doker]ubuntu18安装doker
ubuntu安装doker很简单,分4个步骤: Step1:更新资源库并安装apt-transprot-https软件包. 在安装Docker前, 首拉取最新的软件资源库 wangju@wangju- ...
- java利用dom4j读取xml
java连接oracle数据库的时候, 需要从特定地方读取xml文件中的sql去get结果集, xml文件放在和java文件: SqlLoaderFromXML的目录下OracleSQL, xml文件 ...
- JS对象基础 闭包等
一.我们知道js的基本数据类型包括:number,boolen,string,null及undefined; 看下面的一段代码: var abcobject = { firstname:" ...
- Jmeter响应数据中文乱码|响应内容显示乱码
1.使用jmeter进行接口调用时出现返回数据乱码,如图示 原因是jmeter默认按照ISO-8859-1解析响应的数据. 2.所以需要修改bin目录下的jmeter.properties文件: 具体 ...
- C#学习笔记二 (资源托管,泛型,数组和元组,运算符和类型强制转换)
托管和非托管资源 1.托管资源是指GC管理的内存空间,非托管资源是指文件句柄,网络连接,数据库连接等. 2.方法中临时申请的变量,被存放在栈中.栈存储非对象成员的值数据.例如在方法中有B b=new ...