Ruby Simple Solution


  • 0
    I
    # @param {Integer[]} nums
    # @return {Integer}
    def find_unsorted_subarray(nums)
        first_index = nil
        last_index = nil
        nums.sort.each_with_index do |object,index|
            if object != nums[index]
                first_index = first_index.nil?? index : first_index
                last_index = index + 1
            end
        end
        if first_index && last_index
            return last_index - first_index
        else
            return 0
        end
    end
    ´´´

Log in to reply
 

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