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.
原题链接:https://oj.leetcode.com/problems/search-in-rotated-sorted-array-ii/
题目:继续之前的题,在旋转过的有序数组中找目标值。若同意有反复的值呢?
public boolean search(int[] A, int target) {
int low = 0, high = A.length - 1;
while (low <= high) {
int mid = (low + high) / 2;
if (target == A[mid])
return true;
if (A[mid] > A[low]) {
if (target >= A[low] && target <= A[mid])
high = mid - 1;
else
low = mid + 1;
} else if (A[mid] < A[low]) {
if (target >= A[mid] && target <= A[high])
low = mid + 1;
else
high = mid - 1;
} else
low += 1;
}
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 @ 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 ...
随机推荐
- PS大头照的背景
给同事ps一个大头照,只是修改一下背景颜色,以前没有做过,这次算是小练习了一把,与大家分享一下.修改大头照背景重要的是如何选中背景的区域,如果用魔棒选择的话,头与背景的边处理的不好,说说我的处理方法吧 ...
- [置顶] Windows Phone后台音乐详解一
应用于: Windows Phone 8 | Windows PhoneOS 7.1 你可以为winphone编写在后台播放音乐的app.这表示即使当用户点击返回或开始按钮离开你的应用界面时,你的应用 ...
- xcode-build/version-bump
# xcode-build-bump.sh # @desc Auto-increment the build number every time the project is run. # @usag ...
- Qt图片显示效率的比较 转
转http://blog.sina.com.cn/s/blog_5c70dfc80100r257.html 在Qt中处理图片一般都要用到QImage类,但是QImage的对象不能够直接显示出来,要想能 ...
- post 请求参数
perl代码: my $login_url='http://192.168.1.1/getpage.gch?pid=1001&logout=1'; my $res = $ua->post ...
- 前端javascript框架之AngularJS学习笔记
<!doctype html><html lang="en" ng-app><head><meta charset="utf-8 ...
- grails一对一关联关系
一对一关联关系开发中用的没有一对多那么广泛.可是我认为掌握以下还是有必要的.一对一关联关系有一张表存在外键,引用的通常是主表的主键.grails也对一对一关联关系提供了非常好的支持.配置也是简单的不得 ...
- Get Cordova Ready for Grunt and CoffeeScript
Cordova, Grunt and Coffee You may reference to below if you deside to work with coffee instead of Ja ...
- Swift教程之typealias代替OC的typedef
//MARK:-------swift中的typedef-------------- //使用 keyword定义类型别名,相似typedef typealias NSInteger = Int va ...
- AJAX - 类型“System.Web.UI.UpdatePanel”不具有名为“FileUpload”的公共属性。
类型“system.web.ui.updatepanel” 不具有名为“***”的公共属性,其实原因很简单.就是少了一个<ContentTemplate></ContentTempl ...