Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- bool isUnsigned(const string& s)
- {
- if(s.empty()) return false;
- return all_of(s.begin(), s.end(), [](unsigned char ch) {return isdigit(ch); });
- }
- bool isInt(const string& s)
- {
- if (s.empty()) return false;
- string::size_type start = (s.front() == '+' || s.front() == '-' ? 1 : 0);
- return isUnsigned(s.substr(start));
- }
- bool isRational(const string& s)
- {
- if (s.empty()) return false;
- auto end = s.rfind('.');
- if (end == string::npos) return isInt(s);
- else
- {
- string::size_type start = (s.front() == '+' || s.front() == '-' ? 1 : 0);
- auto isEmpty = start == end;
- if (!isEmpty && !isInt(s.substr(0, end))) return false;
- if (isEmpty && end == s.size() - 1) return false;
- if (end != s.size() -1 && !isUnsigned(s.substr(end + 1))) return false;
- }
- return true;
- }
- class Solution {
- public:
- bool isNumber(string& s)
- {
- s.erase(s.begin(), find_if(s.begin(), s.end(), [](unsigned char ch) {return !isspace(ch); }));
- s.erase(find_if(s.rbegin(), s.rend(), [](unsigned char ch) {return !isspace(ch); }).base(), s.end());
- auto ePos = s.find('e');
- if (ePos == string::npos) return isRational(s);
- return isRational(s.substr(0, ePos)) && isInt(s.substr(ePos + 1));
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement