Valid Number Problem Solution

Valid Number Problem:valid number can be split up into these components (in order):

  1. decimal number or an integer.
  2. (Optional) An 'e' or 'E', followed by an integer.

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.

Example :

Input: s = "0"
Output: true

Valid Number Problem Solution

Problem Solution In Python

class Solution(object):
    def isNumber(self, s,t=1,tt=1):
        if t==1: s,i=s.strip(' '),s.strip(' ').find('e')
        if t==1 and i!=-1: return self.isNumber(s[:i],0,1) and self.isNumber(s[i+1:],-1,0)
        if s.count('.')>tt: return False
        return len(s)-len(s.lstrip('+-'))<2 and s.lstrip('+-').replace('.','').isdigit()

Problem Solution In Java

class Solution {
    public boolean isNumber(String s) {
        if (s == null)return false;
        s = s.trim();
        if (s.length() == 0) return false;
        boolean point = false, sign = false, exponent = false, number = false;
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            if (!Character.isDigit(c)) {
                if (c != 'e' && c != '+' && c != '-' && c != '.') return false;
                if (c == '.') {
                    if (point) {
                        return false;
                    } else if (exponent) {
                        return false;
                    } else {
                        point = true;
                    }
                }
                if (c == 'e') {
                    if (exponent) return false;
                    if (!number) return false;
                    exponent = true;
                    number = false;
                }
                if (c == '+' || c == '-') {
                    if (i == 0) {
                        sign = true;
                    } else {
                        if (s.charAt(i - 1) != 'e') return false;
                    }
                }
            } else {
                number = true;
            }
        }
        return number;
    }
}

Problem Solution In C++

bool isNumber(string s) {
        int len = s.length();
        bool sign = false;
        bool num = false;
        bool point = false;
        bool e = false;

        int i = 0;
        while (i < len && s[i] == ' ') i++;
        int j = len-1;
        while(j >=0 && s[j] == ' ') j--;
        
        for(;i <= j; ++i)
        {
            if(s[i] >= '0' && s[i] <= '9')
            {
                num = true;
            }
            else if(s[i] == '+' || s[i] == '-')
            {
                if(num || point)
                    return false;
                if(!sign)
                    sign = true;
                else
                    return false;
            }
            else if(s[i] == '.')
            {
                if(e)
                    return false;
                if(!point)
                    point = true;
                else
                    return false;
            }
            else if(s[i] == 'e')
            {
                if(!num)
                    return false;
                if(!e)
                {
                    e = true;
                    num = false;
                    sign = false;
                    point = false;
                }else
                    return false;
            }
            else
                return false;
        }
        
        return num;
    }

Problem Solution In C

#define EXPONENT 1
#define POINTED 2
#define SIGNED 4
#define NUMBERED 8

bool isNumber(char * s){
    int flags = 0;
    char* ss;
    unsigned long len = strlen(s);
    if(*s == 'e' || s[len - 1] == 'e')
        return false;
beg_trim:
    if(*s == ' '){
        ++s;
        --len;
        if(len == 0)
            return false;
        goto beg_trim;
    }
    
after_trim:
    ss = s;
    if(s[len - 1] == ' '){
        s[len-1] = 0;
        --len;
         if(len == 0)
            return false;
        goto after_trim;
    }
   if(len == 0)
        return false;
    while(*s){
        if(*s == '+' || *s == '-'){
            if(flags & SIGNED)
                return false;
            flags |= SIGNED;
            ++s;
            continue;
        } else if(*s == 'e'){
            if(flags & EXPONENT)
                return false;
            if(!(flags & NUMBERED))
                return false;
            flags |= EXPONENT | POINTED;
            flags &= ~SIGNED;
            flags &= ~NUMBERED;
            ++s; 
            continue;
        } else if(*s == '.'){
            if(flags & POINTED)
                return false;
            flags |= SIGNED;
            flags |= POINTED;
            ++s;
            continue;
        } else if(*s >= '0' && *s <= '9'){
            flags |= SIGNED;
            flags |= NUMBERED;
            ++s;
            continue;
        }
        return false;
    }
    if(len == 1 && ((*s == '+' || *s =='-') || flags & EXPONENT || flags & POINTED))
        return false;
    if( !(flags & NUMBERED) && (flags & EXPONENT))
        return false;
    if(flags * POINTED && !(flags & NUMBERED))
        return false;
    return true;
}

Shares
facebook sharing button Share
twitter sharing button Tweet
whatsapp sharing button Share
telegram sharing button Share
pinterest sharing button Pin

Leave a Comment