C++ solution (Fastest on LCOJ)


  • 0
    C
    class Solution {
    public:
        void skip_ws(const string& s, size_t& i) {
            while (i < s.size() && (s[i] == ' ' || s[i] == '\t' || s[i] == '\n'))
                i++;
        }
        int read_digits(const string& s, size_t& i, bool allow_sign) {
            int count = 0;
            if (allow_sign && (s[i] == '-' || s[i] == '+')) {
                i++;
            }
            while (i < s.size() && (s[i] >= '0' && s[i] <= '9')) {
                count++; i++;
            }
            return count;
        }
        bool isNumber(string s) {
            size_t i = 0;
            skip_ws(s, i);
            int count = read_digits(s, i, true);
            if (s[i] == '.') {
                i++;
                count += read_digits(s, i, false);
            }
            if (s[i] == 'e') {
                i++;
                if (!read_digits(s, i, true) > 0) return false;
            }
            skip_ws(s, i);
            return (count > 0 && i == s.size());
        }
    };

Log in to reply
 

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