14-line Python solution


  • 0
    class Solution(object):
        def numberOfArithmeticSlices(self, nums):
            """
            :type nums: List[int]
            :rtype: int
            """
            ans = 0
            if len(nums) > 2:
                diff = [nums[i] - nums[i - 1] for i in xrange(1, len(nums))]
                count = 1
                pre = diff[0]
                for i in xrange(1, len(diff)):
                    if diff[i] == pre:
                        count += 1
                    else:
                        ans += count * (count - 1) / 2
                        count = 1
                    pre = diff[i]
                ans += count * (count - 1) / 2
            return ans
    

Log in to reply
 

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