当前位置:网站首页>The simple problem of leetcode is to judge whether the number count of a number is equal to the value of the number
The simple problem of leetcode is to judge whether the number count of a number is equal to the value of the number
2022-07-07 08:09:00 【·Starry Sea】
subject
I'll give you a subscript from 0 Start length is n String num , It contains only numbers .
If for Every 0 <= i < n The subscript i , All meet the digit i stay num In the num[i] Time , So please go back to true , Otherwise return to false .
Example 1:
Input :num = “1210”
Output :true
explain :
num[0] = ‘1’ . Numbers 0 stay num There's a time when .
num[1] = ‘2’ . Numbers 1 stay num There were two times in .
num[2] = ‘1’ . Numbers 2 stay num There's a time when .
num[3] = ‘0’ . Numbers 3 stay num There are zero occurrences in .
“1210” Meet the requirements of the topic , So back true .
Example 2:
Input :num = “030”
Output :false
explain :
num[0] = ‘0’ . Numbers 0 There should be 0 Time , But in num There's a time when .
num[1] = ‘3’ . Numbers 1 There should be 3 Time , But in num There are zero occurrences in .
num[2] = ‘0’ . Numbers 2 stay num In the 0 Time .
Subscript 0 and 1 Have violated the title requirements , So back false .
Tips :
n == num.length
1 <= n <= 10
num Numbers only .
source : Power button (LeetCode)
Their thinking
According to the meaning of the topic , First, you need to find the frequency of each number in the string , Then traverse the entire string to check whether each element meets the conditions .
class Solution:
def digitCount(self, num: str) -> bool:
return all([str(Counter(num)[str(i)])==num[i] for i in range(len(num))])
边栏推荐
- ZCMU--1492: Problem D(C语言)
- Implementation of replacement function of shell script
- LeetCode中等题之我的日程安排表 I
- 快速使用 Jacoco 代码覆盖率统计
- Li Kou interview question 04.01 Path between nodes
- Empire CMS collection Empire template program general
- Who has docker to install MySQL locally?
- Linux server development, redis protocol and asynchronous mode
- [matlab] when matrix multiplication in Simulink user-defined function does not work properly, matrix multiplication module in module library can be used instead
- Roulette chart 2 - writing of roulette chart code
猜你喜欢
快解析内网穿透为文档加密行业保驾护航
2022 recurrent training question bank and answers of refrigeration and air conditioning equipment operation
LeetCode简单题之字符串中最大的 3 位相同数字
【数字IC验证快速入门】11、Verilog TestBench(VTB)入门
2022 National latest fire-fighting facility operator (primary fire-fighting facility operator) simulation questions and answers
调用 pytorch API完成线性回归
Qt学习26 布局管理综合实例
Explore dry goods! Apifox construction ideas
【數字IC驗證快速入門】15、SystemVerilog學習之基本語法2(操作符、類型轉換、循環、Task/Function...內含實踐練習)
Notes on PHP penetration test topics
随机推荐
jeeSite 表单页面的Excel 导入功能
Find the mode in the binary search tree (use medium order traversal as an ordered array)
Es FAQ summary
Summary of redis functions
贝叶斯定律
Padavan manually installs PHP
game攻防世界逆向
复杂网络建模(三)
Network learning (I) -- basic model learning
Force buckle 145 Binary Tree Postorder Traversal
[matlab] when matrix multiplication in Simulink user-defined function does not work properly, matrix multiplication module in module library can be used instead
C language queue
调用 pytorch API完成线性回归
Quick analysis of Intranet penetration helps the foreign trade management industry cope with a variety of challenges
【数字IC验证快速入门】14、SystemVerilog学习之基本语法1(数组、队列、结构体、枚举、字符串...内含实践练习)
Recursive construction of maximum binary tree
Niu Mei's mathematical problem --- combinatorial number
The legend about reading the configuration file under SRC
Binary tree and heap building in C language
2022 tea master (intermediate) examination questions and mock examination