Possible C++ Solution


  • 1
    D
    class Solution {
    public:
        bool isNumber(string s) {
            int i = 0;
            for (; i < s.size() && s[i] == ' '; i++);
            
            if (i < s.size() && s[i] == '-' || s[i] == '+') i++;
            
            bool digit = false;
            
            for (; i < s.size() && isdigit(s[i]); i++) digit = true;
            
            if (i < s.size() && s[i] == '.') i++;
            
            for (; i < s.size() && isdigit(s[i]); i++) digit = true;
            
            if (i < s.size() && (s[i] == 'e' || s[i] == 'E') && digit) {
                i++;
                digit = false;
                if (i < s.size() && (s[i] == '-' || s[i] == '+')) i++;
            }
            
            for (; i < s.size() && isdigit(s[i]); i++) digit = true;
            
            for (; i < s.size() && s[i] == ' '; i++);
            
            return i == s.size() && digit;
        }
    };

Log in to reply
 

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