# My accepted C++ solution (greedy)

• ``````class Solution {
public:
bool isNumber(const char *s) {
bool point = false, notation = false;
int len = strlen(s);
int start = 0, end = len;

for(int i = start; i < end; i++)
if(s[i] == ' ')     start++;
else                break;
for(int i = end - 1; i >= 0; i--)
if(s[i] == ' ')     end--;
else                break;
if(start >= end)        return false;

if(s[start] == '+' || s[start] == '-')
start++;

if(s[start] == '.' && ((s[start + 1] < '0') || (s[start + 1] > '9')))
return false;//special case  '.'

/* this case is right in this problem
if(s[start] > '9'|| s[start] < '0')
return false;// in case ".0887"
*/

if(s[start] == 'e') return false;

for(int i = start; i < end; i++){
if(s[i] <= '9' && s[i] >= '0')  continue;
else if(s[i] == '.'){
start = i + 1;// ex: 904.[4]2
point = true;
break;
}
else if(s[i] == 'e'){
start = i + 1;
if(s[start] == '+' || s[start] == '-')
start++;
notation = true;
break;
}
else    return false;
}

if(point){
if(start >= end)                        return true; //ex:  "90932."
//if(s[start] > '9' || s[start] < '0')    return false;

for(int i = start; i < end; i++){
if(s[i] <= '9' && s[i] >= '0')  continue;
else if(s[i] == 'e'){
start = i + 1;
if(s[start] == '+' || s[start] == '-')
start++;
notation = true;
break;
}
else    return false;
}
}

if(notation){
if(start >= end)                            return false;
else if(s[start] > '9' || s[start] < '0')   return false;

for(int i = start; i < end; i++)
if(s[i] <= '9' && s[i] >= '0')  continue;
else                            return false;
}
return true;
}
};``````

• 太繁琐了，应该用自动机来做最好，逻辑太复杂，不适合面试写

• DFS may be easier.

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