首页 > 其他分享 >[LeetCode] 2042. Check if Numbers Are Ascending in a Sentence

[LeetCode] 2042. Check if Numbers Are Ascending in a Sentence

时间:2023-01-03 03:11:06浏览次数:65  
标签:2042 数字 Sentence strictly number Ascending tokens token numbers

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:

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:

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 <= 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.

检查句子中的数字是否递增。

句子是由若干 token 组成的一个列表,token 间用 单个 空格分隔,句子没有前导或尾随空格。每个 token 要么是一个由数字 0-9 组成的不含前导零的 正整数 ,要么是一个由小写英文字母组成的 单词 。
示例,"a puppy has 2 eyes 4 legs" 是一个由 7 个 token 组成的句子:"2" 和 "4" 是数字,其他像 "puppy" 这样的 tokens 属于单词。
给你一个表示句子的字符串 s ,你需要检查 s 中的 全部 数字是否从左到右严格递增(即,除了最后一个数字,s 中的 每个 数字都严格小于它 右侧 的数字)。
如果满足题目要求,返回 true ,否则,返回 false 。
来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/check-if-numbers-are-ascending-in-a-sentence
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

思路是把 input 字符串分割成 string array ,然后判断每个单词是否是数字,如果是,是否严格大于之前遇到的数字。

时间O(n)

空间O(n) - string array

Java实现

 1 class Solution {
 2     public boolean areNumbersAscending(String s) {
 3         int prev = -1;
 4         String[] words = s.split(" ");
 5         for (String word : words) {
 6             try {
 7                 int num = Integer.parseInt(word);
 8                 if (num <= prev) {
 9                     return false;
10                 }
11                 prev = num;
12             } catch(Exception e) {
13 
14             }
15         }
16         return true;
17     }
18 }

 

LeetCode 题目总结

标签:2042,数字,Sentence,strictly,number,Ascending,tokens,token,numbers
From: https://www.cnblogs.com/cnoodle/p/17020979.html

相关文章