Python 2 lines easy understand solution


  • 13

    Sort the list and check if it's still the same number in the list.

    def findUnsortedSubarray(self, nums):
            is_same = [a == b for a, b in zip(nums, sorted(nums))]
            return 0 if all(is_same) else len(nums) - is_same.index(False) - is_same[::-1].index(False)

  • 2

    Same idea but a one-liner:
    string.strip() method will strip surrounding spaces by default. So we only need to count how many chars left over.

        def findUnsortedSubarray(self, nums):
            return len(''.join(('.', ' ')[m==n] for m, n in zip(sorted(nums), nums)).strip())
    

  • 0

    @waigx
    Interesting solution.


Log in to reply
 

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