My accepted O(n) C++ code


  • -4
    R

    it is very obviously that if all the elements in the array are not equal, the min one should smaller than the one before it. so below is my answer.

    class Solution {
    public:
        int findMin(vector<int> &num) {
            if(num.size() == 1 || num[0] < num[num.size()-1] ) return num[0];
            for(int i = 1; i < num.size(); i++) {
                if(num[i] < num[i-1]) return num[i];
            return num[0];
        }
    };

  • 0
    S
    This post is deleted!

  • 0
    I

    I think you miss a '}' below the for loop.


Log in to reply
 

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