Simple 5 lines Recursive C++ codes


  • 1
    int helper(vector<int>& nums, int l, int r) {
        if (l==r) return nums[l];
        int m = (l+r)/2;
        if (nums[m]<nums[r]) return helper(nums, l, m);
        else if (nums[m]>nums[r]) return helper(nums, m+1, r);
        else return min(helper(nums, l, m), helper(nums, m+1, r));
    }
    int findMin(vector<int>& nums) {
        return helper(nums, 0, nums.size()-1);
    }

Log in to reply
 

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