Leetcode Valid Number Problem Solution

In this Leetcode Valid Number Problem Solution A valid number can be split up into these components (in order):

A decimal number or an integer.

(Optional) An 'e' or 'E', followed by an integer.

A decimal number can be split up into these components (in order):

(Optional) A sign character (either '+' or '-').

One of the following formats:

One or more digits, followed by a dot '.'.

One or more digits, followed by a dot '.', followed by one or more digits.

A dot '.', followed by one or more digits.

An integer can be split up into these components (in order):

(Optional) A sign character (either '+' or '-').

One or more digits.

For example, all the following are valid numbers: ["2", "0089", "-0.1", "+3.14", "4.", "-.9", "2e10", "-90E3", "3e+7", "+6e-1", "53.5e93", "-123.456e789"], while the following are not valid numbers: ["abc", "1a", "1e", "e3", "99e2.5", "--6", "-+3", "95a54e53"].

Given a string s, return true if s is a valid number.

Leetcode Valid Number Problem Solution


Problem solution in Python.

class Solution:
    # @param s, a string
    # @return a boolean
    def isNumber(self, s):
        try: 
            float(s) 
            return True
        except: 
            return False

Problem solution in Java.

class Solution 
{
    public boolean isNumber(String s) 
    {
        if (s == null || s.length() == 0) return false;
        s = s.trim();
        int i = s.indexOf('e');
        if (i == -1)
        {
            return valid_float_sign(s);
        }
        
        return valid_float_sign(s.substring(0, i))
            && valid_int_sign(s.substring(i + 1));
    }
    
    // number is like: +3.8
    boolean valid_float_sign(String s)
    {
        if (s.length() == 0) return false;
        int i = 0;
        if (s.charAt(i) == '+' || s.charAt(i) == '-')
        {
            ++i;
        }
        
        return valid_float(s.substring(i));
    }
    
    // number is like: 3.8
    boolean valid_float(String s)
    {
        int i = s.indexOf('.');
        if (i == -1)
        {
            return valid_int(s);
        }
        
        String left = s.substring(0, i);
        String right = s.substring(i + 1);
        
        if (left.length() == 0) return valid_int(right);
        if (right.length() == 0) return valid_int(left);
        
        return valid_int(left) && valid_int(right);        
    }
    
    // number is like: -3
    boolean valid_int_sign(String s)
    {
        if (s.length() == 0) return false;
        int i = 0;
        if (s.charAt(i) == '+' || s.charAt(i) == '-')
        {
            ++i;
        }
        
        return valid_int(s.substring(i));
    }
    
    // number is like: 3
    boolean valid_int(String s)
    {
        if (s.length() == 0) return false;
        for (int i = 0; i < s.length(); ++i)
        {
            if (s.charAt(i) >= '0' && s.charAt(i) <= '9')
            {
                continue;
            }
            else return false;
        }
        
        return true;
    }
}


Problem solution in C++.

class Solution {
public:
    bool isNumber(string s) {
        int i, cnt=0, pos;
        string s1="", s2="";
        bool sign=false, exp=false;
        s=regex_replace(s, regex("^ +"), "");
        s=regex_replace(s, regex(" +$"), "");
        if(!s.size())
            return false;
        if(s[0]=='-' || s[0]=='+')
            s=s.substr(1);
        if(s.size()==1 && (s[0]=='.' || s[0]=='+' || s[0]=='-'))
            return false;
        if(s[s.size()-1]=='e' || s[s.size()-1]=='+' || s[s.size()-1]=='-')
            return false;
        for(i=0; i<s.size(); i++){
            if(s[i]=='.')
                cnt++;
            if(cnt>1)
                return false;
        }
        cnt=0;
        pos=s.find(".");
        if(pos!=string::npos){
            s1=s.substr(0, pos);
            s2=s.substr(pos+1);
        }
        else s2=s;
        if(!s1.size() && s2[0]=='e')
            return false;
        for(i=0; i<s1.size(); i++){
            if(int(s1[i])<48 || int(s1[i])>57)
                return false;
        }
        for(i=0; i<s2.size(); i++){
            if(int(s2[i])<48 || int(s2[i])>57){
                if(s2[i]!='+' && s2[i]!='-' && s2[i]!='e')
                    return false;
                if(s2[i]=='+' || s2[i]=='-'){
                   if(sign)
                       return false;
                    sign=true;
                }
                if(s2[i]=='e'){
                    if(exp)
                        return false;
                    exp=true;
                }
                if(!exp && sign)
                    return false;
                else if(exp && sign)
                    sign=true;
                
            }
        }
        return true;
    }
};


Problem solution in C.

bool isNumber(char *s){
    int i;
    int counter_sign = 0, counter_e = 0, counter_point = 0, counter_digit = 0, space_in_str = 0;
    char previous_c = '\0', c;
 
    int state = 1;
    for (i = 0; (c = s[i]) != '\0'; i++) {
        int is_sign = 0, is_e = 0, is_point = 0;
 
        if (c == '+' || c == '-'){
            is_sign = 1;
        }
 
        if (isdigit(c)){
            counter_digit++;
        }
 
        if (c == 'e'){
            is_e = 1;
        }
        if (c == '.'){
            is_point = 1;
        }
 
        if (is_sign == 1){
            if (( previous_c != '\0' && previous_c != ' ' && previous_c != 'e') || counter_sign >= 2 ||  previous_c == '.'){
                state = 0;
                break;
            } else {
                counter_sign++;
            }
        }
 
        if (is_point == 1){
            if ((!isdigit(previous_c) && previous_c != '\0' && previous_c != ' ' && previous_c != '+' && previous_c != '-') || counter_point >= 1 || (counter_point == 0 && counter_e >= 1)){
                state = 0;
                break;
            } else {
                counter_point++;
            }
        }
 
        if (is_e == 1){
            if ((!isdigit(previous_c) && previous_c != '.') || counter_e >= 1 || (previous_c == '.' && counter_digit == 0)){
                state = 0;
                break;
            } else {
                counter_e++;
            }
        }
 
        if (c > '9' && c != 'e') {
            state = 0;
            break;
        }
 
 
        if (c == ' ' && previous_c != ' ' && previous_c != '\0'){
            space_in_str = 1;
        }
 
        if (c != ' ' && space_in_str == 1){
            state = 0;
            break;
        }
 
        if (c == ' ' && previous_c == 'e'){
            state = 0;
            break;
        }
 
        previous_c = c;
    }
 
    if ((!isdigit(previous_c) && previous_c != ' ' && previous_c != '.')|| (counter_digit == 0)){
        state = 0;
    }
 
    if (state == 1){
        return true;
    } else {
        return false;
    }
}


Post a Comment

0 Comments