原文链接: https://leetcode-cn.com/problems/check-if-numbers-are-ascending-in-a-sentence
英文原文
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.
Example 2:
Input: s = "hello world 5 x 5" Output: false Explanation: The numbers in s are: 5, 5. They are not strictly increasing.
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.
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.
Constraints:
3 <= s.length <= 200sconsists of lowercase English letters, spaces, and digits from0to9, inclusive.- The number of tokens in
sis between2and100, inclusive. - The tokens in
sare separated by a single space. - There are at least two numbers in
s. - Each number in
sis a positive number less than100, with no leading zeros. scontains no leading or trailing spaces.
中文题目
句子是由若干 token 组成的一个列表,token 间用 单个 空格分隔,句子没有前导或尾随空格。每个 token 要么是一个由数字 0-9 组成的不含前导零的 正整数 ,要么是一个由小写英文字母组成的 单词 。
- 示例,
"a puppy has 2 eyes 4 legs"是一个由 7 个 token 组成的句子:"2"和"4"是数字,其他像"puppy"这样的 tokens 属于单词。
给你一个表示句子的字符串 s ,你需要检查 s 中的 全部 数字是否从左到右严格递增(即,除了最后一个数字,s 中的 每个 数字都严格小于它 右侧 的数字)。
如果满足题目要求,返回 true ,否则,返回 false 。
示例 1:

输入:s = "1 box has 3 blue 4 red 6 green and 12 yellow marbles" 输出:true 解释:句子中的数字是:1, 3, 4, 6, 12 。 这些数字是按从左到右严格递增的 1 < 3 < 4 < 6 < 12 。
示例 2:
输入:s = "hello world 5 x 5" 输出:false 解释:句子中的数字是:5, 5 。这些数字不是严格递增的。
示例 3:

输入:s = "sunset is at 7 51 pm overnight lows will be in the low 50 and 60 s" 输出:false 解释:s 中的数字是:7, 51, 50, 60 。这些数字不是严格递增的。
示例 4:
输入:s = "4 5 11 26" 输出:true 解释:s 中的数字是:4, 5, 11, 26 。 这些数字是按从左到右严格递增的:4 < 5 < 11 < 26 。
提示:
3 <= s.length <= 200s由小写英文字母、空格和数字0到9组成(包含0和9)s中数字 token 的数目在2和100之间(包含2和100)s中的 token 之间由单个空格分隔s中至少有 两个 数字s中的每个数字都是一个 小于100的 正 数,且不含前导零s不含前导或尾随空格
通过代码
高赞题解
思路:1.利用split方法对字符串进行分割,提取出每个token;2.针对每个token,根据其首位字符的ASCII编码大小与‘a’的比较结果,判断该token是否为数字;3.从左至右,依次比较该数字token和前一个数字的大小,判断tokens内的数字是否严格递增。
判断数字的方法 可以采用2中的方法判断token是否为数字,是由于题目中的条件:每个 token 要么是一个由数字 0-9 组成的不含前导零的 正整数 ,要么是一个由小写英文字母组成的单词。
class Solution {
public boolean areNumbersAscending(String s) {
String[] tokens = s.split(" ");
int leftNum = -1;
for(String token : tokens){
if(token.charAt(0) < 'a'){
if(Integer.parseInt(token) <= leftNum){
return false;
}
leftNum = Integer.parseInt(token);
}
}
return true;
}
}
统计信息
| 通过次数 | 提交次数 | AC比率 |
|---|---|---|
| 7214 | 10823 | 66.7% |
提交历史
| 提交时间 | 提交结果 | 执行时间 | 内存消耗 | 语言 |
|---|