This article has participated in the “Digitalstar Project” and won a creative gift package to challenge the creative incentive money.
describe
A sentence is a list of tokens separated by a single space with no leading or trailing spaces. Every token is either a positive number consisting of digits 0-9 with no leading zeros, or a word consisting of lowercase English letters.
- For example, “a puppy has 2 eyes 4 legs” is a sentence with seven tokens: “2” and “4” are numbers and the other tokens such as “puppy” are words.
Given a string s representing a sentence, you need to check if all the numbers in s are strictly increasing from left to right (i.e., other than the last number, each number is strictly smaller than the number on its right in s).
Return true if so, or false otherwise.
Example 1:
Input: s = "1 box has 3 blue 4 red 6 green and 12 yellow marbles"
Output: true
Explanation: The numbers in s are: 1, 3, 4, 6, 12.
They are strictly increasing from left to right: 1 < 3 < 4 < 6 < 12.
Copy the code
Example 2:
Input: s = "hello world 5 x 5"
Output: false
Explanation: The numbers in s are: 5, 5. They are not strictly increasing.
Copy the code
Example 3:
Input: s = "sunset is at 7 51 pm overnight lows will be in the low 50 and 60 s"
Output: false
Explanation: The numbers in s are: 7, 51, 50, 60. They are not strictly increasing.
Copy the code
Example 4:
Input: s = "4 5 11 26"
Output: true
Explanation: The numbers in s are: 4, 5, 11, 26.
They are strictly increasing from left to right: 4 < 5 < 11 < 26.
Copy the code
Note:
3 <= s.length <= 200
s consists of lowercase English letters, spaces, and digits from 0 to 9, inclusive.
The number of tokens in s is between 2 and 100, inclusive.
The tokens in s are separated by a single space.
There are at least two numbers in s.
Each number in s is a positive number less than 100, with no leading zeros.
s contains no leading or trailing spaces.
Copy the code
parsing
A string sentence s contains a positive integer substring or lowercase word separated by a single space, and s has no pre – or post-space. A puppy has 2 eyes 4 legs. They’re asking us to determine whether all the numbers in the string S are in strict ascending order. Return True if True, False otherwise.
The problem gives the concept of strict ascending order: every number except the last is strictly less than the number to its right in S.
The idea is simple:
- Initialize last maximum digit to 0
- Split s into a list of substrings L using the built-in function s.split(” “)
- It then iterates through the list L, updating last to int(c) if int(c) is greater than last if c is a numeric string, and returning False otherwise
- At the end of the traversal, the numbers are in strict ascending order, and True is returned
answer
class Solution(object):
def areNumbersAscending(self, s):
"""
:type s: str
:rtype: bool
"""
L = s.split(' ')
numbers = '1234567890'
last = 0
for c in L:
if c[0] in numbers:
if int(c)>last:
last = int(c)
else:
return False
return True
Copy the code
The results
Runtime: 10 ms, faster than 100.00% of Python online submissions for Check if Numbers Are Ascending. Memory Usage: The submissions in the Python online list for Check if Numbers Are Ascending.Copy the code
parsing
Built-in functions can also be used to solve the problem quickly, simply using the re function to find all the numbers and then determine if the list of numbers is in ascending order.
answer
class Solution(object):
def areNumbersAscending(self, s):
"""
:type s: str
:rtype: bool
"""
nums = re.findall(r'\d+', s)
return nums == sorted(set(nums), key=int)
Copy the code
The results
Runtime: 10 ms, faster than 100.00% of Python online submissions for Check if Numbers Are Ascending. Memory Usage: The submissions in the Python online list for Check if Numbers Are Ascending.Copy the code
Original link: leetcode.com/problems/ch…
Your support is my biggest motivation