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 |
반응형
'python-algorithm' 카테고리의 다른 글
leetcode 2739. Total Distance Traveled (0) | 2023.07.07 |
---|---|
leetcode 2651. Calculate Delayed Arrival Time (0) | 2023.07.06 |
leetcode 2595. Number of Even and Odd Bits (0) | 2023.06.27 |
leetcode 2716. Minimize String Length (0) | 2023.06.27 |
leetcode 2710. Remove Trailing Zeros From a String (0) | 2023.06.26 |
댓글