当前位置:网站首页><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)))
边栏推荐
- R language Visual facet chart, hypothesis test, multivariable grouping t-test, visual multivariable grouping faceting boxplot, and add significance levels and jitter points
- Swiftui swift internal skill: five skills of using opaque type in swift
- Zhou Yajin, a top safety scholar of Zhejiang University, is a curiosity driven activist
- Flet教程之 17 Card卡片组件 基础入门(教程含源码)
- La voie du succès de la R & D des entreprises Internet à l’échelle des milliers de personnes
- STM32 entry development uses IIC hardware timing to read and write AT24C08 (EEPROM)
- Steps of redis installation and self startup configuration under CentOS system
- 一度辍学的数学差生,获得今年菲尔兹奖
- SwiftUI Swift 内功之 Swift 中使用不透明类型的 5 个技巧
- 【纹理特征提取】基于matlab局部二值模式LBP图像纹理特征提取【含Matlab源码 1931期】
猜你喜欢
What is cloud computing?
112.网络安全渗透测试—[权限提升篇10]—[Windows 2003 LPK.DDL劫持提权&msf本地提权]
Excel公式知多少?
How much do you know about excel formula?
Nuclear boat (I): when "male mothers" come into reality, can the biotechnology revolution liberate women?
Swiftui tutorial how to realize automatic scrolling function in 2 seconds
Superscalar processor design yaoyongbin Chapter 8 instruction emission excerpt
【滤波跟踪】捷联惯导纯惯导解算matlab实现
Flet教程之 19 VerticalDivider 分隔符组件 基础入门(教程含源码)
Some opinions and code implementation of Siou loss: more powerful learning for bounding box regression zhora gevorgyan
随机推荐
Easyui学习整理笔记
STM32 entry development write DS18B20 temperature sensor driver (read ambient temperature, support cascade)
Unity中SmoothStep介绍和应用: 溶解特效优化
How to write test cases for test coupons?
R language uses the quantile function to calculate the quantile of the score value (20%, 40%, 60%, 80%), uses the logical operator to encode the corresponding quantile interval (quantile) into the cla
How much do you know about excel formula?
【纹理特征提取】基于matlab局部二值模式LBP图像纹理特征提取【含Matlab源码 1931期】
R语言可视化分面图、假设检验、多变量分组t检验、可视化多变量分组分面箱图(faceting boxplot)并添加显著性水平、添加抖动数据点(jitter points)
How to connect 5V serial port to 3.3V MCU serial port?
HCIA复习整理
EasyUI learn to organize notes
sink 消费 到 MySQL, 数据库表里面已经设置了 自增主键, flink 里面,如何 操作?
What is cloud computing?
Camera calibration (1): basic principles of monocular camera calibration and Zhang Zhengyou calibration
In depth learning autumn recruitment interview questions collection (1)
Technology sharing | packet capturing analysis TCP protocol
Neural approvals to conversational AI (1)
Flet教程之 19 VerticalDivider 分隔符组件 基础入门(教程含源码)
软件内部的定时炸弹:0-Day Log4Shell只是冰山一角
Tsinghua Yaoban programmers, online marriage was scolded?