Valid Number
Validate if a given string is numeric.
Some examples:
Solution:
1. first remove all spaces at the beginning.
2. then check the +/- sign.
3. '.' can only appear once, and the character before '.' or after '.' must be number.
4. 'e' can only appear once, and the character before 'e' must be number, the characters after 'e' can be '+/-' or numbers.
5. '.' cannot appear after 'e'.
6. ignore the spaces at the end.
Some examples:
"0" => true " 0.1 " => true "abc" => false "1 a" => false "2e10" => trueNote: It is intended for the problem statement to be ambiguous. You should gather all requirements up front before implementing one.
Solution:
1. first remove all spaces at the beginning.
2. then check the +/- sign.
3. '.' can only appear once, and the character before '.' or after '.' must be number.
4. 'e' can only appear once, and the character before 'e' must be number, the characters after 'e' can be '+/-' or numbers.
5. '.' cannot appear after 'e'.
6. ignore the spaces at the end.
Comments
Post a Comment