当前位置:网站首页>LeetCode简单题之判断一个数的数字计数是否等于数位的值
LeetCode简单题之判断一个数的数字计数是否等于数位的值
2022-07-07 04:47:00 【·星辰大海】
题目
给你一个下标从 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 只包含数字。
来源:力扣(LeetCode)
解题思路
根据题目的意思,首先需要求出各个数字在字符串中的频率,然后遍历整个字符串检查每个元素是否符合条件。
class Solution:
def digitCount(self, num: str) -> bool:
return all([str(Counter(num)[str(i)])==num[i] for i in range(len(num))])

边栏推荐
猜你喜欢

The configuration that needs to be modified when switching between high and low versions of MySQL 5-8 (take aicode as an example here)

【webrtc】m98 screen和window采集

探索Cassandra的去中心化分布式架构

这5个摸鱼神器太火了!程序员:知道了快删!

Force buckle 145 Binary Tree Postorder Traversal

Es FAQ summary

【數字IC驗證快速入門】15、SystemVerilog學習之基本語法2(操作符、類型轉換、循環、Task/Function...內含實踐練習)

开源生态|打造活力开源社区,共建开源新生态!

Sign up now | oar hacker marathon phase III, waiting for your challenge

Yugu p1020 missile interception (binary search)
随机推荐
2022 simulated examination question bank and online simulated examination of tea master (primary) examination questions
Wechat applet data binding multiple data
QT learning 26 integrated example of layout management
[unity] several ideas about circular motion of objects
[SUCTF 2019]Game
2022制冷与空调设备运行操作复训题库及答案
LeetCode 40:组合总和 II
Padavan manually installs PHP
2022 Inner Mongolia latest advanced fire facility operator simulation examination question bank and answers
Rust versus go (which is my preferred language?)
大视频文件的缓冲播放原理以及实现
Pytest+allure+jenkins installation problem: pytest: error: unrecognized arguments: --alluredir
【数字IC验证快速入门】13、SystemVerilog interface 和 program 学习
C language flight booking system
C language queue
Introduction to basic components of wechat applet
Téléchargement des données de conception des puces
padavan手动安装php
Leanote private cloud note building
You Li takes you to talk about C language 6 (common keywords)