【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 ...
随机推荐
- winform 开发心得~
winform自适应不同分辨率 不同dpi 1.窗体AutoScaleMode属性 使用None 2.自定义控件 AutoScaleMode 使用Inherit 3.所有控件窗体字体使用px为单位
- HomeWork2
程序一: 1 public intfindLast(int[] x, inty) { 2 //Effects: If x==null throw NullPointerException 3 // e ...
- php 通过curl post发送json数据实例
例1 代码如下 复制代码 $data = array("name" => "Hagrid", "age" => "3 ...
- git ssh-add 报错 ssh-add Could not open a connection to your authentication agent
$ ssh-add ~/.ssh/id_rsa.pub Could not open a connection to your authentication agent. 启动ssh-agent服务 ...
- C#操作word模板插入文字、图片及表格详细步骤
c#操作word模板插入文字.图片及表格 1.建立word模板文件 person.dot用书签 标示相关字段的填充位置 2.建立web应用程序 加入Microsoft.Office.Interop.W ...
- _beginThreadex创建多线程解读【转】
_beginThreadex创建多线程解读 一.需要的头文件支持 #include <process.h> // for _beginthread() 需要的设置:Proj ...
- 架设WEBIM
使用openfire+jwchat构建. Openfire 采用Java开发,开源的实时协作(RTC)服务器基于XMPP(Jabber)协议.Openfire安装和使用都非常简单,并利用Web进行管理 ...
- Dp~Hrbust1426( 集训队的晚餐 )
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAxsAAAPRCAYAAACVrbUbAAAgAElEQVR4nOzdW5Bcx33n+X7aiH3b2J
- HDU1102--最小生成树
Constructing Roads Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- JQuery的Ajax跨域请求的解决方案
客户端调用代码示例: var myurl = "http://js.yingdoo.com/embed/CAPTCHA.ashx?m=" + phone_val + "& ...