{"id":8805,"date":"2021-11-26T21:13:03","date_gmt":"2021-11-27T05:13:03","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=8805"},"modified":"2021-11-26T21:13:26","modified_gmt":"2021-11-27T05:13:26","slug":"leetcode-65-valid-number","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/string\/leetcode-65-valid-number\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 65. Valid Number"},"content":{"rendered":"\n<p>A&nbsp;<strong>valid number<\/strong>&nbsp;can be split up into these components (in order):<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>A&nbsp;<strong>decimal number<\/strong>&nbsp;or an&nbsp;<strong>integer<\/strong>.<\/li><li>(Optional) An&nbsp;<code>'e'<\/code>&nbsp;or&nbsp;<code>'E'<\/code>, followed by an&nbsp;<strong>integer<\/strong>.<\/li><\/ol>\n\n\n\n<p>A&nbsp;<strong>decimal number<\/strong>&nbsp;can be split up into these components (in order):<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>(Optional) A sign character (either&nbsp;<code>'+'<\/code>&nbsp;or&nbsp;<code>'-'<\/code>).<\/li><li>One of the following formats:<ol><li>One or more digits, followed by a dot&nbsp;<code>'.'<\/code>.<\/li><li>One or more digits, followed by a dot&nbsp;<code>'.'<\/code>, followed by one or more digits.<\/li><li>A dot&nbsp;<code>'.'<\/code>, followed by one or more digits.<\/li><\/ol><\/li><\/ol>\n\n\n\n<p>An&nbsp;<strong>integer<\/strong>&nbsp;can be split up into these components (in order):<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>(Optional) A sign character (either&nbsp;<code>'+'<\/code>&nbsp;or&nbsp;<code>'-'<\/code>).<\/li><li>One or more digits.<\/li><\/ol>\n\n\n\n<p>For example, all the following are valid numbers:&nbsp;<code>[\"2\", \"0089\", \"-0.1\", \"+3.14\", \"4.\", \"-.9\", \"2e10\", \"-90E3\", \"3e+7\", \"+6e-1\", \"53.5e93\", \"-123.456e789\"]<\/code>, while the following are not valid numbers:&nbsp;<code>[\"abc\", \"1a\", \"1e\", \"e3\", \"99e2.5\", \"--6\", \"-+3\", \"95a54e53\"]<\/code>.<\/p>\n\n\n\n<p>Given a string&nbsp;<code>s<\/code>, return&nbsp;<code>true<\/code><em>&nbsp;if&nbsp;<\/em><code>s<\/code><em>&nbsp;is a&nbsp;<strong>valid number<\/strong><\/em>.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> s = \"0\"\n<strong>Output:<\/strong> true\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 = \"e\"\n<strong>Output:<\/strong> false\n<\/pre>\n\n\n\n<p><strong>Example 3:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> s = \".\"\n<strong>Output:<\/strong> false\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 = \".1\"\n<strong>Output:<\/strong> true\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>1 &lt;= s.length &lt;= 20<\/code><\/li><li><code>s<\/code>&nbsp;consists of only English letters (both uppercase and lowercase), digits (<code>0-9<\/code>), plus&nbsp;<code>'+'<\/code>, minus&nbsp;<code>'-'<\/code>, or dot&nbsp;<code>'.'<\/code>.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Rule checking<\/strong><\/h2>\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 isNumber(string s) {\n    for (int i = 0; i < s.length(); ++i)\n      if (s[i] != ' ') {\n        s = s.substr(i);\n        break;\n      }\n    while (!s.empty() &#038;&#038; s.back() == ' ') s.pop_back();\n    \n    bool dot = false;\n    bool e = false;\n    bool digit = false;\n    for (int i = 0; i < s.length(); ++i) {\n      s[i] = tolower(s[i]);\n      if (isdigit(s[i])) {\n        digit = true;\n      } else if (s[i] == '.') {\n        if (e || dot) return false;\n        dot = true;\n      } else if (s[i] == 'e') {\n        if (e || !digit) return false;\n        e = true;\n        digit = false;\n      } else if (s[i] == '-' || s[i] == '+') {\n        if (i != 0 &#038;&#038; s[i - 1] != 'e')\n          return false;\n      } else {\n        return false;\n      }\n    }\n    return digit;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>A&nbsp;valid number&nbsp;can be split up into these components (in order): A&nbsp;decimal number&nbsp;or an&nbsp;integer. (Optional) An&nbsp;&#8216;e&#8217;&nbsp;or&nbsp;&#8216;E&#8217;, followed by an&nbsp;integer. A&nbsp;decimal number&nbsp;can be split up into these&#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":[157,217,4],"class_list":["post-8805","post","type-post","status-publish","format-standard","hentry","category-string","tag-digit","tag-hard","tag-string","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8805","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=8805"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8805\/revisions"}],"predecessor-version":[{"id":8807,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8805\/revisions\/8807"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=8805"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=8805"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=8805"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}