当前位置:网站首页>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))])
边栏推荐
- [quick start of Digital IC Verification] 17. Basic grammar of SystemVerilog learning 4 (randomization)
- Redis technology leak detection and filling (II) - expired deletion strategy
- [UVM basics] summary of important knowledge points of "UVM practice" (continuous update...)
- [UVM practice] Chapter 1: configuring the UVM environment (taking VCs as an example), run through the examples in the book
- Yugu p1020 missile interception (binary search)
- [quickstart to Digital IC Validation] 15. Basic syntax for SystemVerilog Learning 2 (operator, type conversion, loop, Task / Function... Including practical exercises)
- Thinkcmf6.0安装教程
- 2022 welder (elementary) judgment questions and online simulation examination
- Cnopendata list data of Chinese colleges and Universities
- The charm of SQL optimization! From 30248s to 0.001s
猜你喜欢
Wechat applet data binding multiple data
[UTCTF2020]file header
Operation suggestions for today's spot Silver
Hands on deep learning (IV) -- convolutional neural network CNN
[experience sharing] how to expand the cloud service icon for Visio
Thinkcmf6.0安装教程
Bugku CTF daily one question chessboard with only black chess
探索Cassandra的去中心化分布式架构
Common method signatures and meanings of Iterable, collection and list
Li Kou interview question 04.01 Path between nodes
随机推荐
[UVM practice] Chapter 2: a simple UVM verification platform (2) only driver verification platform
[unity] several ideas about circular motion of objects
这5个摸鱼神器太火了!程序员:知道了快删!
【VHDL 并行语句执行】
Linux server development, MySQL process control statement
Cnopendata geographical distribution data of religious places in China
[webrtc] M98 screen and window acquisition
Why should we understand the trend of spot gold?
青龙面板--花花阅读
2022年茶艺师(中级)考试试题及模拟考试
pytest+allure+jenkins环境--填坑完毕
Ansible
pytest+allure+jenkins環境--填坑完畢
[guess-ctf2019] fake compressed packets
自定义类加载器加载网络Class
Redis technology leak detection and filling (II) - expired deletion strategy
追风赶月莫停留,平芜尽处是春山
C语言航班订票系统
Button wizard collection learning - mineral medicine collection and running map
These five fishing artifacts are too hot! Programmer: I know, delete it quickly!