LeetCode·每日一题·2283.判断一个数的数字计数是否等于数位的值·模拟
2023-09-27 14:26:29 时间
作者:小迅
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
题目
示例
思路
题意 -> 判断给定字符串中的数字出现次数 都满足数位 i 在 num 中出现了 num[i]次
直接按照题意进行模拟,创建两个数组,一个记录字符串中数字的出现次数,一个记录字符串中数字应该出现的次数,最后将两个数组进行比较,不相同则输出false,相同则输出true
代码
bool digitCount(char * num){
int hash[10] = {0};
int count[10] = {0};//初始化
int len = strlen(num);
for (int i = 0; i < len; ++i) {//遍历枚举数组
hash[i] = num[i] - '0';
++count[num[i] - '0'];
}
for (int i = 0; i < 10; ++i) {//判断两个数组
if (hash[i] != count[i]) {
return false;
}
}
return true;
}
作者:小迅
链接:https://leetcode.cn/problems/check-if-number-has-equal-digit-count-and-digit-value/solutions/2056148/mo-ni-zhu-shi-chao-ji-xiang-xi-by-xun-ge-e2ld/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
相关文章
- 【LeetCode】打家劫舍 III [M](递归)
- 【LeetCode】最大正方形 [M](动态规划)
- 【LeetCode】在排序数组中查找元素的第一个和最后一个位置 [M](二分)
- 【LeetCode】最大的以 1 为边界的正方形 [M](模拟)
- LeetCode_回溯_动态规划_中等_131.分割回文串
- LeetCode·每日一题·2437. 有效时间的数目·模拟
- LeetCode·1637. 两点之间不包含任何点的最宽垂直区域·模拟
- LeetCode·每日一题·1638. 统计只差一个字符的子串数目·模拟·
- LeetCode·每日一题·2469. 温度转换·模拟
- LeetCode·每日一题·2373.矩阵中的局部最大值·模拟
- LeetCode·每日一题·1144.递减元素使数组呈锯齿状·模拟
- LeetCode·每日一题·2180.统计各位数字之和为偶数的整数个数·模拟
- LeetCode·每日一题·1764.通过连接另一个数组的子数组得到一个数组·模拟
- LeetCode·每日一题·1684.统计一致字符串的数目·模拟
- LeetCode·每日一题·1784.检查二进制字符串字段·模拟
- LeetCode·每日一题·面试题 01.08. 零矩阵· 模拟
- LeetCode·每日一题·788.旋转数字·模拟
- LeetCode·每日一题·946.验证栈序列·栈模拟
- LeetCode·每日一题·1592.重新排列单词间的空格·模拟
- LeetCode·每日一题·592.分数加减运算·模拟
- LeetCode·第306竞赛·6148.剑指中的局部最大值·模拟
- LeetCode·4寻找两个正序数组的中位数·模拟
- 「LeetCode」622. 设计循环队列
- LeetCode-108. 将有序数组转换为二叉搜索树(java)
- [LeetCode] 384. Shuffle an Array 数组洗牌
- [LeetCode] 261. Graph Valid Tree 图是否是树
- leetcode 647 回文子串
- leetcode 227 基本计算器II
- leetcode 860柠檬水找零
- leetcode 20 有效的括号