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 ...
随机推荐
- 人生苦短之Python类的一二三
在Python中,类也是以class开头定义的.我们定义一个动物类,它有名字和年龄,在java变量有实例变量和局部变量,方法内的变量是局部变量,类里面的变量是实例变量.那么在Python中的类及其属性 ...
- 牛逼的This使用
今天看到一个很不错的this使用demo: package com.toov5.Reordering; class Message1{ private Channel channel; private ...
- mac安装python3
http://www.jianshu.com/p/51811fa24752 brew install python3 安装路径:/usr/local/Cellar 使用: 执行python3即可 配置 ...
- 利用javascript动态创建表格
//说明:实现功能.原理上文相同.不过这次是利用已有的简单的方法创建行和列,并实现内容行鼠标移入变色功能! 效果图: /*两个方法 1. trNode table.insertRow(-1) ...
- 用mingw-w64 编译 x64 位的ffmpeg
http://blog.sina.com.cn/s/blog_6125d067010168dt.html 工作中用到了ffmpeg x64. 发现编译出来x64的ffmpeg,很不容易.特记录下来.原 ...
- BZOJ 1633 [Usaco2007 Feb]The Cow Lexicon 牛的词典:dp【删字符最少】
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1633 题意: 给你一个长度为n的主串a,和一个有m个字符串s[i]的单词书(s[i].si ...
- smarty模板引擎基础(二)
smarty模板引擎所需文件夹共可分为存放页面缓存的(cache).存放配置文件的(configs).存放模板扩充插件的(plugins).存放模板文件的(templates).存放编译后的模板文件的 ...
- 页面渲染——页面合成(composition)的优化
合成(composition)意味着将网页中已经绘画好的部分结合在一起,且展示在屏幕上. 坚持使用transform和opacity属性来操作你的动画animation 在有动画的元素上使用 will ...
- Bone Collector(复习01背包)
传送门 题目大意:01背包裸题. 复习01背包: 题目 有N件物品和一个容量为V的背包.第i件物品的费用是c[i],价值是w[i].求解将哪些物品装入背包可使这些物品的费用总和不超过背包容量,且价值总 ...
- zynq交叉编译环境设置
环境准备 Vmware 虚拟机:ubuntu14.04 下载安装文件 环境设置 按照上面地址下载 xilinx-2011.09-50-arm-xilinx-linux-gnueabi.bin 将das ...