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. [CSS3] CSS Media Queries

    Using CSS media queries allows you to design responsive layout in your web apps and website. We will ...

  2. 通过分析 JDK 源代码研究 Hash 存储机制--转载

    通过 HashMap.HashSet 的源代码分析其 Hash 存储机制 集合和引用 就像引用类型的数组一样,当我们把 Java 对象放入数组之时,并不是真正的把 Java 对象放入数组中,只是把对象 ...

  3. yii图片上传

    http://wuhai.blog.51cto.com/2023916/953300 首先感谢这里的博主,提供了思路,不过在调用 $model->b_image->extensionNam ...

  4. poj 3349 (最小表示法)

    开始按hash做的 交上去就wa 但是和标称拍了半天也没有不一样的 可能是生成的数据太水了吧... #include<iostream> #include<cstdio> #i ...

  5. (转)C#读取文件路径

    //获取包含清单的已加载文件的路径或 UNC 位置. public static string sApplicationPath = Assembly.GetExecutingAssembly ( ) ...

  6. 腾讯云(centos7)上安装并配置PHP

    1.查看yum上的php $ yum list php Loaded plugins: fastestmirror, langpacks Loading mirror speeds from cach ...

  7. Try,Catch,Finally三块中如果有Return是怎么个运行顺序

    今天看一个Java SSH的面试题,题目大概意思是:try.catch中存在return语句,还会执行finally块吗?如果执行,是return先执行还是finally先执行?如果有多个return ...

  8. EF中使用Contains方法

    第一种情况 var db=new ECEntities(); var list=new []{"8","9"}; var result=from a in db ...

  9. ubuntu增加工作分区(workspace)命令

    dconf write  /org/compiz/profiles/unity/plugins/core/hsize 3 dconf write  /org/compiz/profiles/unity ...

  10. ADO.NET 新特性之SqlBulkCopy(批量插入大量数据)

    转自:http://blog.csdn.net/huaer1011/article/details/2312361 在.Net1.1中无论是对于批量插入整个DataTable中的所有数据到数据库中,还 ...