# C++ 8ms straight forward solution

• steps are:

1. trim leading and trailing empty space
2. check +,- sign
3. decimal part consists of 2 chunks of numbers and one decimal point, if only have decimal point but no numbers, return false
4. exponential part can have +/- sign right after e/E
``````class Solution {
public:
bool isNumber(string s)
{
int l = 0, len = s.length(), r = len - 1;
bool num = false;
while(l<=r && s[l]==' ') l++;
while(l<=r && s[r]==' ') r--;
if(l>r) return false;

//sign
if(s[l]=='+' || s[l]=='-')
{
l++;
if(l>r) return false;
}

//numbers before decimal point
while(l<=r && isdigit(s[l]))
{
l++;
num = true;
}
if(l>r) return true;

//decimal point
if(s[l]=='.')
{
l++;
if(!num && l>r) return false;
}

//digits after decimal point and before exponential section
while(l<=r && isdigit(s[l]))
{
l++;
num = true;
}
if(l>r) return true;

//exponential section
if(s[l]=='e' || s[l]=='E')
{
if(!num) return false;
l++;
if(l<=r && (s[l]=='+' || s[l]=='-')) l++;
if(l>r) return false;
while(l<=r && isdigit(s[l]))l++;
if(l>r) return true;
else return false;
}
return false;
}
};``````

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