1 Star 0 Fork 332

peaking / leetcode

forked from doocs / leetcode 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
README.md 4.30 KB
一键复制 编辑 原始数据 按行查看 历史
ylb 提交于 2024-02-21 08:52 . feat: add problem tags (#2361)

2283. 判断一个数的数字计数是否等于数位的值

English Version

题目描述

给你一个下标从 0 开始长度为 n 的字符串 num ,它只包含数字。

如果对于 每个 0 <= i < n 的下标 i ,都满足数位 i 在 num 中出现了 num[i]次,那么请你返回 true ,否则返回 false 。

 

示例 1:

输入:num = "1210"
输出:true
解释:
num[0] = '1' 。数字 0 在 num 中出现了一次。
num[1] = '2' 。数字 1 在 num 中出现了两次。
num[2] = '1' 。数字 2 在 num 中出现了一次。
num[3] = '0' 。数字 3 在 num 中出现了零次。
"1210" 满足题目要求条件,所以返回 true 。

示例 2:

输入:num = "030"
输出:false
解释:
num[0] = '0' 。数字 0 应该出现 0 次,但是在 num 中出现了两次。
num[1] = '3' 。数字 1 应该出现 3 次,但是在 num 中出现了零次。
num[2] = '0' 。数字 2 在 num 中出现了 0 次。
下标 0 和 1 都违反了题目要求,所以返回 false 。

 

提示:

  • n == num.length
  • 1 <= n <= 10
  • num 只包含数字。

解法

方法一:计数 + 枚举

统计字符串中每个数字出现的次数,然后枚举每个数字,判断其出现的次数是否与其值相等,若都相等则返回 true,否则返回 false

时间复杂度 $O(n)$,空间复杂度 $O(C)$。其中 $n$ 是字符串 num 的长度,而 $C$ 是数字的个数。本题中 $C=10$。

class Solution:
    def digitCount(self, num: str) -> bool:
        cnt = Counter(num)
        return all(cnt[str(i)] == int(v) for i, v in enumerate(num))
class Solution {
    public boolean digitCount(String num) {
        int[] cnt = new int[10];
        int n = num.length();
        for (int i = 0; i < n; ++i) {
            ++cnt[num.charAt(i) - '0'];
        }
        for (int i = 0; i < n; ++i) {
            if (cnt[i] != num.charAt(i) - '0') {
                return false;
            }
        }
        return true;
    }
}
class Solution {
public:
    bool digitCount(string num) {
        int cnt[10]{};
        for (char& c : num) {
            ++cnt[c - '0'];
        }
        for (int i = 0; i < num.size(); ++i) {
            if (cnt[i] != num[i] - '0') {
                return false;
            }
        }
        return true;
    }
};
func digitCount(num string) bool {
	cnt := [10]int{}
	for _, c := range num {
		cnt[c-'0']++
	}
	for i, v := range num {
		if cnt[i] != int(v-'0') {
			return false
		}
	}
	return true
}
function digitCount(num: string): boolean {
    const n = num.length;
    const count = new Array(10).fill(0);
    for (let i = 0; i < n; i++) {
        count[i] = Number(num[i]);
    }
    for (const c of num) {
        count[c]--;
    }
    return count.every(v => v === 0);
}
impl Solution {
    pub fn digit_count(num: String) -> bool {
        let s = num.as_bytes();
        let n = num.len();
        let mut count = [0; 10];
        for i in 0..n {
            count[i] = s[i] - b'0';
        }
        for c in s {
            count[(c - b'0') as usize] -= 1;
        }
        count.iter().all(|v| *v == 0)
    }
}
bool digitCount(char* num) {
    int count[10] = {0};
    for (int i = 0; num[i]; i++) {
        count[i] = num[i] - '0';
    }
    for (int i = 0; num[i]; i++) {
        count[num[i] - '0']--;
    }
    for (int i = 0; i < 10; i++) {
        if (count[i] != 0) {
            return false;
        }
    }
    return true;
}
Java
1
https://gitee.com/peaklee1134/leetcode.git
git@gitee.com:peaklee1134/leetcode.git
peaklee1134
leetcode
leetcode
main

搜索帮助