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.
My solution is tedious, and I consider three possibilities separately.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
public: | |
bool isInt(string s){ | |
for(int i=0; i<s.length(); ++i){ | |
int c=s[i]-'0'; | |
if(c>9||c<0){ | |
if((s[i]=='+'||s[i]=='-')&&i==0&&i!=s.length()-1) | |
continue; | |
return false; | |
} | |
} | |
return true; | |
} | |
bool isFloat(string s){ | |
int pofpt=-1; | |
for(int i=0; i<s.length(); i++){ | |
if(s[i]=='.'){ | |
pofpt=i; | |
break; | |
} | |
} | |
if(pofpt==-1||(s.length()==1)) | |
return false; | |
string s0=s.substr(0, pofpt); | |
string s1=s.substr(pofpt+1, s.length()-pofpt); | |
bool r0=isInt(s0); | |
bool r1=isInt(s1); | |
if(s0.length()==1&&(s0[0]=='-'||s0[0]=='+')) | |
r0=true; | |
if(s1[0]=='-'||s1[0]=='+') | |
r1=false; | |
if((s0[s0.length()-1]=='-'||s0[s0.length()-1]=='+')&&s1.empty()) | |
return false; | |
return r0&&r1; | |
} | |
bool isSci(string s){ | |
int pofe=-1; | |
for(int i=0; i<s.length(); i++){ | |
if(s[i]=='e'){ | |
pofe=i; | |
break; | |
} | |
} | |
if(pofe==0||pofe==s.length()-1||pofe==-1) | |
return false; | |
string s0=s.substr(0, pofe); | |
string s1=s.substr(pofe+1, s.length()-pofe); | |
bool r0=(isInt(s0)||isFloat(s0)); | |
bool r1=isInt(s1); | |
return r0&&r1; | |
} | |
bool isNumber(string s) { | |
if(s.length()==0) | |
return false; | |
int p=0; | |
for(;p<s.length(); p++){ | |
if(s[p]!=' ') | |
break; | |
} | |
s.erase(s.begin(), s.begin()+p); | |
p=s.length()-1; | |
for(; p>=0; p--){ | |
if(s[p]!=' ') | |
break; | |
} | |
s.erase(s.begin()+p+1, s.end()); | |
if(s.length()==0) | |
return false; | |
bool r0=isInt(s); | |
bool r1=isFloat(s); | |
bool r2=isSci(s); | |
return (r0||r1||r2); | |
} | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
public: | |
bool isNumber(string s) { | |
int i = 0, n = s.length(); | |
// Skip the leading spaces | |
while (i < n && isspace(s[i])) i++; | |
// Skip the sign | |
if (s[i] == '+' || s[i] == '-') i++; | |
// Check for the following significant parts | |
int digits = 0, dots = 0; | |
while (i < n && (isdigit(s[i]) || s[i] == '.')) { | |
if (isdigit(s[i])) digits++; | |
else dots++; | |
i++; | |
} | |
if (digits < 1 || dots > 1) return false; | |
if (i == n) return true; | |
// Check for the exponential parts | |
if (s[i] == 'e') { | |
i++; | |
if (i == n) return false; | |
if (s[i] == '+' || s[i] == '-') i++; | |
digits = 0; | |
while (i < n && (isdigit(s[i]))) { | |
digits++; | |
i++; | |
} | |
if (digits < 1) return false; | |
} | |
// Skip the trailing spaces | |
while (i < n && isspace(s[i])) i++; | |
return i == n; | |
} | |
}; |
No comments:
Post a Comment