An accepted code. Looking for advice in improving my code.


  • 0
    C

    This is my accepted code. It is very long and clumsy. Any advice will be appreciated.

    class Solution {
    public:
        int gen(vector<int> &num, int start, int end){
            if (start>end) return INT_MAX;
            int mid, min;
            mid = start + ((end-start)>>1);
            if (num[mid]>=num[start]){
                min = gen(num,mid+1,end);
                if (min>num[start]) min = num[start];
            }
            else{
                min = gen(num,start,mid-1);
                if (min>num[mid]) min = num[mid];
            }
            return min;
        }
        int gen1(vector<int> &num, int start, int end){
            if (start>end) return INT_MAX;
            int mid, min;
            mid = start + ((end-start)>>1);
            if (num[mid]>num[start]){
                min = gen1(num,start,mid-1);
                if (min>num[end]) min = num[end];
            }
            else{
                min = gen1(num,mid+1,end);
                if (min>num[mid]) min = num[mid];
            }
            return min;
        }
        int findMin(vector<int> &num) {
            if (num.size()==0) return -1;
            return min(gen(num,0,num.size()-1),gen1(num,0,num.size()-1));
        }
    };

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.