Skip to content
LC-2544 Easy LeetCode

2544. Alternating Digit Sum

Read the full problem statement on LeetCode.
Difficulty: easy Acceptance: 68% Topics: Math
View full problem on LeetCode

Reading material

Reference solution (spoiler · python)
# Time:  O(logn)
# Space: O(1)

# math
class Solution(object):
    def alternateDigitSum(self, n):
        """
        :type n: int
        :rtype: int
        """
        result = 0
        sign = 1
        while n:
            sign *= -1
            result += sign*(n%10)
            n //= 10
        return sign*result

Solution from kamyu104/LeetCode-Solutions · MIT