2042. 检查句子中的数字是否递增
2042. 检查句子中的数字是否递增
题目
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, orfalse 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.
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不含前导或尾随空格
解题思路
提取数字:
- 使用正则表达式
/\d{1,2}/ig匹配所有 1 到 2 位的数字。 match方法会返回一个匹配到的数字字符串数组。
- 使用正则表达式
验证递增:
- 遍历提取的数字数组。
- 将每个数字字符串转为数字并比较当前数字和前一个数字。
- 如果当前数字小于或等于前一个数字,返回
false。 - 如果全部数字严格递增,返回
true。
复杂度分析
- 时间复杂度:
O(m)- 正则匹配数字:
O(n),其中n是字符串长度。 - 遍历数字数组:
O(m),其中m是提取的数字个数。 - 总时间复杂度为
O(n)。
- 正则匹配数字:
- 空间复杂度:
O(m),正则匹配产生了一个数组,其中m是提取的数字个数。
代码
/**
* @param {string} s
* @return {boolean}
*/
var areNumbersAscending = function (s) {
let arr = s.match(/\d{1,2}/gi); // 匹配1到2位的数字
for (let i = 1; i < arr.length; i++) {
// 检查递增
if (Number(arr[i]) <= Number(arr[i - 1])) return false;
}
return true;
};
相关题目
| 题号 | 标题 | 题解 | 标签 | 难度 | 力扣 |
|---|---|---|---|---|---|
| 8 | 字符串转换整数 (atoi) | [✓] | 字符串 | 🟠 | 🀄️ 🔗 |
| 1859 | 将句子排序 | [✓] | 字符串 排序 | 🟢 | 🀄️ 🔗 |
| 2124 | 检查是否所有 A 都在 B 之前 | [✓] | 字符串 | 🟢 | 🀄️ 🔗 |
