当前位置:网站首页>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))])
边栏推荐
- JS复制图片到剪切板 读取剪切板
- Linux server development, redis source code storage principle and data model
- JSON data flattening pd json_ normalize
- Thinkcmf6.0安装教程
- Cnopendata list data of Chinese colleges and Universities
- [VHDL parallel statement execution]
- 这5个摸鱼神器太火了!程序员:知道了快删!
- 积分商城管理系统中应包含的四大项
- Summary of redis functions
- 2022 tea master (intermediate) examination questions and mock examination
猜你喜欢
Network learning (II) -- Introduction to socket
QT learning 26 integrated example of layout management
【数字IC验证快速入门】14、SystemVerilog学习之基本语法1(数组、队列、结构体、枚举、字符串...内含实践练习)
Ansible
Niu Mei's mathematical problem --- combinatorial number
LeetCode简单题之字符串中最大的 3 位相同数字
2022 welder (elementary) judgment questions and online simulation examination
buureservewp(2)
JSON data flattening pd json_ normalize
Qt学习27 应用程序中的主窗口
随机推荐
【数字IC验证快速入门】17、SystemVerilog学习之基本语法4(随机化Randomization)
Complex network modeling (I)
Introduction to basic components of wechat applet
dash plotly
Yugu p1020 missile interception (binary search)
Merging binary trees by recursion
复杂网络建模(一)
uniapp 移动端强制更新功能
【数字IC验证快速入门】10、Verilog RTL设计必会的FIFO
Qt学习28 主窗口中的工具栏
【数字IC验证快速入门】15、SystemVerilog学习之基本语法2(操作符、类型转换、循环、Task/Function...内含实践练习)
2022 recurrent training question bank and answers of refrigeration and air conditioning equipment operation
Linux server development, SQL statements, indexes, views, stored procedures, triggers
Find the mode in the binary search tree (use medium order traversal as an ordered array)
[quick start of Digital IC Verification] 17. Basic grammar of SystemVerilog learning 4 (randomization)
game攻防世界逆向
Call pytorch API to complete linear regression
青龙面板-今日头条
基于Pytorch 框架手动完成线性回归
DNS server configuration