当前位置:网站首页><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)))边栏推荐
- Various uses of vim are very practical. I learned and summarized them in my work
- Reasons for the failure of web side automation test
- UP Meta—Web3.0世界创新型元宇宙金融协议
- [data clustering] realize data clustering analysis based on multiverse optimization DBSCAN with matlab code
- . Net Maui performance improvement
- R语言使用magick包的image_mosaic函数和image_flatten函数把多张图片堆叠在一起形成堆叠组合图像(Stack layers on top of each other)
- STM32 entry development write DS18B20 temperature sensor driver (read ambient temperature, support cascade)
- [Yugong series] go teaching course 005 variables in July 2022
- 软件内部的定时炸弹:0-Day Log4Shell只是冰山一角
- Le Cluster kubernets en cours d'exécution veut ajuster l'adresse du segment réseau du pod
猜你喜欢

How to write test cases for test coupons?

powershell cs-UTF-16LE编码上线

竟然有一半的人不知道 for 与 foreach 的区别???

总结了200道经典的机器学习面试题(附参考答案)

Suggestions on one-stop development of testing life
![[neural network] convolutional neural network CNN [including Matlab source code 1932]](/img/65/cf9d0a3f46a581dc8f28de2e28779d.png)
[neural network] convolutional neural network CNN [including Matlab source code 1932]
![[full stack plan - programming language C] basic introductory knowledge](/img/6d/555ac6b80b015e0cdfb7ef0a234f9d.png)
[full stack plan - programming language C] basic introductory knowledge

Poor math students who once dropped out of school won the fields award this year

MATLAB實現Huffman編碼譯碼含GUI界面

Reasons for the failure of web side automation test
随机推荐
【数据聚类】基于多元宇宙优化DBSCAN实现数据聚类分析附matlab代码
R语言使用quantile函数计算评分值的分位数(20%、40%、60%、80%)、使用逻辑操作符将对应的分位区间(quantile)编码为分类值生成新的字段、strsplit函数将学生的名和姓拆分
软件内部的定时炸弹:0-Day Log4Shell只是冰山一角
Common SQL statement collation: MySQL
人大金仓受邀参加《航天七〇六“我与航天电脑有约”全国合作伙伴大会》
STM32 entry development NEC infrared protocol decoding (ultra low cost wireless transmission scheme)
R language uses image of magick package_ Mosaic functions and images_ The flatten function stacks multiple pictures together to form a stack layers on top of each other
一起探索云服务之云数据库
The annual salary of general test is 15W, and the annual salary of test and development is 30w+. What is the difference between the two?
Talk about SOC startup (VI) uboot startup process II
Le Cluster kubernets en cours d'exécution veut ajuster l'adresse du segment réseau du pod
MySQL安装常见报错处理大全
Cmu15445 (fall 2019) project 2 - hash table details
Suggestions on one-stop development of testing life
UP Meta—Web3.0世界创新型元宇宙金融协议
Flet教程之 15 GridView 基础入门(教程含源码)
Explore cloud database of cloud services together
STM32F1与STM32CubeIDE编程实例-315M超再生无线遥控模块驱动
Swiftui swift internal skill: five skills of using opaque type in swift
【滤波跟踪】基于matlab捷联惯导仿真【含Matlab源码 1935期】