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.
Idea: What is the problem?
input: string
output: true or false
function: check whether the string is numeric or not?
question: but what does that mean for a string is numeric or not.
testing case:
(1) the string includes only digits, then it is numeric, and return true;
(2) the string includes digits with some whitespace, then return true;
........ Lots of test cases.
class Solution {
public:
bool isNumber(const char *s) {
if(s == nullptr) return false;
//remove the space in the front of the string.
while(*s == ' ') s++;
if(*s == '\n') return false;
else if(*s == '+' || *s == '-') s++;
bool dot = false;
bool expo = false;
bool space = false;
bool firstPart = false;
bool secondPart = false;
while(*s != '\n'){
if(*s == '.')
{
if(dot || expo || space)
return false;
else
dot = true;
}
else if(*s == 'e' || *s == 'E'){
if(!firstPart || expo || space)
return false;
else
expo = true;
}
else if(isDigit(*s)){
if(space) return false;
if(!expo) firstPart = true;
else secondPart = true;
}
else if(*s == ' '){
space = true;
}
else
return false;
s++;
}
if(!firstPart)
return false;
else if(expo && !secondPart)
return false;
else
return true;
}
private:
bool isDigit(char c){
if(c - '0' <= 9 && c - '0' >=0) return true;
else return false;
}
};
这种方法没通过,显示 wrong answer, while test case: “3” should return true, but return false;
下面的方法通过了:
class Solution {
public:
bool isNumber(const char *s) {
string num = s; //good idea
int i=0;
while(num[i]==' ')i++;
if(num[i]=='-' || num[i]=='+')i++;
int j=num.size()-1;
while(num[j]==' ')j--; //easy to deal with this case.
if(i<=j)
num = num.substr(i,j-i+1);
else return false;
int dot = -1;
int ee = -1;
for(int i=0; i<num.size(); i++)
{
if(dot == -1 && num[i] == '.')
dot = i;
else if(ee == -1 && num[i] == 'e'){
ee = i;
if(num[i+1] == '-' || num[i+1]=='+')
i++;
}
else{
int tmpnum = num[i]-'0';
if(0<=tmpnum && tmpnum<=9)continue;
else return false;
}
}
//xxx.xxexx
string startstr,midstr,laststr;
if(dot==-1 && ee==-1){//xxxx
startstr = num;
if(startstr.size()<1)return false;
}else if(dot!=-1 && ee==-1){//xxx.xxx
startstr = num.substr(0,dot);
midstr=num.substr(dot+1);//.1,2.,0.1,0.0,2.0
if(startstr.size()<1 && midstr.size()<1)
return false;
}else if(dot==-1 && ee!=-1){//xxxexxx
startstr = num.substr(0,ee);
if(startstr.size()<1)return false;
if(num[ee+1] == '-' || num[ee+1]=='+')
laststr = num.substr(ee+2);
else
laststr = num.substr(ee+1);
if(laststr.size()<1)return false;
}else{//xxx.xxexx
if(dot>ee)return false;
startstr = num.substr(0,dot);
midstr=num.substr(dot+1,ee-dot-1);
if(startstr.size()<1 && midstr.size()<1)
return false;
if(num[ee+1] == '-' || num[ee+1]=='+')
laststr = num.substr(ee+2);
else
laststr = num.substr(ee+1);
if(laststr.size()<1)return false;
}
return true;
}
};
Reference:http://blog.csdn.net/doc_sgl/article/details/13505069