返回介绍

solution / 2800-2899 / 2843.Count Symmetric Integers / README_EN

发布于 2024-06-17 01:02:59 字数 4413 浏览 0 评论 0 收藏 0

2843. Count Symmetric Integers

中文文档

Description

You are given two positive integers low and high.

An integer x consisting of 2 * n digits is symmetric if the sum of the first n digits of x is equal to the sum of the last n digits of x. Numbers with an odd number of digits are never symmetric.

Return _the number of symmetric integers in the range_ [low, high].

 

Example 1:

Input: low = 1, high = 100
Output: 9
Explanation: There are 9 symmetric integers between 1 and 100: 11, 22, 33, 44, 55, 66, 77, 88, and 99.

Example 2:

Input: low = 1200, high = 1230
Output: 4
Explanation: There are 4 symmetric integers between 1200 and 1230: 1203, 1212, 1221, and 1230.

 

Constraints:

  • 1 <= low <= high <= 104

Solutions

Solution 1: Enumeration

We enumerate each integer $x$ in the range $[low, high]$, and check whether it is a palindromic number. If it is, then the answer $ans$ is increased by $1$.

The time complexity is $O(n \times \log m)$, and the space complexity is $O(\log m)$. Here, $n$ is the number of integers in the range $[low, high]$, and $m$ is the maximum integer given in the problem.

class Solution:
  def countSymmetricIntegers(self, low: int, high: int) -> int:
    def f(x: int) -> bool:
      s = str(x)
      if len(s) & 1:
        return False
      n = len(s) // 2
      return sum(map(int, s[:n])) == sum(map(int, s[n:]))

    return sum(f(x) for x in range(low, high + 1))
class Solution {
  public int countSymmetricIntegers(int low, int high) {
    int ans = 0;
    for (int x = low; x <= high; ++x) {
      ans += f(x);
    }
    return ans;
  }

  private int f(int x) {
    String s = "" + x;
    int n = s.length();
    if (n % 2 == 1) {
      return 0;
    }
    int a = 0, b = 0;
    for (int i = 0; i < n / 2; ++i) {
      a += s.charAt(i) - '0';
    }
    for (int i = n / 2; i < n; ++i) {
      b += s.charAt(i) - '0';
    }
    return a == b ? 1 : 0;
  }
}
class Solution {
public:
  int countSymmetricIntegers(int low, int high) {
    int ans = 0;
    auto f = [](int x) {
      string s = to_string(x);
      int n = s.size();
      if (n & 1) {
        return 0;
      }
      int a = 0, b = 0;
      for (int i = 0; i < n / 2; ++i) {
        a += s[i] - '0';
        b += s[n / 2 + i] - '0';
      }
      return a == b ? 1 : 0;
    };
    for (int x = low; x <= high; ++x) {
      ans += f(x);
    }
    return ans;
  }
};
func countSymmetricIntegers(low int, high int) (ans int) {
  f := func(x int) int {
    s := strconv.Itoa(x)
    n := len(s)
    if n&1 == 1 {
      return 0
    }
    a, b := 0, 0
    for i := 0; i < n/2; i++ {
      a += int(s[i] - '0')
      b += int(s[n/2+i] - '0')
    }
    if a == b {
      return 1
    }
    return 0
  }
  for x := low; x <= high; x++ {
    ans += f(x)
  }
  return
}
function countSymmetricIntegers(low: number, high: number): number {
  let ans = 0;
  const f = (x: number): number => {
    const s = x.toString();
    const n = s.length;
    if (n & 1) {
      return 0;
    }
    let a = 0;
    let b = 0;
    for (let i = 0; i < n >> 1; ++i) {
      a += Number(s[i]);
      b += Number(s[(n >> 1) + i]);
    }
    return a === b ? 1 : 0;
  };
  for (let x = low; x <= high; ++x) {
    ans += f(x);
  }
  return ans;
}

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

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

发布评论

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