4 lines Python via sorting


  • 2

    Since length of the array won't exceed 10000, O(nlogn) sorting is good enough.

    class Solution(object):
        def findUnsortedSubarray(self, nums):
            """
            :type nums: List[int]
            :rtype: int
            """
            n, sorts = len(nums), sorted(nums)
            if nums == sorts: return 0
            l, r = min(i for i in range(n) if nums[i] != sorts[i]), max(i for i in range(n) if nums[i] != sorts[i])
            return r - l + 1
    

  • 0
    K

    Could you explain this

    l, r = min(i for i in range(n) if nums[i] != sorts[i]), max(i for i in range(n) if nums[i] != sorts[i])


  • 0
    L

    @kromah find the begin and end of the sub array


Log in to reply
 

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