leetcode 之Search in Rotated Sorted Array(四)
描述
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[m]>=A[l]是无法判断出(m,l)之间是升序的。因此,需将大于和等于分开来考虑。
只需在等于时,将重复元素跳过再比较即可。
int searchRotateSA(int A[], int n,int target)
{
int first = , last = n;
while (first!=last)
{
int mid = first + (last - first) / ;
if (A[mid] = target)
{
return mid;
}
else if (A[first]<A[mid])//判断大小顺序
{
if (A[first] <= target&&target <= A[mid])
last = mid;
else
first = mid + ;
}
else if (A[first]>A[mid])
{
if (A[first] >= target&& A[mid] <= target)
last = mid;
else
first = mid + ; }
else first++;
} return -;
}
leetcode 之Search in Rotated Sorted Array(四)的更多相关文章
- [LeetCode] 033. Search in Rotated Sorted Array (Hard) (C++)
指数:[LeetCode] Leetcode 解决问题的指数 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 033. ...
- [array] leetcode - 33. Search in Rotated Sorted Array - Medium
leetcode - 33. Search in Rotated Sorted Array - Medium descrition Suppose an array sorted in ascendi ...
- LeetCode 81 Search in Rotated Sorted Array II [binary search] <c++>
LeetCode 81 Search in Rotated Sorted Array II [binary search] <c++> 给出排序好的一维有重复元素的数组,随机取一个位置断开 ...
- LeetCode 33 Search in Rotated Sorted Array [binary search] <c++>
LeetCode 33 Search in Rotated Sorted Array [binary search] <c++> 给出排序好的一维无重复元素的数组,随机取一个位置断开,把前 ...
- [leetcode]81. Search in Rotated Sorted Array II旋转过有序数组里找目标值II(有重)
This is a follow up problem to Search in Rotated Sorted Array, where nums may contain duplicates. 思路 ...
- Java for LeetCode 081 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
做Leetcode题有一段时间了,但都是断断续续的,到现在才做了30题左右,感觉对自己来说还是有点难度的.希望自己能继续坚持下去,在校招前能解决超过一百题吧. 其实这些题就是用来训练你的解题思路的,做 ...
- LeetCode 81. Search in Rotated Sorted Array II(在旋转有序序列中搜索之二)
Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would this ...
随机推荐
- ZOJ3899 State Reversing 【线段树 + NTT】
题目链接 ZOJ3899 题解 比较累,做一道水题 还被卡常= = 我在\(ZOJ\)交过的两道\(NTT\)都被卡常了.. 哦,题意就是求第二类斯特林数,然后线段树维护一下集合数量就可以了 #inc ...
- [ZJOI2007]棋盘制作 【最大同色矩形】
题目描述 国际象棋是世界上最古老的博弈游戏之一,和中国的围棋.象棋以及日本的将棋同享盛名.据说国际象棋起源于易经的思想,棋盘是一个8*8大小的黑白相间的方阵,对应八八六十四卦,黑白对应阴阳. 而我们的 ...
- python 分享文件
http://note.youdao.com/noteshare?id=1787e8bf3a71fca16005ece3e7fffb6c
- STL源码分析-内存分配器
http://note.youdao.com/noteshare?id=744696e5f6daf0f2f03f10e381485e67
- ASP.NET Core的身份认证框架IdentityServer4--入门【转】
原文地址 Identity Server 4是IdentityServer的最新版本,它是流行的OpenID Connect和OAuth Framework for .NET,为ASP.NET Cor ...
- 解决VS Code编译调试中文输出乱码
最近尝试用VS Code配置了C和C++的编译调试环境,结果遇到了中文输出乱码问题,查阅网上竟然还没有相关问题,有怀疑是mingw中文支持问题,但最后证明是VS Code编码问题. 解决方案: 文件- ...
- [USACO14JAN]Recording the Moolympics
题目描述 Being a fan of all cold-weather sports (especially those involving cows), Farmer John wants to ...
- 详解ASP.NET4 GridView的四种排序样式
与ASP.NET 的其他Web控件一能够,Gridview控件拥有很多不同的CSS样式属性设置,包括象CssClass,Font字体,ForeColor,BackColor,BackColor, Wi ...
- 使用 XSLT 作为 HTML 的样式表
简介 当听到样式表这个词时,您可能会想到 CSS 样式表.XSLT 样式表通常用于 XML 转换,比如在 Web 服务之间映射数据.因为 XSLT 非常适合此用途,所以创建了顶层元素 <styl ...
- iframe子夜页面调父页面的方法 取父页面的值
1.调父页面的方法的写法 window.parent.yincang();//yincang()是父页面的一个方法 2.取父页面的值的写法 window.parent.document.gettEle ...