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 ...
随机推荐
- Scala编程--函数式对象
本章的重点在于定义函数式对象,也就是说,没有任何可变状态的对象的类.作为运行的例子,我们将创造若干把分数作为不可变对象建模的类的变体.在这过程中,我们会展示给你Scala面向对象编程的更多方面:类参数 ...
- nginx url重定向
nginx内部支持url rewrite,内部编译进去了rewrite模块,nginx的rewrite模块类似于apache的rewriterule功能:支持多种规则和正则表达式: 详细介绍如下: N ...
- [转载] javascript实现深度克隆
js一般有两种不同数据类型的值: 基本类型(包括undefined,Null,boolean,String,Number),按值传递: 引用类型(包括数组,对象),按址传递,引用类型在值传递的时候是内 ...
- Activity中获取当前Fragment 中的子控件
XXXAdapter中 增加 public Fragment currentFragment; @Override public void setPrimaryItem(ViewGroup conta ...
- 0x7c95caa2指令引用的0x00000000内存 该内存不能read
出现这样的错误,往往和动态库有关系! 解决方法:
- php 文件操作
$fn="e:\debug.txt"; if(is_writable($fn)==false){ die("不能写入"); } file_put_content ...
- linux rlwrap
无意中发现了rlwrap,终于可以在linux下使用方向键上下翻页输入过的语句了. 比如sqlplus or ggsci中使用. 如果是ubuntu,则在software center中可以直接安装r ...
- mvc 传递匿名对象
Controller代码: public ActionResult TupleTest() { LinqDBEntities db = new LinqDBEntities(); dynamic da ...
- Python学习路程day20
本节内容: 项目:开发一个简单的BBS论坛 需求: 整体参考“抽屉新热榜” + “虎嗅网” 实现不同论坛版块 帖子列表展示 帖子评论数.点赞数展示 在线用户展示 允许登录用户发贴.评论.点赞 允许上传 ...
- Xcode 7.0升级后的bitcode
iOS 9.0中加入了一个新的功能,bitcode, 使用bitcode优化的app,体积可以变得更小. Apple可以在提交app后,向9.0及以上版本用户提供优化的小体积版本,向其他用户提供常规版 ...