当前位置:网站首页>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();
}
};
边栏推荐
- 今日直播 | “人玑协同 未来已来”2022弘玑生态伙伴大会蓄势待发
- Meilu biological IPO was terminated: the annual revenue was 385million, and Chen Lin was the actual controller
- The slave i/o thread stops because master and slave have equal MySQL serv
- Low CPU load and high loadavg processing method
- map的使用(列表的数据赋值到表单,json逗号隔开显示赋值)
- The dplyr package of R language performs data grouping aggregation statistical transformations and calculates the grouping mean of dataframe data
- 主从搭建报错:The slave I/O thread stops because master and slave have equal MySQL serv
- Take a look at how cabloyjs workflow engine implements activiti boundary events
- Zero foundation entry polardb-x: build a highly available system and link the big data screen
- How to type multiple spaces when editing CSDN articles
猜你喜欢

反射及在运用过程中出现的IllegalAccessException异常

C language daily practice - day 22: Zero foundation learning dynamic planning
![打家劫舍III[后序遍历与回溯+动态规划]](/img/9b/e9eeed138e46afdeed340bf2629ee1.png)
打家劫舍III[后序遍历与回溯+动态规划]

Mysql Information Schema 学习(二)--Innodb表

CPU负载很低,loadavg很高处理方法

Documents to be used in IC design process

中缀表达式转后缀表达式详细思路及代码实现
时钟轮在 RPC 中的应用

Zero foundation entry polardb-x: build a highly available system and link the big data screen

Intelligent supply chain management system solution for hardware and electromechanical industry: digital intelligent supply chain "creates new blood" for traditional industries
随机推荐
Benefit a lot, Android interview questions
R language uses rchisq function to generate random numbers that conform to Chi square distribution, and uses plot function to visualize random numbers that conform to Chi square distribution
Php+redis realizes the function of canceling orders over time
R语言ggplot2可视化:使用ggpubr包的ggviolin函数可视化小提琴图
LeetCode_双指针_中等_61. 旋转链表
主从搭建报错:The slave I/O thread stops because master and slave have equal MySQL serv
A full set of teaching materials, real questions of Android interview of 7 major manufacturers including Alibaba Kwai pinduoduo
R语言ggplot2可视化时间序列柱形图:通过双色渐变配色颜色主题可视化时间序列柱形图
零基础入门PolarDB-X:搭建高可用系统并联动数据大屏
swagger2报错Illegal DefaultValue null for parameter type integer
Live broadcast today | the 2022 Hongji ecological partnership conference of "Renji collaboration has come" is ready to go
R语言ggplot2可视化:使用ggpubr包的ggdotplot函数可视化点阵图(dot plot)、设置palette参数设置不同水平点阵图数据点和箱图的颜色
五金机电行业智能供应链管理系统解决方案:数智化供应链为传统产业“造新血”
How to do smoke test
RT-Thread 组件 FinSH 使用时遇到的问题
Tensorflow and torch code verify whether CUDA is successfully installed
CPU负载很低,loadavg很高处理方法
Pychrm Community Edition calls matplotlib pyplot. Solution of imshow() function image not popping up
受益匪浅,安卓面试问题
R语言ggplot2可视化:使用ggpubr包的ggstripchart函数可视化分组点状条带图(dot strip plot)、设置add参数为不同水平点状条带图添加箱图