当前位置:网站首页><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)))边栏推荐
- [encapsulation of time format tool functions]
- 【纹理特征提取】基于matlab局部二值模式LBP图像纹理特征提取【含Matlab源码 1931期】
- Present pod information to the container through environment variables
- NPC Jincang was invited to participate in the "aerospace 706" I have an appointment with aerospace computer "national Partner Conference
- Software design - "high cohesion and low coupling"
- powershell cs-UTF-16LE编码上线
- Fleet tutorial 15 introduction to GridView Basics (tutorial includes source code)
- 人大金仓受邀参加《航天七〇六“我与航天电脑有约”全国合作伙伴大会》
- Mise en œuvre du codage Huffman et du décodage avec interface graphique par MATLAB
- Camera calibration (1): basic principles of monocular camera calibration and Zhang Zhengyou calibration
猜你喜欢

Superscalar processor design yaoyongbin Chapter 9 instruction execution excerpt

Reasons for the failure of web side automation test

Mise en œuvre du codage Huffman et du décodage avec interface graphique par MATLAB

What is cloud computing?

Unity中SmoothStep介绍和应用: 溶解特效优化

Stm32f1 and stm32subeide programming example -max7219 drives 8-bit 7-segment nixie tube (based on SPI)

Talk about SOC startup (x) kernel startup pilot knowledge

Design intelligent weighing system based on Huawei cloud IOT (STM32)

STM32F1与STM32CubeIDE编程实例-315M超再生无线遥控模块驱动

请查收.NET MAUI 的最新学习资源
随机推荐
【滤波跟踪】捷联惯导纯惯导解算matlab实现
R語言使用magick包的image_mosaic函數和image_flatten函數把多張圖片堆疊在一起形成堆疊組合圖像(Stack layers on top of each other)
Various uses of vim are very practical. I learned and summarized them in my work
18 basic introduction to divider separator component of fleet tutorial (tutorial includes source code)
【系统设计】指标监控和告警系统
SwiftUI Swift 内功之如何在 Swift 中进行自动三角函数计算
C#中在路径前加@的作用
Flet教程之 15 GridView 基础入门(教程含源码)
【最短路】Acwing1128信使:floyd最短路
Common SQL statement collation: MySQL
112. Network security penetration test - [privilege promotion article 10] - [Windows 2003 lpk.ddl hijacking rights lifting & MSF local rights lifting]
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
The Oracle message permission under the local Navicat connection liunx is insufficient
请查收.NET MAUI 的最新学习资源
Zhou Yajin, a top safety scholar of Zhejiang University, is a curiosity driven activist
千人规模互联网公司研发效能成功之路
深度学习秋招面试题集锦(一)
相机标定(1): 单目相机标定及张正友标定基本原理
Automated testing framework
The function of adding @ before the path in C #