python-algorithm

leetcode 2535. Difference Between Element Sum and Digit Sum of an Array

무적김두칠 2023. 1. 17. 16:26

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
반응형