返回介绍

solution / 2500-2599 / 2544.Alternating Digit Sum / README_EN

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

2544. Alternating Digit Sum

中文文档

Description

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 opposite sign to its adjacent digits.

Return _the sum of all digits with their corresponding sign_.

 

Example 1:

Input: n = 521
Output: 4
Explanation: (+5) + (-2) + (+1) = 4.

Example 2:

Input: n = 111
Output: 1
Explanation: (+1) + (-1) + (+1) = 1.

Example 3:

Input: n = 886996
Output: 0
Explanation: (+8) + (-8) + (+6) + (-9) + (+9) + (-6) = 0.

 

Constraints:

  • 1 <= n <= 109

 

Solutions

Solution 1: Simulation

We can directly simulate the process as described in the problem.

We define an initial symbol $sign=1$. Starting from the most significant digit, we take out one digit $x$ each time, multiply it by $sign$, add the result to the answer, then negate $sign$, and continue to process the next digit until all digits are processed.

The time complexity is $O(\log n)$, and the space complexity is $O(\log n)$. Here, $n$ is the given number.

class Solution:
  def alternateDigitSum(self, n: int) -> int:
    return sum((-1) ** i * int(x) for i, x in enumerate(str(n)))
class Solution {
  public int alternateDigitSum(int n) {
    int ans = 0, sign = 1;
    for (char c : String.valueOf(n).toCharArray()) {
      int x = c - '0';
      ans += sign * x;
      sign *= -1;
    }
    return ans;
  }
}
class Solution {
public:
  int alternateDigitSum(int n) {
    int ans = 0, sign = 1;
    for (char c : to_string(n)) {
      int x = c - '0';
      ans += sign * x;
      sign *= -1;
    }
    return ans;
  }
};
func alternateDigitSum(n int) (ans int) {
  sign := 1
  for _, c := range strconv.Itoa(n) {
    x := int(c - '0')
    ans += sign * x
    sign *= -1
  }
  return
}
function alternateDigitSum(n: number): number {
  let ans = 0;
  let sign = 1;
  while (n) {
    ans += (n % 10) * sign;
    sign = -sign;
    n = Math.floor(n / 10);
  }
  return ans * -sign;
}
impl Solution {
  pub fn alternate_digit_sum(mut n: i32) -> i32 {
    let mut ans = 0;
    let mut sign = 1;
    while n != 0 {
      ans += (n % 10) * sign;
      sign = -sign;
      n /= 10;
    }
    ans * -sign
  }
}
int alternateDigitSum(int n) {
  int ans = 0;
  int sign = 1;
  while (n) {
    ans += (n % 10) * sign;
    sign = -sign;
    n /= 10;
  }
  return ans * -sign;
}

Solution 2

class Solution:
  def alternateDigitSum(self, n: int) -> int:
    ans, sign = 0, 1
    for c in str(n):
      x = int(c)
      ans += sign * x
      sign *= -1
    return ans
impl Solution {
  pub fn alternate_digit_sum(n: i32) -> i32 {
    let mut ans = 0;
    let mut sign = 1;

    for c in format!("{}", n).chars() {
      let x = c.to_digit(10).unwrap() as i32;
      ans += x * sign;
      sign *= -1;
    }

    ans
  }
}

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

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

发布评论

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