# 8ms c++ solution, disgusting

• ``````class Solution {
public:
void trim(string& s){
if(s[0] == ' ' || s[s.size()-1] == ' '){
size_t pos = s.find_first_not_of(" "); //trim the left side.
s.erase(0, pos);

pos = s.find_last_not_of(" "); //trim the right side.
if(string::npos != pos)
s.erase(pos+1);
}
}
bool isNumber(string& s) {
bool exponent = false;
bool flag = true;
//first, trim the string.
trim(s);
//we can count the number of periods.
size_t periods = count(s.begin(), s.end(), '.');
if(periods > 1)
return false;
else if(s.size() <= 1){ //process when the string is 1 or smaller.
if(isdigit(s[0]) == false)
return false;
return true;
}
for(int i=0; i<s.size(); ++i){ //this iteration stops input like "-."
if(isdigit(s[i]) == true){ //first time we find a valid number,
flag = false;   //break the loop and set the flag.
break;
}
}
if(flag)
return false;
else {
if(isdigit(s[0]) == false && s[0] != '.' && s[0] != '+' && s[0] != '-')
return false;
if(isdigit(s[s.size()-1]) == false && s[s.size()-1] != '.')
return false;
if(s[0] == '.' && isdigit(s[1]) == false)
return false;
else {
for(int i=1; i<s.size(); ++i){ //start at i=1 because we've covered the first index above..
if(s[i] == '.' && exponent == false)
continue;
if(s[i] == 'e' && exponent == false){
if(i+1 != s.size() && s[i+1] != '+' && s[i+1] != '-')
if(isdigit(s[i-1]) == false && s[i-1] != '.' || isdigit(s[i+1]) == false)
return false;
exponent = true;
continue;
}
else if(isdigit(s[i]) == false)
if(s[i-1] != 'e') //allow input like "103812e+6"
return false;
}
}
return true;
}
}
};``````

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.