【leetcode】Search in Rotated Sorted Array II(middle)☆
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.
我的思路:
太混乱了 不提了。注意关键区分依据 排好序的一定是从小到大的
看大神的吧:
bool search(int A[], int n, int key) {
int l = , r = n - ;
while (l <= r) {
int m = l + (r - l)/;
if (A[m] == key) return true; //return m in Search in Rotated Array I
if (A[l] < A[m]) { //left half is sorted 排好序的部分一定是从小到大的
if (A[l] <= key && key < A[m]) //在排好序的这部分
r = m - ;
else
l = m + ;
} else if (A[l] > A[m]) { //right half is sorted
if (A[m] < key && key <= A[r])
l = m + ;
else
r = m - ;
} else l++; //A[l] == A[m] 把l增大1个再循环
}
return false;
}
我的代码,把三个数字都相等的情况单独处理,其他就用无重复处理。 其实我的代码看起来长一些,但是在处理1111111111115这种情况时我的方法优势还是有的
bool search(int A[], int n, int target) {
int l = , r = n - ;
while(l <= r)
{
int m = (l + r) / ;
if(target == A[m])
return true;
else if(A[l] == A[m] && A[m] == A[r]) //三个值相等
{
bool isequalleft = true;
for(int i = l; i < m; i++)
{
if(A[i] != A[i + ])
{
isequalleft = false;
break;
}
}
if(isequalleft) //去掉重复的那一半
l = m + ;
else
r = m - ;
}
else //与不重复的方法相同
{
if (A[l] <= A[m]) {
if (target >= A[l] && target < A[m]) {
r = m - ;
} else {
l = m + ;
}
} else {
if (target > A[m] && target <= A[r]) {
l = m + ;
} else {
r = m - ;
}
}
}
} return false;
}
【leetcode】Search in Rotated Sorted Array II(middle)☆的更多相关文章
- 【LeetCode】Search in Rotated Sorted Array II(转)
原文链接 http://oj.leetcode.com/problems/search-in-rotated-sorted-array-ii/ http://blog.csdn.net/linhuan ...
- 【leetcode】Search in Rotated Sorted Array II
Search in Rotated Sorted Array II Follow up for "Search in Rotated Sorted Array":What if d ...
- 【leetcode】Remove Duplicates from Sorted List II (middle)
Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numb ...
- 【leetcode】Search in Rotated Sorted Array
Search in Rotated Sorted Array Suppose a sorted array is rotated at some pivot unknown to you before ...
- LeetCode 81. 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】Search in Rotated Sorted Array
Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 migh ...
- 【LeetCode】Search in Rotated Sorted Array——旋转有序数列找目标值
[题目] Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 ...
- 【leetcode】Search in Rotated Sorted Array (hard)
Suppose a sorted array is rotated at some pivot unknown to you beforehand. (i.e., 0 1 2 4 5 6 7 migh ...
随机推荐
- spring缓存Ehcache(入门2)
使用Ehcache缓存工具类. 一.由于使用了maven,所以需要引入依赖包: <dependency> <groupId>net.sf.ehcache</groupId ...
- Max Degree of Parallelism最大并行度配置
由于公司的业务在急速增长中,发现数据库服务器已经基本撑不住这么多并发.一方面,要求开发人员调整并发架构,利用缓存减少查询.一方面从数据库方面改善并发.数据库的并行度可设置如下: 1)cost thre ...
- 不挣扎了,开始学习LINQ TO XML,进而来解析网页。
找到了别人遇到和我一样的问题:http://ylad.codeplex.com/discussions/430095(英文) 一位叫做Mister Goodcat的提供了信息: Short answe ...
- cf306 C. Divisibility by Eight(数学推导)
C. Divisibility by Eight time limit per test 2 seconds memory limit per test 256 megabytes input sta ...
- Mac Pro 入门、遇到的问题、个性化设置 汇总
入门资料 入门一:Mac 基本用法 入门二:Mac 使用VMware Fusion虚拟机 入门三:Mac 使用brew安装软件 问题汇总 [问题1]如何复制文本? 一只手指头按下,另外一只手指头滑动选 ...
- linux 脚本命令匹配并获取下一行数据
三种方式: 匹配“Title”并打印出匹配行的下一行 grep -A 1 'Title' urfile awk '/Title/{getline a;print $0"\n"a ...
- php的Excel相关操作
1.需求 把数据库的数据输出excel格式 2.解决方案 利用phpexcel中的examples的01和07,对excel文件的读写 3.操作流程 a.https://github.com/PHPO ...
- JavaScript深入浅出4-对象
慕课网教程视频地址:Javascript深入浅出 对象的结构:包含一系列无序的属性,每个属性都有字符串key和对应的值 创建对象:对象字面量.new/原型链.Object.create 对象的属性操作 ...
- Android 中的消息传递,详解广播机制
--------------------------------------广播机制简介--------------------------------------------- Android中的广 ...
- Android之Toast通知的几种自定义用法
Toast.makeText(context, str, Toast.LENGTH_SHORT).show(); } ...