表示数值的字符串
表示数值的字符串
http://www.nowcoder.com/questionTerminal/e69148f8528c4039ad89bb2546fd4ff8
class Solution {
public:
bool isNumeric(string str)
{
bool sign = false, decimal = false, hasE = false;
for(int i = 0;i < str.length();i++)
{
if(str[i] == 'e' || str[i] == 'E')
{
if(i == str.length() - 1) return false;
if(hasE) return false;
hasE = true;
}
else if(str[i] == '+' || str[i] == '-')
{
if(sign && str[i - 1] != 'e' && str[i - 1] != 'E') return false;
if(!sign && i > 0 && str[i - 1] != 'e' && str[i - 1] != 'E') return false;
sign = true;
}
else if(str[i] == '.')
{
if(hasE || decimal) return false;
decimal = true;
}
else if(str[i] < '0' || str[i] > '9')
{
return false;
}
}
return true;
}
};