返回介绍

solution / 2500-2599 / 2535.Difference Between Element Sum and Digit Sum of an Array / README_EN

发布于 2024-06-17 01:03:04 字数 4683 浏览 0 评论 0 收藏 0

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

中文文档

Description

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 necessarily distinct) that appear in nums.

Return _the absolute difference between the element sum and digit sum of _nums.

Note that the absolute difference between two integers x and y is defined as |x - y|.

 

Example 1:

Input: nums = [1,15,6,3]
Output: 9
Explanation: 
The element sum of nums is 1 + 15 + 6 + 3 = 25.
The digit sum of nums is 1 + 1 + 5 + 6 + 3 = 16.
The absolute difference between the element sum and digit sum is |25 - 16| = 9.

Example 2:

Input: nums = [1,2,3,4]
Output: 0
Explanation:
The element sum of nums is 1 + 2 + 3 + 4 = 10.
The digit sum of nums is 1 + 2 + 3 + 4 = 10.
The absolute difference between the element sum and digit sum is |10 - 10| = 0.

 

Constraints:

  • 1 <= nums.length <= 2000
  • 1 <= nums[i] <= 2000

Solutions

Solution 1: Simulation

We traverse the array $nums$, calculate the sum of elements $a$ and the sum of digits $b$, and finally return $|a - b|$.

The time complexity is $O(n)$, where $n$ is the length of the array $nums$. The space complexity is $O(1)$.

class Solution:
  def differenceOfSum(self, nums: List[int]) -> int:
    a, b = sum(nums), 0
    for x in nums:
      while x:
        b += x % 10
        x //= 10
    return abs(a - b)
class Solution {
  public int differenceOfSum(int[] nums) {
    int a = 0, b = 0;
    for (int x : nums) {
      a += x;
      for (; x > 0; x /= 10) {
        b += x % 10;
      }
    }
    return Math.abs(a - b);
  }
}
class Solution {
public:
  int differenceOfSum(vector<int>& nums) {
    int a = 0, b = 0;
    for (int x : nums) {
      a += x;
      for (; x; x /= 10) {
        b += x % 10;
      }
    }
    return abs(a - b);
  }
};
func differenceOfSum(nums []int) int {
  a, b := 0, 0
  for _, x := range nums {
    a += x
    for ; x > 0; x /= 10 {
      b += x % 10
    }
  }
  return abs(a - b)
}

func abs(x int) int {
  if x < 0 {
    return -x
  }
  return x
}
function differenceOfSum(nums: number[]): number {
  return nums.reduce((r, v) => {
    r += v;
    while (v !== 0) {
      r -= v % 10;
      v = Math.floor(v / 10);
    }
    return r;
  }, 0);
}
impl Solution {
  pub fn difference_of_sum(nums: Vec<i32>) -> i32 {
    let mut ans = 0;
    for &num in nums.iter() {
      let mut num = num;
      ans += num;
      while num != 0 {
        ans -= num % 10;
        num /= 10;
      }
    }
    ans
  }
}
int differenceOfSum(int* nums, int numsSize) {
  int ans = 0;
  for (int i = 0; i < numsSize; i++) {
    ans += nums[i];
    while (nums[i]) {
      ans -= nums[i] % 10;
      nums[i] /= 10;
    }
  }
  return ans;
}

Solution 2

impl Solution {
  pub fn difference_of_sum(nums: Vec<i32>) -> i32 {
    let a: i32 = nums.iter().sum();
    let b: i32 = nums
      .iter()
      .map(|&n|
        n
          .to_string()
          .chars()
          .map(|c| c.to_digit(10).unwrap() as i32)
          .sum::<i32>()
      )
      .sum();
    (a - b).abs()
  }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文