Validate if a given string is numeric.
Some examples: Note: It is intended for the problem statement to be ambiguous. You should gather all requirements up front before implementing one. |
The question has been closed for the following reason "bulk close" by 1337c0d3r 25 Oct '13, 06:41
My solution using finite automata (inspired by @luckynoob):
|
Well, if we are allowed to use regex, this can be done in one line code. The running time depends on how the compile parses regex though.
|
|
private:
public:
|
more readable finite automata |
class Solution { public: bool isNumKernel(string&& s, bool num, bool point) { if (s.size()==1) { if(isdigit(s[0]) || (s[0]=='.' && num && !point)) return true; else return false; }
}; We can focus on the effectual string by using sstream. Dividing the string into 2 parts by 'e', the right part (if existed) should contain only sign and digits, while the left part should be a float. |
Ugly solution. Submitted and revised 10 time before accepted ...
} |