题目
65. Valid Number
- Difficulty: Hard
- Total Accepted: 99.5K
- Total Submissions: 753.7K
Validate if a given string can be interpreted as a decimal number.
Some examples:"0" => true" 0.1 " => true"abc" => false"1 a" => false"2e10" => true" -90e3 " => true" 1e" => false"e3" => false" 6e-1" => true" 99e2.5 " => false"53.5e93" => true" --6 " => false"-+3" => false"95a54e53" => false
Note: It is intended for the problem statement to be ambiguous. You should gather all requirements up front before implementing one. However, here is a list of characters that can be in a valid decimal number:
Numbers 0-9
Exponent - “e”
Positive/negative sign - “+”/“-“
Decimal point - “.”
Of course, the context of these characters also matters in the input.
解题报告
AC 截图
题目大意
检查一个字符串的内容(在删去前后缀的空格后)是否是一个合法的十进制数。
解题思路
该题可以通过将字符串分块来令问题化为简单的子问题的组合。首先考虑有 E 的字符串,则我们只需要判断左边是否为一个合法小数,右边是否为一个合法整数。
对于一个小数字符串,我们只需要判断小数点左边和右边是否为一个合法整数。
对于一个整数字符串,我们只需要判断除了符号外,其他字符是否是一个合法数字即可。
坑点
1. 和 .3 都是合法的数字。
题解
AC 截图
C++ 代码
1 | class Solution { |