无标签
发布日期:
2021-03-28
更新日期:
2021-03-28
文章字数:
140
阅读时长:
1 分
阅读次数:
5713. 字符串中不同整数的数目
https://leetcode-cn.com/problems/number-of-different-integers-in-a-string/
思路: 双指针问题, 匹配数字, 保持字符串, 去除多余的0, 存入hash表, hash表的大小即为不同整数的数目
class Solution {
public:
int numDifferentIntegers(string word) {
unordered_set <string> nums;
for(int i = 0; i < word.size(); ++i){
if(!isdigit(word[i])) continue;
string s;
int j = i;
while(j < word.size() && isdigit(word[j])) s += word[j++];
int k = 0;
while(k<s.size() && s[k] == '0') ++k;
nums.insert(s.substr(k));
i = j;
}
return nums.size();
}
};