65 Valid Number
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.
这题不是老师讲感觉这辈子都不会做。主要做法是,先判断+/-号,然后数字+‘.',最后’e'的部分。返回时注意判断是否整条string都parse掉。然后在判断‘e'的部分时,记得e后面要有数字。如果没加数字就已经到末尾的话,直接返回false。
这题需要注意的地方是:
trim完以后得判断一下是否已经为空,为空return false。例如:“ ”
每个while的地方都得判断i是否越界
小数点大于1 & 数字小于1。例如:"."
Last updated