-
-
Save eclipselu/7101838 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
public: | |
bool isNumber(const char *s) { | |
if (!s) return false; | |
bool sign = true; bool num = false; | |
bool e = true; bool dot = true; int space = 0; | |
char c; | |
while (c=*s++) { | |
if (c != ' ' && space == 2) return false; | |
if (c=='+' || c=='-') { | |
if (!sign) return false; | |
sign = false; space = 1; | |
} else if (isdigit(c)) { | |
sign = false; num = true; | |
space = 1; | |
} else if ((c=='e' || c=='E')) { | |
if (!e || !num) return false; | |
e = false; dot = false; | |
sign = true; num = false; | |
space = 1; | |
} else if (c=='.') { | |
if (!dot) return false; | |
dot = false; sign = false; | |
space = 1; | |
} else if (c==' ') { | |
if (space == 1) space = 2; | |
} else return false; | |
} | |
return num; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment