返回介绍

solution / 2700-2799 / 2729.Check if The Number is Fascinating / README_EN

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

2729. Check if The Number is Fascinating

中文文档

Description

You are given an integer n that consists of exactly 3 digits.

We call the number n fascinating if, after the following modification, the resulting number contains all the digits from 1 to 9 exactly once and does not contain any 0's:

  • Concatenate n with the numbers 2 * n and 3 * n.

Return true_ if _n_ is fascinating, or _false_ otherwise_.

Concatenating two numbers means joining them together. For example, the concatenation of 121 and 371 is 121371.

 

Example 1:

Input: n = 192
Output: true
Explanation: We concatenate the numbers n = 192 and 2 * n = 384 and 3 * n = 576. The resulting number is 192384576. This number contains all the digits from 1 to 9 exactly once.

Example 2:

Input: n = 100
Output: false
Explanation: We concatenate the numbers n = 100 and 2 * n = 200 and 3 * n = 300. The resulting number is 100200300. This number does not satisfy any of the conditions.

 

Constraints:

  • 100 <= n <= 999

Solutions

Solution 1

class Solution:
  def isFascinating(self, n: int) -> bool:
    s = str(n) + str(2 * n) + str(3 * n)
    return "".join(sorted(s)) == "123456789"
class Solution {
  public boolean isFascinating(int n) {
    String s = "" + n + (2 * n) + (3 * n);
    int[] cnt = new int[10];
    for (char c : s.toCharArray()) {
      if (++cnt[c - '0'] > 1) {
        return false;
      }
    }
    return cnt[0] == 0 && s.length() == 9;
  }
}
class Solution {
public:
  bool isFascinating(int n) {
    string s = to_string(n) + to_string(n * 2) + to_string(n * 3);
    sort(s.begin(), s.end());
    return s == "123456789";
  }
};
func isFascinating(n int) bool {
  s := strconv.Itoa(n) + strconv.Itoa(n*2) + strconv.Itoa(n*3)
  cnt := [10]int{}
  for _, c := range s {
    cnt[c-'0']++
    if cnt[c-'0'] > 1 {
      return false
    }
  }
  return cnt[0] == 0 && len(s) == 9
}
function isFascinating(n: number): boolean {
  const s = `${n}${n * 2}${n * 3}`;
  return s.split('').sort().join('') === '123456789';
}
impl Solution {
  pub fn is_fascinating(n: i32) -> bool {
    let s = format!("{}{}{}", n, n * 2, n * 3);

    let mut cnt = vec![0; 10];
    for c in s.chars() {
      let t = (c as usize) - ('0' as usize);
      cnt[t] += 1;
      if cnt[t] > 1 {
        return false;
      }
    }

    cnt[0] == 0 && s.len() == 9
  }
}

Solution 2

use std::collections::HashMap;

impl Solution {
  pub fn is_fascinating(mut n: i32) -> bool {
    let mut i = n * 2;
    let mut j = n * 3;

    let mut hash = HashMap::new();

    while n != 0 {
      let cnt = hash.entry(n % 10).or_insert(0);
      *cnt += 1;
      n /= 10;
    }

    while i != 0 {
      let cnt = hash.entry(i % 10).or_insert(0);
      *cnt += 1;
      i /= 10;
    }

    while j != 0 {
      let cnt = hash.entry(j % 10).or_insert(0);
      *cnt += 1;
      j /= 10;
    }

    for k in 1..=9 {
      if !hash.contains_key(&k) || hash[&k] > 1 {
        return false;
      }
    }

    !hash.contains_key(&0)
  }
}

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

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

发布评论

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