081 Search in Rotated Sorted Array II 搜索旋转排序数组 ||
这是 “搜索旋转排序数组”问题的跟进:
如果数组元素允许重复,怎么办?
这会影响到程序的时间复杂度吗?会有怎样的影响,为什么?
假设按照升序排序的数组在预先未知的某个关键点上旋转。
(例如, 0 1 2 4 5 6 7 可能变成 4 5 6 7 0 1 2)。
编写一个函数来判断给定的目标是否在数组中。
该数组可能包含重复项。
详见:https://leetcode.com/problems/search-in-rotated-sorted-array-ii/description/
Java实现:
class Solution {
public boolean search(int[] nums, int target) {
int n=nums.length;
if(n==0||nums==null){
return false;
}
int left=0;
int right=n-1;
while(left<=right){
int mid=(left+right)>>1;
if(nums[mid]==target){
return true;
}else if(nums[left]==nums[mid]&&nums[mid]==nums[right]){
++left;
--right;
}else if(nums[left]<=nums[mid]){
if(nums[left]<=target&&target<nums[mid]){
right=mid-1;
}else{
left=mid+1;
}
}else{
if(target>nums[mid]&&target<=nums[right]){
left=mid+1;
}else{
right=mid-1;
}
}
}
return false;
}
}
081 Search in Rotated Sorted Array II 搜索旋转排序数组 ||的更多相关文章
- [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 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 OJ: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 在旋转有序数组中搜索 II
Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would this ...
- LeetCode 154. Find Minimum in Rotated Sorted Array II寻找旋转排序数组中的最小值 II (C++)
题目: Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. ( ...
- [LeetCode] 154. Find Minimum in Rotated Sorted Array II 寻找旋转有序数组的最小值 II
Follow up for "Find Minimum in Rotated Sorted Array":What if duplicates are allowed? Would ...
- leetCode 81.Search in Rotated Sorted Array II (旋转数组的搜索II) 解题思路和方法
Follow up for "Search in Rotated Sorted Array": What if duplicates are allowed? Would this ...
- [LeetCode]题解(python):081 - Search in Rotated Sorted Array II
题目来源 https://leetcode.com/problems/search-in-rotated-sorted-array-ii/ Follow up for "Search in ...
随机推荐
- iOS description
description:重写对象的这个方法,会在打印的时候显示出自定义的description中的内容debugDescription:方法是在开发者在调试器中以控制台命令打印对象时才调用的. 在NS ...
- Program received signal SIGSEGV, Segmentation fault.
GDB调试的时候出现了: Program received signal SIGSEGV, Segmentation fault.(程序收到信号SIGSEGV,分段故障) SIGSEGV:在POSIX ...
- IOS开发学习笔记(1)-----UILabel 详解
1. [代码][C/C++]代码 //创建uilabelUILabel *label1 = [[UILabel alloc] initWithFrame:CGRectMake(20, 40, ...
- codeforces A. Fox and Box Accumulation 解题报告
题目链接:http://codeforces.com/problemset/problem/388/A 题目意思:有 n 个 boxes,每个box 有相同的 size 和 weight,但是stre ...
- fiddler_test
fiddler学习第二天 啦啦啦 拉拉呀
- php排序方法之插入排序
//插入排序法 $arr = array(3,55,45,2,67,76,6.7,-65,85,4); function insertSort($arr){ for ( $i=0; $i<cou ...
- Linux命令学习笔记- vmstat命令实战详解
vmstat命令是最常见的Linux/Unix监控工具,可以展现给定时间间隔的服务器的状态值,包括服务器的CPU使用率,内存使用,虚拟内存交换情况,IO读写情况.这个命令是我查看Linux/Unix最 ...
- c和c++字符串分割
1.c++版本,第一个参数为待分割的字符串 , 第二个参数为分割字符串 std::vector<std::string> split(const std::string& s, c ...
- bzoj2117
动态电分治+二分 肯定要枚举所有点对,那么我们建出点分树降低树高,然后每个点存下点分树中所有子树到这个点的距离,然后二分+lower_bound就行了. #include<bits/stdc++ ...
- 【旧文章搬运】Win7 OBJECT_HEADER之TypeIndex解析
原文发表于百度空间,2010-08-09========================================================================== 在Wind ...