当前位置:网站首页>1805. 字符串中不同整数的数目
1805. 字符串中不同整数的数目
2022-07-06 11:32:00 【拽拽就是我】
leetcode力扣刷题打卡
题目:1805. 字符串中不同整数的数目
描述:给你一个字符串 word ,该字符串由数字和小写英文字母组成。
请你用空格替换每个不是数字的字符。例如,“a123bc34d8ef34” 将会变成 " 123 34 8 34" 。注意,剩下的这些整数为(相邻彼此至少有一个空格隔开):“123”、“34”、“8” 和 “34” 。
返回对 word 完成替换后形成的 不同 整数的数目。
只有当两个整数的 不含前导零 的十进制表示不同, 才认为这两个整数也不同。
解题思路
1、虽然题目要求返回整数的数目,但其实没必要把字符串转换成整数,只需要统计是有效数字(不以0开头的整数)的字符串的数目就好;
2、不同整数的数目,那就要set去重。
原代码##
class Solution {
public:
int numDifferentIntegers(string word) {
set<string>set;
int flag = 0;
string temp = "";
for (int i = 0; i < word.size(); ++i) {
if (word[i] >= '0' && word[i] <= '9') {
flag = 1;
if (temp == "" && word[i] == '0') continue;
temp += word[i];
} else {
if (flag) {
flag = 0;
set.insert(temp);
temp = "";
}
}
}
if (flag) {
set.insert(temp);
}
return set.size();
}
};
边栏推荐
- 黑馬--Redis篇
- Computer network: sorting out common network interview questions (I)
- C # - realize serialization with Marshall class
- How to type multiple spaces when editing CSDN articles
- 今日直播 | “人玑协同 未来已来”2022弘玑生态伙伴大会蓄势待发
- 【翻译】数字内幕。KubeCon + CloudNativeCon在2022年欧洲的选择过程
- MRO industrial products enterprise procurement system: how to refine procurement collaborative management? Industrial products enterprises that want to upgrade must see!
- 10 schemes to ensure interface data security
- The dplyr package of R language performs data grouping aggregation statistical transformations and calculates the grouping mean of dataframe data
- JDBC details
猜你喜欢

系统性详解Redis操作Hash类型数据(带源码分析及测试结果)

How to do smoke test

Detailed idea and code implementation of infix expression to suffix expression

Mysql Information Schema 学习(一)--通用表

Yutai micro rushes to the scientific innovation board: Huawei and Xiaomi fund are shareholders to raise 1.3 billion

LeetCode_双指针_中等_61. 旋转链表
![[translation] a GPU approach to particle physics](/img/07/57036c925155cab36678c696e89440.jpg)
[translation] a GPU approach to particle physics

Reflection and illegalaccessexception exception during application

Spark foundation -scala

CPU负载很低,loadavg很高处理方法
随机推荐
【翻译】Linkerd在欧洲和北美的采用率超过了Istio,2021年增长118%。
ROS custom message publishing subscription example
A popular explanation will help you get started
如何自定义动漫头像?这6个免费精品在线卡通头像生成器,看一眼就怦然心动!
【翻译】云原生观察能力微调查。普罗米修斯引领潮流,但要了解系统的健康状况仍有障碍...
保证接口数据安全的10种方案
全套教学资料,阿里快手拼多多等7家大厂Android面试真题
Don't miss this underestimated movie because of controversy!
谷粒商城--分布式高级篇P129~P339(完结)
About image reading and processing, etc
Take a look at how cabloyjs workflow engine implements activiti boundary events
Fast power template for inverse element, the role of inverse element and example [the 20th summer competition of Shanghai University Programming League] permutation counting
GCC [7] - compilation checks the declaration of functions, and link checks the definition bugs of functions
Looting iii[post sequence traversal and backtracking + dynamic planning]
Translation D28 (with AC code POJ 26:the nearest number)
Modulenotfounderror: no module named 'PIL' solution
思维导图+源代码+笔记+项目,字节跳动+京东+360+网易面试题整理
Solution of commercial supply chain management platform for packaging industry: layout smart supply system and digitally integrate the supply chain of packaging industry
Tongyu Xincai rushes to Shenzhen Stock Exchange: the annual revenue is 947million Zhang Chi and Su Shiguo are the actual controllers
【翻译】数字内幕。KubeCon + CloudNativeCon在2022年欧洲的选择过程