Class Solution

java.lang.Object
g0001_0100.s0065_valid_number.Solution

public class Solution extends Object
65 - Valid Number.<p>Hard</p> <p>A <strong>valid number</strong> can be split up into these components (in order):</p> <ol> <li>A <strong>decimal number</strong> or an <strong>integer</strong>.</li> <li>(Optional) An <code>'e'</code> or <code>'E'</code>, followed by an <strong>integer</strong>.</li> </ol> <p>A <strong>decimal number</strong> can be split up into these components (in order):</p> <ol> <li>(Optional) A sign character (either <code>'+'</code> or <code>'-'</code>).</li> <li>One of the following formats: <ol> <li>One or more digits, followed by a dot <code>'.'</code>.</li> <li>One or more digits, followed by a dot <code>'.'</code>, followed by one or more digits.</li> <li>A dot <code>'.'</code>, followed by one or more digits.</li> </ol> </li> </ol> <p>An <strong>integer</strong> can be split up into these components (in order):</p> <ol> <li>(Optional) A sign character (either <code>'+'</code> or <code>'-'</code>).</li> <li>One or more digits.</li> </ol> <p>For example, all the following are valid numbers: <code>[&quot;2&quot;, &quot;0089&quot;, &quot;-0.1&quot;, &quot;+3.14&quot;, &quot;4.&quot;, &quot;-.9&quot;, &quot;2e10&quot;, &quot;-90E3&quot;, &quot;3e+7&quot;, &quot;+6e-1&quot;, &quot;53.5e93&quot;, &quot;-123.456e789&quot;]</code>, while the following are not valid numbers: <code>[&quot;abc&quot;, &quot;1a&quot;, &quot;1e&quot;, &quot;e3&quot;, &quot;99e2.5&quot;, &quot;--6&quot;, &quot;-+3&quot;, &quot;95a54e53&quot;]</code>.</p> <p>Given a string <code>s</code>, return <code>true</code> <em>if</em> <code>s</code> <em>is a <strong>valid number</strong></em>.</p> <p><strong>Example 1:</strong></p> <p><strong>Input:</strong> s = &ldquo;0&rdquo;</p> <p><strong>Output:</strong> true</p> <p><strong>Example 2:</strong></p> <p><strong>Input:</strong> s = &ldquo;e&rdquo;</p> <p><strong>Output:</strong> false</p> <p><strong>Example 3:</strong></p> <p><strong>Input:</strong> s = &ldquo;.&rdquo;</p> <p><strong>Output:</strong> false</p> <p><strong>Example 4:</strong></p> <p><strong>Input:</strong> s = &ldquo;.1&rdquo;</p> <p><strong>Output:</strong> true</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 <= s.length <= 20</code></li> <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> </ul>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details

    • isNumber

      public boolean isNumber(String s)