[LeetCode] Search in Rotated Sorted Array I (33) && II (81) 解题思路
33. 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
might become 4 5 6 7 0 1 2
).
You are given a target value to search. If found in the array return its index, otherwise return -1.
You may assume no duplicate exists in the array.
问题: 给定一个已排序的数组,该数组以某一个元素作为支点做了旋转,在改旋转后数组中搜索值。
已排序数组的搜索,自然会想到二分搜索。将旋转到后面的部分用负数表示下标,便可以正常使用二分搜索。
需要注意的是对比元素值 和 目标值时,记得将 下标转会正数 ( i + len ) % len 。
=================================
81. 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.
问题: 若 Search in Rotated Sorted Array 中的数组存在重复元素,如何解决原问题?
重复元素对于上面算法唯一一个影响,就是算法实现时候,判断是否有旋转需要更严谨一点。
第一题代码:
int search(vector<int>& nums, int target) { int len = (int)nums.size(); if (len == ){
return -;
} if ( len == ){
return (nums[] == target) ? : -;
} int realL;
int realR;
if (nums[] > nums[len-]) {
for ( int i = ; i < len ; i++){
if (nums[i] > nums[i+]){
realR = i;
break;
}
} realL = realR - len + ;
}else{
realL = ;
realR = len - ;
} while( realL < realR ){ if (realL + == realR){
int idxL = ( realL + len ) % len;
int idxR = ( realR + len ) % len; if (nums[idxL] == target){
return idxL;
} if (nums[idxR] == target){
return idxR;
} return -;
} int mid = ( realL + realR ) / ;
int idx = ( mid + len ) % len; if (nums[idx] == target){
return idx;
} if (nums[idx] < target){
realL = mid;
}else{
realR = mid;
}
} // Actually, program will never step to here. It will return value previously.
return -;
}
第二题代码:
bool search(vector<int>& nums, int target) { int len = (int)nums.size(); if (len == ){
return false;
} if ( len == ){
return (nums[] == target) ? : ;
} int realL;
int realR; int ii = ;
while (ii < len - ) {
if (nums[ii] <= nums[ii + ]) {
ii++;
continue;
}else{
break;
}
} if (ii == len - ) {
realL = ;
realR = len - ;
}else{
realR = ii;
realL = realR - len + ;
} while( realL < realR ){ if (realL + == realR){
int idxL = ( realL + len ) % len;
int idxR = ( realR + len ) % len; if (nums[idxL] == target){
return true;
} if (nums[idxR] == target){
return true;
}
return false;
} int mid = ( realL + realR ) / ;
int idx = ( mid + len ) % len; if (nums[idx] == target){
return true;
} if (nums[idx] < target){
realL = mid;
}else{
realR = mid;
}
} // Actually, program will never step to here. It will return value previously.
return -;
}
[LeetCode] Search in Rotated Sorted Array I (33) && II (81) 解题思路的更多相关文章
- 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: Search in Rotated Sorted Array II 解题报告
Search in Rotated Sorted Array II Follow up for "LeetCode: Search in Rotated Sorted Array 解题报告& ...
- 33.[LeetCode] Search in Rotated Sorted Array
Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. (i.e. ...
- [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 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 @ Python
原题地址:https://oj.leetcode.com/problems/search-in-rotated-sorted-array-ii/ 题意: Follow up for "Sea ...
- 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 Search in Rotated Sorted Array 在旋转了的数组中查找
Search in Rotated Sorted Array Suppose a sorted array is rotated at some pivot unknown to you before ...
随机推荐
- PHP5.4的变化关注---What has changed in PHP 5.4.x(转)
What has changed in PHP 5.4.x Most improvements in PHP 5.4.x have no impact on existing code. There ...
- 【前端JS】input textarea 默认文字,点击消失
如题.前端页面的 input textarea 有时候须要显示默认文字以提示用户,下面为实现代码,以 input 为例.textarea 能够直接搬用 HTML <input type=&quo ...
- i利用图片按钮 和 input type="image" 为背景提交表单
<img src="img/cancel.jpg" onclick="javascript:document.getElementByIdx_x('loginFor ...
- 帧动画 AnimationDrawable
Drawable Animation(Frame Animation):帧动画,就像GIF图片,通过一系列Drawable依次显示来模拟动画的效果. 首先,在res/drawable中定义动画 < ...
- exp导出出现:ORA-00904: "POLTYP": invalid identifier
相关文章: <exp导出出现:ORA-00904: : invalid identifier>:http://blog.itpub.net/23135684/viewspace-13 ...
- ngrok内网穿透(微信调试:只试用于微信测试账号)
一.简介 ngrok:https://ngrok.com 功能:就是把外网地址映射到本地的内网地址 缺点: 1.免费版生成的域名是随机的(由于我是用于调试,就没什么关系,如果是正式生产环境可能需要一个 ...
- ACM中常用的C/C++函数
只大概说明功能,具体用法请自行百度. C函数 memset:按字节填充地址空间 sscanf:从一个字符串中格式化读取变量 sprintf:将变量格式化写入字符串中 atoi:字符串转int atof ...
- android 手机信息获取
1. adb已安装 2. adb shell getprop 此时已列出所有相关信息
- 转 jQuery(图片、相册)插件代码实例
jQuery想必大部分前端er都知道甚至很熟悉了,网上有数以万计的优秀的jQuery插件以及教程,今天收集了一些关于图片.相册的jQuery插件代码,希望会对你有所帮助. 1. 3D Gallery ...
- 11061160_11061151_Pair Project: Elevator Scheduler软件工程结对编程作业总结
软件工程结对编程作业总结 11061160 顾泽鹏 11061151 庞梦劼 一.关于结对编程 这次的软工任务既不是单打独斗的个人任务,也不是集思广益的团队项目,而是人数为两人的结对编程.两个人合 ...