python-algorithm
leetcode 2544. Alternating Digit Sum
무적김두칠
2023. 7. 3. 11:07
https://leetcode.com/problems/alternating-digit-sum/description/
Alternating Digit Sum - LeetCode
Can you solve this real interview question? Alternating Digit Sum - You are given a positive integer n. Each digit of n has a sign according to the following rules: * The most significant digit is assigned a positive sign. * Each other digit has an opposit
leetcode.com
1
2
3
4
5
6
7
8
9
10
11
|
class Solution:
def alternateDigitSum(self, n: int) -> int:
n = str(n)
answer = 0
for i, num in enumerate(n):
if i % 2 == 0:
answer += int(num)
else:
answer += -int(num)
return answer
|
cs |
반응형