• [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);
                }
            }
        }
    };
    

      

  • 相关阅读:
    【转】CNN卷积神经网络_ GoogLeNet 之 Inception(V1-V4)
    【转】Caffe的solver文件配置
    python 从filelist.txt中拷贝文件到另一文件夹中
    【转】fscanf 跳过空格,读取一行
    caffe配置NCCL
    caffe实现多任务学习
    mysql 2
    mysql 1
    mysql
    jQuery
  • 原文地址:https://www.cnblogs.com/lautsie/p/4196789.html
Copyright © 2020-2023  润新知