My simple iterative C++ code(4ms)


  • 0
    M
    class Solution {
    public:
        int findMin(vector<int>& nums) {
            
            int size = nums.size();
            int l = 0;
            int r = size-1;
            
            while(r-l>1)
            {
                int m = l+(r-l)/2;
                
                if(nums[m] > nums[r])
                {
                    l = m;
                }
                else
                {
                    r = m;
                }
                
            }
            
            return (nums[r]<nums[l]?nums[r]:nums[l]);
        }
    };

Log in to reply
 

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