Share code for both I and II


  • 0
    M
    public int findMin(int[] num) {
        if(num.length==0||num==null)
        return 0;
        for(int i=0;i<num.length-1;i++){
            if(num[i]>num[i+1])
            return num[i+1];
        }
        return num[0];
    }

  • 0
    D
    class Solution {  // same code for both problems
    public:
        int findMin(vector<int> &num) {
            int min = num[0];  // by default min is at position 0
            const int size = num.size();
            for (int i = 1; i < size; i++) {
                if (num[i] < min)
                    return num[i];
            }
            return min;
        }
    };

  • 0
    M

    change the if to "if( num == null || num.length == 0)" then I entirely agree


  • 0
    Q

    tag for this question is binary search.


Log in to reply
 

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