leetcode 二分查找 Search in Rotated Sorted ArrayII
Search in Rotated Sorted Array II
Total Accepted: 18500 Total
Submissions: 59945My Submissions
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.
题意:一个已经排序好的数组。被按某个位置旋转了一次,给定一个值target,在该旋转后的数组里查找该值。(数组中的元素可能反复)
思路:二分查找
难点在于确定往数组的哪一半段继续二分查找
设起点、中间点、终点分别为 start、middle、end (採用前闭后开的区间表示方法
假设target = A[middle] return middle
假设A[middle] > A[start],则[start,middle)单调递增
1.假设target < A[middle] && target >= A[start],则 end = middle
2.start = middle + 1, otherwise
假设A[middle] < A[start],则[middle,end)单调递增
1.假设target > A[middle] && target <= A[end - 1],则 start = middle + 1
2.end = middle, otherwise
假设A[middle] == A[start]。那A[start]也不会是target,能够通过start++; 去掉A[start]
复杂度:时间O(n),空间O(1)
int search(int A[], int n, int target){
int start = 0, end = n, middle ;
while(start < end){
middle = (start + end) / 2;
if(A[middle] == target) return middle;
if(A[middle] > A[start]){
if(target >= A[start] && target < A[middle]){
end = middle;
}else{
start = middle + 1;
}
}else if(A[middle] < A[start]){
if(target > A[middle] && target <= A[end - 1]){
start = middle + 1;
}else{
end = middle;
}
}
else{
++start;
}
}
return -1;
}
leetcode 二分查找 Search in Rotated Sorted ArrayII的更多相关文章
- leetcode 二分查找 Search in Rotated Sorted Array
Search in Rotated Sorted Array Total Accepted: 28132 Total Submissions: 98526My Submissions Suppose ...
- Search in Sorted Array,Search in Rotated Sorted Array,Search in Rotated Sorted ArrayII
一:Search in Sorted Array 二分查找,可有重复元素,返回target所在的位置,只需返回其中一个位置,代码中的查找范围为[low,high),左闭右开,否则容易照成死循环. 代码 ...
- 【一天一道LeetCode】#81. Search in Rotated Sorted Array II
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Follow ...
- leetcode 题解:Search in Rotated Sorted Array II (旋转已排序数组查找2)
题目: Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would t ...
- LeetCode OJ:Search in Rotated Sorted Array II(翻转排序数组的查找)
Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would this ...
- leetcode解题报告(4):Search in Rotated Sorted ArrayII
描述 Follow up for "Search in Rotated Sorted Array": What if duplicates are allowed? Would t ...
- [Leetcode][Python]33: Search in Rotated Sorted Array
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 33: Search in Rotated Sorted Arrayhttps ...
- LeetCode题解33.Search in Rotated Sorted Array
33. Search in Rotated Sorted Array Suppose an array sorted in ascending order is rotated at some piv ...
- LeetCode解题报告—— Search in Rotated Sorted Array & Search for a Range & Valid Sudoku
1. Search in Rotated Sorted Array Suppose an array sorted in ascending order is rotated(轮流,循环) at so ...
随机推荐
- Python监控目录和文件变化
一.os.listdir import os, time path_to_watch = "." before = dict ([(f, None) for f in os.lis ...
- 【CF398B】B. Painting The Wall(期望)
B. Painting The Wall time limit per test 1 second memory limit per test 256 megabytes input standard ...
- android activity 窗口 样式
韩梦飞沙 韩亚飞 313134555@qq.com yue31313 han_meng_fei_sha 将 活动 设置成 窗口样式, 可以设置 主题 为 对话框, 或者 半透明. 安卓:主题= ...
- BZOJ 2118 墨墨的等式(最短路)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=2118 [题目大意] 求a1x1+a2y2+…+anxn=B在B的取值范围,有多少B可以 ...
- loj6089 小 Y 的背包计数问题
link 吐槽: 好吧开学了果然忙得要死……不过为了证明我的blog还没有凉,还是跑来更一波水题 题意: 有n种物品,第i种体积为i,问装满一个大小为n的背包有多少种方案? $n\leq 10^5.$ ...
- bzoj 1875: [SDOI2009]HH去散步 -- 矩阵乘法
1875: [SDOI2009]HH去散步 Time Limit: 20 Sec Memory Limit: 64 MB Description HH有个一成不变的习惯,喜欢饭后百步走.所谓百步走, ...
- python开发_thread_布朗运动
这篇blog是非常有趣的,是的,他非常有趣 下面我将给大家介绍有关python中thread来实现布朗运动的一个例子 下面是运行效果: ================================ ...
- Codeforces Round #354 (Div. 2) E. The Last Fight Between Human and AI 数学
E. The Last Fight Between Human and AI 题目连接: http://codeforces.com/contest/676/problem/E Description ...
- FolderSync Instant sync 即时同步
Folderpairs - Edit folderpair - Sync options - Instant sync Select this for instant sync on change. ...
- VHDL语言实现的任意整数分频器
fpga中,一般外接的晶振是50Mhz,如果电路中一个模块需要25mhz时钟,那么进行一个2分频,这个是相当容易的,下面是一种方法,还有可以用一个二进制计数器实现.这里就不写代码了.easy.同样的原 ...