|
| 1 | +<p>Given a string <code>s</code>, return whether <code>s</code> is a <strong>valid number</strong>.<br /> |
| 2 | +<br /> |
| 3 | +For example, all the following are valid numbers: <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: <code>"abc", "1a", "1e", "e3", "99e2.5", "--6", "-+3", "95a54e53"</code>.</p> |
| 4 | + |
| 5 | +<p>Formally, a <strong>valid number</strong> is defined using one of the following definitions:</p> |
| 6 | + |
| 7 | +<ol> |
| 8 | + <li>An <strong>integer number</strong> followed by an <strong>optional exponent</strong>.</li> |
| 9 | + <li>A <strong>decimal number</strong> followed by an <strong>optional exponent</strong>.</li> |
| 10 | +</ol> |
| 11 | + |
| 12 | +<p>An <strong>integer number</strong> is defined with an <strong>optional sign</strong> <code>'-'</code> or <code>'+'</code> followed by <strong>digits</strong>.</p> |
| 13 | + |
| 14 | +<p>A <strong>decimal number</strong> is defined with an <strong>optional sign</strong> <code>'-'</code> or <code>'+'</code> followed by one of the following definitions:</p> |
| 15 | + |
| 16 | +<ol> |
| 17 | + <li><strong>Digits</strong> followed by a <strong>dot</strong> <code>'.'</code>.</li> |
| 18 | + <li><strong>Digits</strong> followed by a <strong>dot</strong> <code>'.'</code> followed by <strong>digits</strong>.</li> |
| 19 | + <li>A <strong>dot</strong> <code>'.'</code> followed by <strong>digits</strong>.</li> |
| 20 | +</ol> |
| 21 | + |
| 22 | +<p>An <strong>exponent</strong> is defined with an <strong>exponent notation</strong> <code>'e'</code> or <code>'E'</code> followed by an <strong>integer number</strong>.</p> |
| 23 | + |
| 24 | +<p>The <strong>digits</strong> are defined as one or more digits.</p> |
| 25 | + |
| 26 | +<p> </p> |
| 27 | +<p><strong class="example">Example 1:</strong></p> |
| 28 | + |
| 29 | +<div class="example-block"> |
| 30 | +<p><strong>Input:</strong> <span class="example-io">s = "0"</span></p> |
| 31 | + |
| 32 | +<p><strong>Output:</strong> <span class="example-io">true</span></p> |
| 33 | +</div> |
| 34 | + |
| 35 | +<p><strong class="example">Example 2:</strong></p> |
| 36 | + |
| 37 | +<div class="example-block"> |
| 38 | +<p><strong>Input:</strong> <span class="example-io">s = "e"</span></p> |
| 39 | + |
| 40 | +<p><strong>Output:</strong> <span class="example-io">false</span></p> |
| 41 | +</div> |
| 42 | + |
| 43 | +<p><strong class="example">Example 3:</strong></p> |
| 44 | + |
| 45 | +<div class="example-block"> |
| 46 | +<p><strong>Input:</strong> <span class="example-io">s = "."</span></p> |
| 47 | + |
| 48 | +<p><strong>Output:</strong> <span class="example-io">false</span></p> |
| 49 | +</div> |
| 50 | + |
| 51 | +<p> </p> |
| 52 | +<p><strong>Constraints:</strong></p> |
| 53 | + |
| 54 | +<ul> |
| 55 | + <li><code>1 <= s.length <= 20</code></li> |
| 56 | + <li><code>s</code> consists of only English letters (both uppercase and lowercase), digits (<code>0-9</code>), plus <code>'+'</code>, minus <code>'-'</code>, or dot <code>'.'</code>.</li> |
| 57 | +</ul> |
0 commit comments