Nav: << previous: 64.最小路径和 | next: 66.加一 >>
Description
tab: English
<p>Given a string <code>s</code>, return whether <code>s</code> is a <strong>valid number</strong>.<br />
<br />
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>
<p>Formally, a <strong>valid number</strong> is defined using one of the following definitions:</p>
<ol>
<li>An <strong>integer number</strong> followed by an <strong>optional exponent</strong>.</li>
<li>A <strong>decimal number</strong> followed by an <strong>optional exponent</strong>.</li>
</ol>
<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>
<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>
<ol>
<li><strong>Digits</strong> followed by a <strong>dot</strong> <code>'.'</code>.</li>
<li><strong>Digits</strong> followed by a <strong>dot</strong> <code>'.'</code> followed by <strong>digits</strong>.</li>
<li>A <strong>dot</strong> <code>'.'</code> followed by <strong>digits</strong>.</li>
</ol>
<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>
<p>The <strong>digits</strong> are defined as one or more digits.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">s = "0"</span></p>
<p><strong>Output:</strong> <span class="example-io">true</span></p>
</div>
<p><strong class="example">Example 2:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">s = "e"</span></p>
<p><strong>Output:</strong> <span class="example-io">false</span></p>
</div>
<p><strong class="example">Example 3:</strong></p>
<div class="example-block">
<p><strong>Input:</strong> <span class="example-io">s = "."</span></p>
<p><strong>Output:</strong> <span class="example-io">false</span></p>
</div>
<p> </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>
---
[submissions](https://leetcode.com/problems/valid-number/submissions/) | [solutions](https://leetcode.com/problems/valid-number/solutions/)
tab: 中文
<p>给定一个字符串 <code>s</code> ,返回 <code>s</code> 是否是一个 <strong>有效数字</strong>。</p>
<p>例如,下面的都是有效数字:<code>"2", "0089", "-0.1", "+3.14", "4.", "-.9", "2e10", "-90E3", "3e+7", "+6e-1", "53.5e93", "-123.456e789"</code>,而接下来的不是:<code>"abc", "1a", "1e", "e3", "99e2.5", "--6", "-+3", "95a54e53"</code>。</p>
<p>一般的,一个 <strong>有效数字</strong> 可以用以下的规则之一定义:</p>
<ol>
<li>一个 <strong>整数</strong> 后面跟着一个 <strong>可选指数</strong>。</li>
<li>一个 <strong>十进制数</strong> 后面跟着一个 <strong>可选指数</strong>。</li>
</ol>
<p>一个 <strong>整数</strong> 定义为一个 <strong>可选符号</strong> <code>'-'</code> 或 <code>'+'</code> 后面跟着 <strong>数字</strong>。</p>
<p>一个 <strong>十进制数</strong> 定义为一个 <strong>可选符号 </strong><code>'-'</code> 或 <code>'+'</code> 后面跟着下述规则:</p>
<ol>
<li><strong>数字 </strong>后跟着一个 <strong>小数点 <code>.</code></strong>。</li>
<li><strong>数字 </strong>后跟着一个 <strong>小数点 <code>.</code> </strong>再跟着<strong> 数位</strong>。</li>
<li>一个 <strong>小数点 <code>.</code> </strong>后跟着<strong> 数位</strong>。</li>
</ol>
<p><strong>指数</strong> 定义为指数符号 <code>'e'</code> 或 <code>'E'</code>,后面跟着一个 <b>整数</b>。</p>
<p><strong>数字</strong> 定义为一个或多个数位。</p>
<p> </p>
<p><strong class="example">示例 1:</strong></p>
<div class="example-block">
<p><strong>输入:</strong><span class="example-io">s = "0"</span></p>
<p><strong>输出:</strong><span class="example-io">true</span></p>
</div>
<p><strong class="example">示例 2:</strong></p>
<div class="example-block">
<p><strong>输入:</strong><span class="example-io">s = "e"</span></p>
<p><strong>输出:</strong><span class="example-io">false</span></p>
</div>
<p><strong class="example">示例 3:</strong></p>
<div class="example-block">
<p><strong>输入:</strong><span class="example-io">s = "."</span></p>
<p><strong>输出:</strong><span class="example-io">false</span></p>
</div>
<p> </p>
<p><strong>提示:</strong></p>
<ul>
<li><code>1 <= s.length <= 20</code></li>
<li><code>s</code> 仅含英文字母(大写和小写),数字(<code>0-9</code>),加号 <code>'+'</code> ,减号 <code>'-'</code> ,或者点 <code>'.'</code> 。</li>
</ul>
---
[提交记录](https://leetcode.cn/problems/valid-number/submissions/) | [题解](https://leetcode.cn/problems/valid-number/solution/)
Solutions & Notes
properties:
note.updated:
displayName: Last Updated
note.relative_links:
displayName: Related Links
note.desc:
displayName: Description
note.grade:
displayName: Rating
note.program_language:
displayName: Language
note.time_complexity:
displayName: TC
note.space_complexity:
displayName: SC
views:
- type: table
name: Solutions & Notes
filters:
and:
- file.hasLink(this.file)
- file.tags.containsAny("leetcode/solution", "leetcode/note")
order:
- file.name
- desc
- program_language
- time_complexity
- space_complexity
- grade
- relative_links
- updated
sort:
- property: grade
direction: ASC
- property: time_complexity
direction: ASC
- property: program_language
direction: ASC
columnSize:
file.name: 104
note.space_complexity: 65
note.grade: 126
Similar Problems
properties:
note.lcTopics:
displayName: Topics
note.lcAcRate:
displayName: AC Rate
note.favorites:
displayName: Favorites
note.grade:
displayName: Rating
note.translatedTitle:
displayName: Title (CN)
note.lcDifficulty:
displayName: Difficulty
views:
- type: table
name: Similar Problems
filters:
and:
- file.hasLink(this.file)
- similarQuestions.contains(this.file)
order:
- file.name
- translatedTitle
- lcTopics
- lcDifficulty
- lcAcRate
- grade
- favorites
sort:
- property: file.name
direction: ASC
- property: lcTopics
direction: DESC
columnSize:
note.translatedTitle: 240
note.lcTopics: 347
note.lcAcRate: 75
note.grade: 122