当前位置:网站首页><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
- La voie du succès de la R & D des entreprises Internet à l’échelle des milliers de personnes
- Camera calibration (1): basic principles of monocular camera calibration and Zhang Zhengyou calibration
- MySQL安装常见报错处理大全
- NPC Jincang was invited to participate in the "aerospace 706" I have an appointment with aerospace computer "national Partner Conference
- Camera calibration (2): summary of monocular camera calibration
- 总结了200道经典的机器学习面试题(附参考答案)
- 竟然有一半的人不知道 for 与 foreach 的区别???
- 【紋理特征提取】基於matlab局部二值模式LBP圖像紋理特征提取【含Matlab源碼 1931期】
- STM32 entry development NEC infrared protocol decoding (ultra low cost wireless transmission scheme)
猜你喜欢
【数据聚类】基于多元宇宙优化DBSCAN实现数据聚类分析附matlab代码
总结了200道经典的机器学习面试题(附参考答案)
[texture feature extraction] LBP image texture feature extraction based on MATLAB local binary mode [including Matlab source code 1931]
Flet教程之 15 GridView 基础入门(教程含源码)
从工具升级为解决方案,有赞的新站位指向新价值
MySQL安装常见报错处理大全
Swiftui tutorial how to realize automatic scrolling function in 2 seconds
【全栈计划 —— 编程语言之C#】基础入门知识一文懂
MATLAB实现Huffman编码译码含GUI界面
SwiftUI Swift 内功之如何在 Swift 中进行自动三角函数计算
随机推荐
Camera calibration (2): summary of monocular camera calibration
Two week selection of tdengine community issues | phase II
Superscalar processor design yaoyongbin Chapter 8 instruction emission excerpt
Use references
Talk about SOC startup (11) kernel initialization
Fleet tutorial 14 basic introduction to listtile (tutorial includes source code)
What is cloud computing?
【滤波跟踪】捷联惯导纯惯导解算matlab实现
108.网络安全渗透测试—[权限提升篇6]—[Windows内核溢出提权]
About how to install mysql8.0 on the cloud server (Tencent cloud here) and enable local remote connection
SwiftUI Swift 内功之如何在 Swift 中进行自动三角函数计算
正在运行的Kubernetes集群想要调整Pod的网段地址
Software design - "high cohesion and low coupling"
The Oracle message permission under the local Navicat connection liunx is insufficient
Tsinghua Yaoban programmers, online marriage was scolded?
HCIA复习整理
Programming examples of stm32f1 and stm32subeide -315m super regenerative wireless remote control module drive
[encapsulation of time format tool functions]
The road to success in R & D efficiency of 1000 person Internet companies
108. Network security penetration test - [privilege escalation 6] - [windows kernel overflow privilege escalation]