Problem
Validate if a given string can be interpreted as a decimal number.
Some examples:
1 |
|
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.
Update (2015-02-10):
The signature of the C++
function had been updated. If you still see your function signature accepts a const char *
argument, please click the reload button to reset your code definition.
Explanation
- Loop through all characters of the string. Initialize variables
boolean num = false, numAfterE = true, dot = false, exp = false, sign = false
to mark if we have seen these variable characteristic, then check different conditions.
Solution
1 |
|