{"id":8604,"date":"2021-10-17T20:33:51","date_gmt":"2021-10-18T03:33:51","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=8604"},"modified":"2021-10-17T20:34:55","modified_gmt":"2021-10-18T03:34:55","slug":"leetcode-2042-check-if-numbers-are-ascending-in-a-sentence","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/string\/leetcode-2042-check-if-numbers-are-ascending-in-a-sentence\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 2042. Check if Numbers Are Ascending in a Sentence"},"content":{"rendered":"\n<p>A sentence is a list of&nbsp;<strong>tokens<\/strong>&nbsp;separated by a&nbsp;<strong>single<\/strong>&nbsp;space with no leading or trailing spaces. Every token is either a&nbsp;<strong>positive number<\/strong>&nbsp;consisting of digits&nbsp;<code>0-9<\/code>&nbsp;with no leading zeros, or a&nbsp;<strong>word<\/strong>&nbsp;consisting of lowercase English letters.<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>For example,&nbsp;<code>\"a puppy has 2 eyes 4 legs\"<\/code>&nbsp;is a sentence with seven tokens:&nbsp;<code>\"2\"<\/code>&nbsp;and&nbsp;<code>\"4\"<\/code>&nbsp;are numbers and the other tokens such as&nbsp;<code>\"puppy\"<\/code>&nbsp;are words.<\/li><\/ul>\n\n\n\n<p>Given a string&nbsp;<code>s<\/code>&nbsp;representing a sentence, you need to check if&nbsp;<strong>all<\/strong>&nbsp;the numbers in&nbsp;<code>s<\/code>&nbsp;are&nbsp;<strong>strictly increasing<\/strong>&nbsp;from left to right (i.e., other than the last number,&nbsp;<strong>each<\/strong>&nbsp;number is&nbsp;<strong>strictly smaller<\/strong>&nbsp;than the number on its&nbsp;<strong>right<\/strong>&nbsp;in&nbsp;<code>s<\/code>).<\/p>\n\n\n\n<p>Return&nbsp;<code>true<\/code><em>&nbsp;if so, or&nbsp;<\/em><code>false<\/code><em>&nbsp;otherwise<\/em>.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2021\/09\/30\/example1.png\" alt=\"example-1\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted:crayon:false\"><strong>Input:<\/strong> s = \"1 box has 3 blue 4 red 6 green and 12 yellow marbles\"\n<strong>Output:<\/strong> true\n<strong>Explanation:<\/strong> The numbers in s are: 1, 3, 4, 6, 12.\nThey are strictly increasing from left to right: 1 &lt; 3 &lt; 4 &lt; 6 &lt; 12.\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted:crayon:false\"><strong>Input:<\/strong> s = \"hello world 5 x 5\"\n<strong>Output:<\/strong> false\n<strong>Explanation:<\/strong> The numbers in s are: <strong>5<\/strong>, <strong><u>5<\/u><\/strong>. They are not strictly increasing.\n<\/pre>\n\n\n\n<p><strong>Example 3:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2021\/09\/30\/example3.png\" alt=\"example-3\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted:crayon:false\"><strong>Input:<\/strong> s = \"sunset is at 7 51 pm overnight lows will be in the low 50 and 60 s\"\n<strong>Output:<\/strong> false\n<strong>Explanation:<\/strong> The numbers in s are: 7, <strong>51<\/strong>, <strong>50<\/strong>, 60. They are not strictly increasing.\n<\/pre>\n\n\n\n<p><strong>Example 4:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted:crayon:false\"><strong>Input:<\/strong> s = \"4 5 11 26\"\n<strong>Output:<\/strong> true\n<strong>Explanation:<\/strong> The numbers in s are: 4, 5, 11, 26.\nThey are strictly increasing from left to right: 4 &lt; 5 &lt; 11 &lt; 26.\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>3 &lt;= s.length &lt;= 200<\/code><\/li><li><code>s<\/code>&nbsp;consists of lowercase English letters, spaces, and digits from&nbsp;<code>0<\/code>&nbsp;to&nbsp;<code>9<\/code>, inclusive.<\/li><li>The number of tokens in&nbsp;<code>s<\/code>&nbsp;is between&nbsp;<code>2<\/code>&nbsp;and&nbsp;<code>100<\/code>, inclusive.<\/li><li>The tokens in&nbsp;<code>s<\/code>&nbsp;are separated by a single space.<\/li><li>There are at least&nbsp;<strong>two<\/strong>&nbsp;numbers in&nbsp;<code>s<\/code>.<\/li><li>Each number in&nbsp;<code>s<\/code>&nbsp;is a&nbsp;<strong>positive<\/strong>&nbsp;number&nbsp;<strong>less<\/strong>&nbsp;than&nbsp;<code>100<\/code>, with no leading zeros.<\/li><li><code>s<\/code>&nbsp;contains no leading or trailing spaces.<\/li><\/ul>\n\n\n\n<p>Solution: String<\/p>\n\n\n\n<p>Time complexity: O(n)<br>Space complexity: O(1)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  bool areNumbersAscending(string s) {\n    stringstream ss(s);\n    string token;\n    int last = -1;\n    while (ss >> token) {\n      if (isdigit(token[0])) {\n        int num = stoi(token);\n        if (num <= last) return false;\n        last = num;\n      }\n    }\n    return true;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>A sentence is a list of&nbsp;tokens&nbsp;separated by a&nbsp;single&nbsp;space with no leading or trailing spaces. Every token is either a&nbsp;positive number&nbsp;consisting of digits&nbsp;0-9&nbsp;with no leading zeros,&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[47],"tags":[222,276,4],"class_list":["post-8604","post","type-post","status-publish","format-standard","hentry","category-string","tag-easy","tag-split","tag-string","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8604","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=8604"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8604\/revisions"}],"predecessor-version":[{"id":8606,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8604\/revisions\/8606"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=8604"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=8604"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=8604"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}