Keep trying(submitting and checking all corner cases...), and your codes will be accepted lol....


  • 0
    class Solution {
    public:
        bool isNumber(string s) {
            int idx = 0;
            int back_idx = s.size() - 1;
            while(idx < s.size() && s[idx] == ' '){
                idx++;
            }
            while(back_idx >= 0 && s[back_idx] == ' '){
                back_idx--;
            }
            while(idx < s.size() && (s[idx] == '+' || s[idx] == '-'))
                idx++;
            if(idx > back_idx)
                return false;
            bool has_e = false;
            bool has_int = false;
            bool has_dot = false;
            bool has_signature = false;
            while(idx <= back_idx){
                if(s[idx] >= '0' && s[idx] <= '9'){
                    idx++;   
                    has_int = true;
                }
                else if(s[idx] == 'e' && has_int && !has_e && idx + 1 < s.size() && (isdigit(s[idx + 1]) ||s[idx + 1] == '-' || s[idx + 1] == '+')){
                        idx++;
                        has_e = true;
                }
                else if(s[idx] == '.' && !has_dot && !has_e && (has_int || (idx + 1 < s.size() && isdigit(s[idx + 1])))){
                    has_dot = true;
                    idx++;
                }
                else if((s[idx] == '+' || s[idx] == '-') && (idx - 1 < 0 || s[idx - 1] == 'e') && idx + 1 < s.size() && isdigit(s[idx + 1])){
                    idx++;
                }
                else
                    return false;
            }
            return true;
        }
    };
    

Log in to reply
 

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