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) 解题思路的更多相关文章

  1. 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 ...

  2. LeetCode: Search in Rotated Sorted Array II 解题报告

    Search in Rotated Sorted Array II Follow up for "LeetCode: Search in Rotated Sorted Array 解题报告& ...

  3. 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. ...

  4. [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 ...

  5. [LeetCode] Search in Rotated Sorted Array II 在旋转有序数组中搜索之二

    Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would this ...

  6. LeetCode——Search in Rotated Sorted Array II

    Follow up for "Search in Rotated Sorted Array": What if duplicates are allowed? Would this ...

  7. [leetcode]Search in Rotated Sorted Array II @ Python

    原题地址:https://oj.leetcode.com/problems/search-in-rotated-sorted-array-ii/ 题意: Follow up for "Sea ...

  8. LeetCode: Search in Rotated Sorted Array 解题报告

    Search in Rotated Sorted Array Suppose a sorted array is rotated at some pivot unknown to you before ...

  9. LeetCode Search in Rotated Sorted Array 在旋转了的数组中查找

    Search in Rotated Sorted Array Suppose a sorted array is rotated at some pivot unknown to you before ...

随机推荐

  1. TN2151:崩溃报告

    understanding and analyzing ios application crashreports 这个TN涉及了与崩溃相关的 内存耗尽信息,堆栈信息 以及 异常编号 等信息 内存耗尽 ...

  2. 【C++深入探索】Copy-and-swap idiom详解和实现安全自我赋值

    分类: C/C++2012-08-30 21:40 2017人阅读 评论(2) 收藏 举报 任何管理某资源的类比如智能指针需要遵循一个规则(The Rule of Three): 如果你需要显式地声明 ...

  3. Linux查看硬件信息以及驱动设备的命令

    用硬件检测程序kuduz探测新硬件:service kudzu start ( or restart) 查看CPU信息:cat /proc/cpuinfo 查看板卡信息:cat /proc/pci 查 ...

  4. shell编程备忘

    1.脚本存放目录 workspace="$(cd "$(dirname "$0")"; pwd)" 2.输出  其中 command 代表指 ...

  5. c# 预处理命令

    在编译之前进行的处理. 预处理命令以符号“#”开头. #define 只能 定义符号 不能定义宏(#define PI 3.14 这是错的,在c#中没宏) #region #endregion #if ...

  6. 马士兵SVN.

    下载 服务端:VisualSVN Server 和客户端:TortoiseSVN cmd,并cd 到 VisualSVN Server安装目录下的bin目录. 新建库: svnadmin create ...

  7. javaScript 自定义事件、发布订阅设计模式

    现在很多应用都允许用户根据自己的喜好订阅一些自己较为关注的信息,当应用更新了这些信息后将针对不同的订阅类型推送此类信息.例如xx招聘网,当你订阅了互联网IT技术相关分类的招聘信息推送后,当企业在该网站 ...

  8. django安装

    见 http://jingyan.baidu.com/article/466506580e7d29f549e5f8b6.html 下载安装python下载解压django cmd进入django目录, ...

  9. 启动mySQL安装出现1067错误

    可能几种的办法: 删除data目录下的ib_logfile0和ib_logfile1 查看my.ini文件中的dir设置 查看err文件,如果是temp出现错误文件,则添加temp文件的路径

  10. HashTable 及应用

    HashTable-散列表/哈希表,是根据关键字(key)而直接访问在内存存储位置的数据结构. 它通过一个关键值的函数将所需的数据映射到表中的位置来访问数据,这个映射函数叫做散列函数,存放记录的数组叫 ...