当前位置:网站首页>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();
}
};
边栏推荐
- 保证接口数据安全的10种方案
- usb host 驱动 - UVC 掉包
- Detailed idea and code implementation of infix expression to suffix expression
- Intelligent supply chain management system solution for hardware and electromechanical industry: digital intelligent supply chain "creates new blood" for traditional industries
- short i =1; i=i+1与short i=1; i+=1的区别
- Tensorflow2.0 自定义训练的方式求解函数系数
- Sanmian ant financial successfully got the offer, and has experience in Android development agency recruitment and interview
- 思維導圖+源代碼+筆記+項目,字節跳動+京東+360+網易面試題整理
- Help improve the professional quality of safety talents | the first stage of personal ability certification and assessment has been successfully completed!
- Tensorflow and torch code verify whether CUDA is successfully installed
猜你喜欢
Actf 2022 came to a successful conclusion, and 0ops team won the second consecutive championship!!
How word displays modification traces
Pytorch common loss function
潇洒郎: AttributeError: partially initialized module ‘cv2‘ has no attribute ‘gapi_wip_gst_GStreamerPipe
Reflection and illegalaccessexception exception during application
Looting iii[post sequence traversal and backtracking + dynamic planning]
Low CPU load and high loadavg processing method
Interface test tool - postman
Help improve the professional quality of safety talents | the first stage of personal ability certification and assessment has been successfully completed!
Live broadcast today | the 2022 Hongji ecological partnership conference of "Renji collaboration has come" is ready to go
随机推荐
Characteristic colleges and universities, jointly build Netease Industrial College
安装Mysql报错:Could not create or access the registry key needed for the...
Meilu biological IPO was terminated: the annual revenue was 385million, and Chen Lin was the actual controller
R语言ggplot2可视化:使用ggpubr包的ggdotplot函数可视化点阵图(dot plot)、设置palette参数设置不同水平点阵图数据点和箱图的颜色
Synchronous development of business and application: strategic suggestions for application modernization
The second day of rhcsa study
LeetCode-1279. 红绿灯路口
C # - realize serialization with Marshall class
零基础入门PolarDB-X:搭建高可用系统并联动数据大屏
【翻译】云原生观察能力微调查。普罗米修斯引领潮流,但要了解系统的健康状况仍有障碍...
接雨水问题解析
It's super detailed in history. It's too late for you to read this information if you want to find a job
系统性详解Redis操作Hash类型数据(带源码分析及测试结果)
R语言使用order函数对dataframe数据进行排序、基于单个字段(变量)进行降序排序(DESCENDING)
LeetCode_格雷编码_中等_89.格雷编码
保证接口数据安全的10种方案
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
R语言ggplot2可视化:使用ggpubr包的ggstripchart函数可视化分组点状条带图(dot strip plot)、设置add参数为不同水平点状条带图添加箱图
Pychrm Community Edition calls matplotlib pyplot. Solution of imshow() function image not popping up
[translation] a GPU approach to particle physics