zoukankan      html  css  js  c++  java
  • [leetcode]Find Minimum in Rotated Sorted Array II

    二分,各种情况。

    class Solution {
    public:
        int findMin(vector<int> &num) {
            int size = num.size();
            int minVal = num[size-1];
            findMinRe(num, 0, size - 1, minVal);
            return minVal;
        }
        
        void findMinRe(vector<int> &num, int left, int right, int &minVal) {
            if (right < left) return;
            int mid = (left + right) / 2;
            if (num[left] < num[right]) {
                minVal = min(minVal, num[left]);
                return;
            }
            if (num[left] > num[right]) {
                minVal = min(minVal, num[right]);
                if (num[mid] <= num[right]) {
                    minVal = min(minVal, num[mid]);
                    findMinRe(num, left, mid - 1, minVal);
                } else {
                    findMinRe(num, mid + 1, right, minVal);
                }
            }
            if (num[left] == num[right]) {
                minVal = min(num[left], minVal);
                if (num[mid] < num[right]) {
                    minVal = min(minVal, num[mid]);
                    findMinRe(num, left, mid - 1, minVal);
                } else if (num[mid] > num[right]) {
                    findMinRe(num, mid + 1, right, minVal);
                } else { // ==
                    findMinRe(num, left, mid - 1, minVal);
                    findMinRe(num, mid + 1, right, minVal);
                }
            }
        }
    };
    

      

  • 相关阅读:
    python 基础第二篇
    python 基础第五篇
    python 基础第四篇
    购物小编程(完整编码)
    计算机 python概论
    str 相关操作
    python 基础第三篇
    Nginx 配置多站点vhost
    h5页面宽度设置7.5rem
    js 倒计时,转义
  • 原文地址:https://www.cnblogs.com/lautsie/p/4196789.html
Copyright © 2011-2022 走看看