LeetCode-Search in Rotated Sorted Array II
Follow up for "Search in Rotated Sorted Array":
What if duplicates are allowed? Would this affect the run-time complexity? How and why? Write a function to determine if a given target is in the array.
解决方法就是对于A[mid]==A[left]和A[mid]==A[right]单独处理。
public class Solution {
public boolean search(int[] nums, int target) {
if(nums==null || nums.length==0){
return false;
}
int left=0;
int right=nums.length-1;
while(left<=right){
int mid=left+(right-left)/2;
if(target<nums[mid]){
if(nums[mid]< nums[right]){// right side is sorted
right=mid-1;
}
else if(nums[mid] == nums[right]){//can't tell right is sorted or not, move pointer
right--;
}
else {//left side is sorted
if(target<nums[left]){
left=mid+1;
}
else{
right=mid-1;
}
}
}
else if(target>nums[mid]){
if(nums[mid] > nums[left]){//left is sorted
left=mid+1;
}
else if(nums[mid]== nums[left]){// cann't tell left side is sorted or not, move pointer
left++;
}
else{//right is sorted
if(target>nums[right]){
right=mid-1;
}
else{
left=mid+1;
}
}
}
else{
return true;
}
}
return false;
}
}
LeetCode-Search in Rotated Sorted Array II的更多相关文章
- LeetCode: Search in Rotated Sorted Array II 解题报告
Search in Rotated Sorted Array II Follow up for "LeetCode: Search in Rotated Sorted Array 解题报告& ...
- [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]Search in Rotated Sorted Array II @ Python
原题地址:https://oj.leetcode.com/problems/search-in-rotated-sorted-array-ii/ 题意: Follow up for "Sea ...
- [LeetCode] Search in Rotated Sorted Array II [36]
称号 Follow up for "Search in Rotated Sorted Array": What if duplicates are allowed? Would t ...
- [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 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 I II
LeetCode:Search in Rotated Sorted Array Suppose a sorted array is rotated at some pivot unknown to y ...
- LeetCode OJ:Search in Rotated Sorted Array II(翻转排序数组的查找)
Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would this ...
随机推荐
- Sql Server 查看表修改记录
可以尝试如下建议:1.可以使用默认的Log工具或者第三方的(比如:LiteSpeed)的工具.2.做Trace机制,下次出现问题可以溯源.3.一个简单的办法: --Step #1: USE DBNam ...
- Command Pattern
当(客户)对象访问(服务)请求服务时,最直接的方法就是方法调用.
- [转载]va_start和va_end使用详解
va_start和va_end使用详解 原文地址:http://www.cnblogs.com/hanyonglu/archive/2011/05/07/2039916.html 本文主要介绍va_s ...
- MVC WebAPI中响应客户端请求返回图片
// GET api/values public HttpResponseMessage Get() { Image img = GetImage(); MemoryStream ms ...
- Python开发入门与实战13-基于模板的界面
13. 基于模板的界面 本章我们将继续基于库存的简单例子来阐述如何在python django中体现MVC的架构,根据djangobook说明: M:数据存取部分,由django数据库层处理,本章要讲 ...
- unslider.js 实现移动web轮播
unslider.js可以实现轮播,但是在移动端还需要另两个插件. jquery.event.move和jQuery.event.swipe : 下面就是简单的实例: <!doctype htm ...
- Schedule 学习
现在做的项目都有用到Schedule,现在用一点时间来总结. 一.首先要到Nuget中下载Quartz.net. 二.下载下来了,你需要对它进行配置,使它能给你正常的使用. 三.在Global.asa ...
- ObjC宏定义小细节
Macros A definition that takes arguments, particularly more than one, is often known as a macro: #de ...
- 程序测试用的IE浏览器第二次无法加载入口程序的问题及其解决方法
注:针对的是C#程序(Silverlight) 第一步.找到入口程序所在的路径,以记事本形式打开<入口程序.csproj>,由于之前配置入口程序时,设置了“Use Local IIS We ...
- mantis邮箱配置
1.修改/var/www/html/mantisbt-1.3.3/config下config_inc.php配置文件 以163邮箱为例 # --- Email Configuration --- $g ...