当前位置:网站首页><No. 9> 1805. Number of different integers in the string (simple)

<No. 9> 1805. Number of different integers in the string (simple)

2022-07-07 12:07:00 Smoked Luoting purple Pavilion

Catalog

Title source :

Title Description :

 Python Realization :

Python In one line :


Title source :

Click Here

Title Description :

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 .

 Python Realization :

class Solution(object):
    def numDifferentIntegers(self, word):
        """
        :type word: str
        :rtype: int
        """
        rm_word = re.findall("\d+", word) # "\d+": Match the Numbers , Can be continuous 
        rm_word2 = [a.lstrip('0') for a in rm_word] # Use lstrip() Function removes the leading 0
        set_word = set(rm_word2) #set The assembly can automatically de weight 
        # list_word = list(set_word)  This line can have , But there is no , It doesn't affect the result  return len(list_word)
        return len(set_word)



Python In one line :

class Solution(object):
    def numDifferentIntegers(self, word):
        """
        :type word: str
        :rtype: int
        """


        return len(set(x.lstrip('0') for x in re.findall("\d+", word)))

原网站

版权声明
本文为[Smoked Luoting purple Pavilion]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/188/202207071002352897.html