. - 力扣(LeetCode)

from typing import List
 
 
class Solution:
	# 直接根据题意解题即可
	def differenceOfSum(self, nums: List[int]) -> int:
		nums_single_list = []
		for num in nums:
			while num != 0:
				nums_single_list.append(num % 10)
				num = num // 10
		return abs(sum(nums_single_list) - sum(nums))
 
	# 参考官方解题答案优化后代码,在循环中直接把求和做了。然后绝对值的逻辑是多余的,元素的和必定大于每一位数的和,所以直接返回元素和减去每一位数的和即可。
	def differenceOfSumEx(self, nums: List[int]) -> int:
		num_sum = 0
		single_sum = 0
		nums_single_list = []
		for num in nums:
			num_sum += num
			while num != 0:
				single_sum += num % 10
				nums_single_list.append(num % 10)
				num = num // 10
 
		return num_sum - single_sum