class Solution {
public:
bool isNumber(string s)
{
const int n = s.size();
int i = 0;
while(i < n && s[i] == ' ')
++i;
if(i == n)
return false;
if(s[i] == '+' || s[i] == '-')
++i;
int numofpoint=0;
int numofdigit=0;
while(s[i] == '.' || s[i] >= '0' && s[i] <= '9')
{
if(s[i] == '.')
++numofpoint;
else
++numofdigit;
++i;
}
if(numofpoint > 1 || numofdigit < 1)
return false;
if(s[i] == 'e')
{
++i;
if(s[i] == '-' || s[i] == '+')
++i;
int numofdigit1=0;
while(s[i]>='0' && s[i] <= '9')
{
++numofdigit1;
++i;
}
if(numofdigit1<1)
return false;
}
while(i<n)
{
if(s[i]==' ')
++i;
else
break;
}
if(i == n)
return true;
else
return false;
}
};
Valid Number
最新推荐文章于 2021-06-21 17:15:44 发布