Valid Number

Validate if a given string is numeric.
Some examples:
"0" => true
" 0.1 " => true
"abc" => false
"1 a" => false
"2e10" => true
Note: 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

Popular posts from this blog

Maximum Gap

[ITint5] Maximum Subarray for a Circular Array

[CC150] Chapter 8 Object-Oriented Design