Topic:
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.
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 on the reload button to reset your code Definition.
test instructions and analysis: determines whether a string can be converted to a legitimate The number , the main note that at most only one decimal point and e,e cannot have the decimal point first or the last digit can be a decimal point, +/-can only appear in the first bit or after the first bit of E.
Code:
class Solution {public Boolean isnumber (string s) {//Determine if a string is a valid number s = S.trim (); Boolean pointseen = false; Boolean eseen = false; Boolean isnumber = false; for (int i=0;i<s.length (); i++) {Char temp = S.charat (i); if (temp>= ' 0 ' && temp<= ' 9 ') {Isnumber = true; }else if (temp = = '. ') {if (Eseen | | pointseen) return FALSE; Pointseen = true; }else if (temp = = ' E ') {if (Eseen | |!isnumber) return FALSE; Eseen = true; Isnumber = false; }else if (temp = = ' + ' | | temp== '-') {//can only be in the first or isnumber followed by +/-if (i!=0 && s.charat (i-1)! = ' E ') return false; Isnumber = false; }else return false; } return isnumber; }}
[Leetcode] 65. Valid number Java