https://leetcode.com/problems/difference-between-element-sum-and-digit-sum-of-an-array/description/
Difference Between Element Sum and Digit Sum of an Array - LeetCode
Can you solve this real interview question? Difference Between Element Sum and Digit Sum of an Array - You are given a positive integer array nums. * The element sum is the sum of all the elements in nums. * The digit sum is the sum of all the digits (not
leetcode.com
1
2
3
4
5
6
7
8
9
10
|
class Solution:
def differenceOfSum(self, nums: List[int]) -> int:
sum_nums = 0
digit_sum = 0
for i in nums:
sum_nums +=i
for digit in str(i):
digit_sum += int(digit)
return abs(sum_nums - digit_sum)
|
cs |
반응형
'python-algorithm' 카테고리의 다른 글
leetcode 2465. Number of Distinct Averages (0) | 2023.01.19 |
---|---|
leetcode 2496. Maximum Value of a String in an Array (0) | 2023.01.19 |
leetcode 326. Power of Three (0) | 2023.01.17 |
leetcode 263. Ugly Number (0) | 2023.01.17 |
leetcode 231. Power of Two (0) | 2023.01.17 |
댓글