当前位置:网站首页>Leetcode simple question: check whether the numbers in the sentence are increasing

Leetcode simple question: check whether the numbers in the sentence are increasing

2022-07-06 15:03:00 ·Starry Sea

subject

The sentence consists of several token A list of ,token In between Single The blank space to separate , The sentence has no leading or trailing spaces . Every token Or a number 0-9 Consisting of... Without leading zeros Positive integer , Or it's a small English letter word .
Example ,“a puppy has 2 eyes 4 legs” It's a by 7 individual token Make up sentences :“2” and “4” It's the number. , Other images “puppy” In this way tokens Belongs to the word .
Give you a string that represents a sentence s , You need to check s Medium All Is the number strictly increasing from left to right ( namely , Except for the last number ,s Medium Every Numbers are strictly smaller than it On the right side The number of ).
If you meet the requirements of the topic , return true , otherwise , return false .
Example 1:
 Insert picture description here
Input :s = “1 box has 3 blue 4 red 6 green and 12 yellow marbles”
Output :true
explain : The number in the sentence is :1, 3, 4, 6, 12 .
These numbers are strictly increasing from left to right 1 < 3 < 4 < 6 < 12 .
Example 2:
Input :s = “hello world 5 x 5”
Output :false
explain : The number in the sentence is :5, 5 . These numbers are not strictly incremental .
Example 3:
 Insert picture description here
Input :s = “sunset is at 7 51 pm overnight lows will be in the low 50 and 60 s”
Output :false
explain :s The number in is :7, 51, 50, 60 . These numbers are not strictly incremental .
Example 4:
Input :s = “4 5 11 26”
Output :true
explain :s The number in is :4, 5, 11, 26 .
These numbers are strictly increasing from left to right :4 < 5 < 11 < 26 .
Tips :
3 <= s.length <= 200
s By lowercase letters 、 Spaces and numbers 0 To 9 form ( contain 0 and 9)
s Middle number token The number of is in 2 and 100 Between ( contain 2 and 100)
s Medium token Separated by a single space
s At least there is Two Numbers
s Every number in is a Less than 100 Of just Count , Without leading zeros
s No leading or trailing spaces
source : Power button (LeetCode)

Their thinking

   A simple idea , Find all the numbers in the sentence , Then compare whether it meets the conditions of the topic one by one .

class Solution:
    def areNumbersAscending(self, s: str) -> bool:
        num=re.findall(r'\d+',s)
        for i in range(1,len(num)):
            if int(num[i])<=int(num[i-1]):
                return False
        return True

 Insert picture description here

原网站

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