当前位置:网站首页><No. 9> 1805. 字符串中不同整数的数目 (简单)
<No. 9> 1805. 字符串中不同整数的数目 (简单)
2022-07-07 10:02:00 【薰珞婷紫小亭子】
目录
题目来源:
题目描述:
给你一个字符串 word ,该字符串由数字和小写英文字母组成。
请你用空格替换每个不是数字的字符。例如,"a123bc34d8ef34" 将会变成 " 123 34 8 34" 。注意,剩下的这些整数为(相邻彼此至少有一个空格隔开):"123"、"34"、"8" 和 "34" 。
返回对 word 完成替换后形成的 不同 整数的数目。
只有当两个整数的 不含前导零 的十进制表示不同, 才认为这两个整数也不同。

Python 实现:
class Solution(object):
def numDifferentIntegers(self, word):
"""
:type word: str
:rtype: int
"""
rm_word = re.findall("\d+", word) # "\d+":匹配数字,可连续
rm_word2 = [a.lstrip('0') for a in rm_word] #使用lstrip()函数去除前导0
set_word = set(rm_word2) #set集合能够自动去重
# list_word = list(set_word) 这行可有,可无,不影响结果 return len(list_word)
return len(set_word)
Python 一行实现:
class Solution(object):
def numDifferentIntegers(self, word):
"""
:type word: str
:rtype: int
"""
return len(set(x.lstrip('0') for x in re.findall("\d+", word)))边栏推荐
- VIM command mode and input mode switching
- SwiftUI Swift 内功之如何在 Swift 中进行自动三角函数计算
- 110.网络安全渗透测试—[权限提升篇8]—[Windows SqlServer xp_cmdshell存储过程提权]
- Enclosed please find. Net Maui's latest learning resources
- 千人规模互联网公司研发效能成功之路
- Explore cloud database of cloud services together
- 超标量处理器设计 姚永斌 第9章 指令执行 摘录
- Ask about the version of flinkcdc2.2.0, which supports concurrency. Does this concurrency mean Multiple Parallelism? Now I find that mysqlcdc is full
- SwiftUI Swift 内功之 Swift 中使用不透明类型的 5 个技巧
- Flet教程之 16 Tabs 选项卡控件 基础入门(教程含源码)
猜你喜欢

SwiftUI 4 新功能之掌握 WeatherKit 和 Swift Charts
![110.网络安全渗透测试—[权限提升篇8]—[Windows SqlServer xp_cmdshell存储过程提权]](/img/62/1ec8885aaa2d4dca0e764b73a1e2df.png)
110.网络安全渗透测试—[权限提升篇8]—[Windows SqlServer xp_cmdshell存储过程提权]

Time bomb inside the software: 0-day log4shell is just the tip of the iceberg

Camera calibration (1): basic principles of monocular camera calibration and Zhang Zhengyou calibration

Improve application security through nonce field of play integrity API
![[system design] index monitoring and alarm system](/img/8e/9c4c168f7f2b8e1f0786a5fe158544.png)
[system design] index monitoring and alarm system

5V串口接3.3V单片机串口怎么搞?

禁锢自己的因素,原来有这么多

How to write test cases for test coupons?

正在運行的Kubernetes集群想要調整Pod的網段地址
随机推荐
QT | multiple windows share a prompt box class
浙江大学周亚金:“又破又立”的顶尖安全学者,好奇心驱动的行动派
超标量处理器设计 姚永斌 第8章 指令发射 摘录
Unity中SmoothStep介绍和应用: 溶解特效优化
SwiftUI 4 新功能之掌握 WeatherKit 和 Swift Charts
Reasons for the failure of web side automation test
Present pod information to the container through environment variables
Enclosed please find. Net Maui's latest learning resources
[shortest circuit] acwing 1127 Sweet butter (heap optimized dijsktra or SPFA)
108.网络安全渗透测试—[权限提升篇6]—[Windows内核溢出提权]
sql里,我想设置外键,为什么出现这个问题
[neural network] convolutional neural network CNN [including Matlab source code 1932]
人大金仓受邀参加《航天七〇六“我与航天电脑有约”全国合作伙伴大会》
18 basic introduction to divider separator component of fleet tutorial (tutorial includes source code)
Swiftui swift internal skill how to perform automatic trigonometric function calculation in swift
Flet教程之 16 Tabs 选项卡控件 基础入门(教程含源码)
Talk about SOC startup (IX) adding a new board to uboot
The function of adding @ before the path in C #
When sink is consumed in mysql, the self incrementing primary key has been set in the database table. How to operate in Flink?
Flet教程之 14 ListTile 基础入门(教程含源码)