当前位置:网站首页>1805. Number of different integers in the string

1805. Number of different integers in the string

2022-07-06 19:37:00 Drag is me

leetcode Force button to brush questions and punch in

subject :1805. The number of different integers in a string
describe : Give you a string word , The string consists of numbers and lowercase letters .

Please replace each character that is not a number with a space . for example ,“a123bc34d8ef34” Will become " 123 34 8 34" . Be careful , The remaining integers are ( Adjacent to each other with at least one space ):“123”、“34”、“8” and “34” .

Return to right word Formed after replacement Different The number of integers .

Only if two integers Without leading zeros The decimal representation of is different , I think these two integers are also different .

Their thinking

1、 Although the title requires that the number of integers be returned , But there is no need to convert strings into integers , Only need statistics to be significant figures ( Don't to 0 The whole number at the beginning ) The number of strings is good ;
2、 Number of different integers , Then set duplicate removal .

Source code ##

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();
    }
};
原网站

版权声明
本文为[Drag is me]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/187/202207061132213252.html