zoukankan      html  css  js  c++  java
  • 面试题8:旋转数组中的最小数字

    面试题8:旋转数组中的最小数字

    剑指Offer中有这道题目的分析。这是一道二分查找的变形的题目。

    旋转之后的数组实际上可以划分成两个有序的子数组:前面子数组的大小都大于后面子数组中的元素
    注意到实际上最小的元素就是两个子数组的分界线。本题目给出的数组一定程度上是排序的,因此我们试着用二分查找法寻找这个最小的元素。
    思路:
    (1)我们用两个指针left,right分别指向数组的第一个元素和最后一个元素。按照题目的旋转的规则,第一个元素应该是大于最后一个元素的(没有重复的元素)。
    但是如果不是旋转,第一个元素肯定小于最后一个元素。
    (2)找到数组的中间元素。
    中间元素大于第一个元素,则中间元素位于前面的递增子数组,此时最小元素位于中间元素的后面。我们可以让第一个指针left指向中间元素。
    移动之后,第一个指针仍然位于前面的递增数组中。
    中间元素小于第一个元素,则中间元素位于后面的递增子数组,此时最小元素位于中间元素的前面。我们可以让第二个指针right指向中间元素。
    移动之后,第二个指针仍然位于后面的递增数组中。
    这样可以缩小寻找的范围。
    (3)按照以上思路,第一个指针left总是指向前面递增数组的元素,第二个指针right总是指向后面递增的数组元素。
    最终第一个指针将指向前面数组的最后一个元素,第二个指针指向后面数组中的第一个元素。
    也就是说他们将指向两个相邻的元素,而第二个指针指向的刚好是最小的元素,这就是循环的结束条件。
    到目前为止以上思路很耗的解决了没有重复数字的情况,这一道题目添加上了这一要求,有了重复数字。
    因此这一道题目比上一道题目多了些特殊情况:
    我们看一组例子:{1,0,1,1,1} 和 {1,1, 1,0,1} 都可以看成是递增排序数组{0,1,1,1,1}的旋转。
    这种情况下我们无法继续用上一道题目的解法,去解决这道题目。因为在这两个数组中,第一个数字,最后一个数字,中间数字都是1。
    第一种情况下,中间数字位于后面的子数组,第二种情况,中间数字位于前面的子数组。
    因此当两个指针指向的数字和中间数字相同的时候,我们无法确定中间数字1是属于前面的子数组(绿色表示)还是属于后面的子数组(紫色表示)。
    也就无法移动指针来缩小查找的范围。

    代码:

     1 #include <iostream>
     2 #include <vector>
     3 #include <string>
     4 #include <stack>
     5 #include <algorithm>
     6 using namespace std;
     7  
     8 class Solution {
     9 public:
    10     int minNumberInRotateArray(vector<int> rotateArray) {
    11         int size = rotateArray.size();
    12         if(size == 0){
    13             return 0;
    14         }//if
    15         int left = 0,right = size - 1;
    16         int mid = 0;
    17         // rotateArray[left] >= rotateArray[right] 确保旋转
    18         while(rotateArray[left] >= rotateArray[right]){
    19             // 分界点
    20             if(right - left == 1){
    21                 mid = right;
    22                 break;
    23             }//if
    24             mid = left + (right - left) / 2;
    25             // rotateArray[left] rotateArray[right] rotateArray[mid]三者相等
    26             // 无法确定中间元素是属于前面还是后面的递增子数组
    27             // 只能顺序查找
    28             if(rotateArray[left] == rotateArray[right] && rotateArray[left] == rotateArray[mid]){
    29                 return MinOrder(rotateArray,left,right);
    30             }//if
    31             // 中间元素位于前面的递增子数组
    32             // 此时最小元素位于中间元素的后面
    33             if(rotateArray[mid] >= rotateArray[left]){
    34                 left = mid;
    35             }//if
    36             // 中间元素位于后面的递增子数组
    37             // 此时最小元素位于中间元素的前面
    38             else{
    39                 right = mid;
    40             }//else
    41         }//while
    42         return rotateArray[mid];
    43     }
    44 private:
    45     // 顺序寻找最小值
    46     int MinOrder(vector<int> &num,int left,int right){
    47         int result = num[left];
    48         for(int i = left + 1;i < right;++i){
    49             if(num[i] < result){
    50                 result = num[i];
    51             }//if
    52         }//for
    53         return result;
    54     }
    55 };
    56  
    57 int main(){
    58     Solution s;
    59     //vector<int> num = {0,1,2,3,4,5};
    60     //vector<int> num = {4,5,6,7,1,2,3};
    61     vector<int> num = {2,2,2,2,1,2};
    62     int result = s.minNumberInRotateArray(num);
    63     // 输出
    64     cout<<result<<endl;
    65     return 0;
    66 }
  • 相关阅读:
    httpcontext in asp.net unit test
    initialize or clean up your unittest within .net unit test
    Load a script file in sencha, supports both asynchronous and synchronous approaches
    classes system in sencha touch
    ASP.NET MVC got 405 error on HTTP DELETE request
    how to run demo city bars using sencha architect
    sencha touch mvc
    sencha touch json store
    sencha touch jsonp
    51Nod 1344:走格子(贪心)
  • 原文地址:https://www.cnblogs.com/raichen/p/5639412.html
Copyright © 2011-2022 走看看