My accepted code in cpp


  • 0
    V

    I just use the simple algorithm, the solution has bee accepted, but I don't think it's a good answer, I'm going to read otherbody's code.

    class Solution {
    public:
      bool isNumber(const char *s) {
        if (!s) return false;
        // delete backspace
        while (*s == ' ') ++s;
        if (!*s) return false;
        const char *end = s;
        while (*end) ++end;
        --end;
        while (*end == ' ') --end;
    
        if (s > end) return false;
        string str(s, end + 1);
    
        if (str.empty()) return false;
        strlwr(str);
        if (HasInvalidChar(str)) {
          return false;
        }
    
        bool(Solution::*check_handlers[])(const string&) = {
          &Solution::is_hex,
          &Solution::is_oct,
          &Solution::is_int,
          &Solution::is_e,
          &Solution::is_double
        };
        for (auto func : check_handlers) {
          if ((this->*func)(str)) {
            return true;
          }
        }
        return false;
      }
    
    private:
      bool HasInvalidChar(const string& str) {
        const char *dict = "01234567890.abcdefx+-";
        for (auto c : str) {
          if (!strchr(dict, c)) {
            return true;
          }
        }
        return false;
      }
    
      // 0x123
      bool is_hex(const string &s) {
        string str = s;
        rm_signed(str);
        if (str.size() <= 2) {
          return false;
        }
        if (str[0] != 0 || str[1] != 'x') {
          return false;
        }
        for (string::size_type i = 2; i < str.size(); ++i) {
          if (!is_digit(str[i]) && !is_letter(str[i])) {
            return false;
          }
        }
        return true;
      }
    
      // 0123
      bool is_oct(const string &s) {
        string str = s;
        rm_signed(str);
        if (str.size() <= 1) {
          return false;
        }
        if (str[0] != 0) {
          return false;
        }
        for (string::size_type i = 1; i < str.size(); ++i) {
          if (str[i] < '0' || str[i] > '7') {
            return false;
          }
        }
        return true;
      }
    
      // 123
      bool is_int(const string &s) {
        string str = s;
        rm_signed(str);
        if (str.empty()) return false;
        for (auto c : str) {
          if (!is_digit(c)) {
            return false;
          }
        }
        return true;
      }
    
      // 12e3
      bool is_e(const string &str) {
        string::size_type pos = str.find('e');
        if (pos == string::npos) {
          return false;
        }
        string left(str.begin(), str.begin() + pos);
        string right(str.begin() + pos + 1, str.end());
        if (left.empty() || right.empty()) return false;
        return (is_double(left) || is_int(left)) && is_int(right);
      }
    
      /**
      * 0.1
      * .1
      * 000.1
      * 0.
      */
      bool is_double(const string& str) {
        string::size_type dot_pos = str.find('.');
        if (dot_pos == string::npos) return false;
        string left(str.begin(), str.begin() + dot_pos);
        string right(str.begin() + dot_pos + 1, str.end());
        rm_signed(left);
        if (left.empty() && right.empty()) return false;
        if (!left.empty()) {
          for (auto c : left) {
            if (!is_digit(c)) return false;
          }
        }
        if (!right.empty()) {
          for (auto c : right) {
            if (!is_digit(c)) {
              return false;
            }
          }
        }
        return true;
      }
    
      bool is_digit(char c) {
        return c >= '0' && c <= '9';
      }
    
      bool is_letter(char c) {
        return c >= 'a' && c <= 'f';
      }
    
      void strlwr(string &str) {
        for (auto &c : str) {
          c = tolower(c);
        }
      }
    
      /**
      * remove signed char on the left
      * +123 -> 123
      * -123 -> 123
      */
      void rm_signed(string &str) {
        if (str.empty()) return;
        if (str[0] == '-' || str[0] == '+') {
          str = string(str.begin() + 1, str.end());
        }
      }
    };

Log in to reply
 

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