Skip to content

Commit a670368

Browse files
authored
Create alternating-digit-sum.py
1 parent 00f4cb5 commit a670368

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

Python/alternating-digit-sum.py

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Time: O(logn)
2+
# Space: O(1)
3+
4+
# math
5+
class Solution(object):
6+
def alternateDigitSum(self, n):
7+
"""
8+
:type n: int
9+
:rtype: int
10+
"""
11+
result = 0
12+
sign = 1
13+
while n:
14+
sign *= -1
15+
result += sign*(n%10)
16+
n //= 10
17+
return sign*result

0 commit comments

Comments
 (0)