# 1805.字符串中不同整数的数目

## 问题描述
[1805.字符串中不同整数的数目](https://leetcode.cn/problems/number-of-different-integers-in-a-string/)

## 解题思路
把数字当作字符串处理，存入`unordered_set`(哈希表)中，注意最后一个字符是数字的情况。

## 代码
```cpp
class Solution {
public:
    int numDifferentIntegers(string word) {
        unordered_set<string> words;
        string str;
        for (int i = 0; i < word.size(); i++) {
            if (str.empty()) {
                if (word[i] - '0' <= 9)
                    str.push_back(word[i]);
            } else {
                if (word[i] - '0' > 9) {
                    if (words.find(str) == words.end())
                        words.insert(str);
                    str.clear();
                } else {
                    if (str.size() == 1 && str[0] == '0') { // 去除先导0
                        str.clear();
                    }
                    str.push_back(word[i]);
                    }

            }
        }
        if (!str.empty() && words.find(str) == words.end())
            words.insert(str);
        return words.size();
    }
};
```


