当前位置:网站首页>The largest 3 same digits in the string of leetcode simple question

The largest 3 same digits in the string of leetcode simple question

2022-07-07 08:09:00 ·Starry Sea

subject

Give you a string num , Represents a large integer . If an integer satisfies all of the following conditions , The integer is considered to be a High quality integer :

The integer is num One length of the is 3 Of Substring .
The integer is repeated by a unique number 3 Secondary composition .
Returns... As a string The largest good integer . If there is no integer that meets the requirements , Then return an empty string “” .

Be careful :

Substring Is a sequence of consecutive characters in a string .
num Or high-quality integers may exist Leading zero .

Example 1:

Input :num = “6777133339”
Output :“777”
explain :num There are two high-quality integers in :“777” and “333” .
“777” It's the biggest one , So back “777” .
Example 2:

Input :num = “2300019”
Output :“000”
explain :“000” Is the only good integer .
Example 3:

Input :num = “42352338”
Output :“”
explain : There is no length of 3 An integer consisting of only one unique number . therefore , There are no good integers .

Tips :

3 <= num.length <= 1000
num Just numbers (0 - 9) form

source : Power button (LeetCode)

Their thinking

   This problem only needs to be done with a length of 3 And then traverse the entire string , Find the largest number that meets the conditions .

class Solution:
    def largestGoodInteger(self, num: str) -> str:
        MIN=''
        for i in range(0,len(num)-2):
            if num[i]==num[i+1] and num[i+1]==num[i+2]:
                if MIN=='':
                    MIN=num[i]*3
                else:
                    MIN=num[i]*3 if int(num[i]*3)>int(MIN) else MIN
        return MIN

 Insert picture description here

原网站

版权声明
本文为[·Starry Sea]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/188/202207070446576551.html