Share my solution with just if statement(using cpp)


  • 0
    O
    class Solution {
    

    public:
    bool isNumber(const char *s) {
    bool flaga = 1;
    bool flagb = 1;

        bool space = 0;
        bool nospace = 0;
        bool ex = 0;
        bool num = 0;
        bool num1 = 0;
        bool minus = 1;
        
        for (int i = 0; i < strlen (s); i ++)
        {
            if (s[i] <= '9' && s[i] >= '0')
                num = 1;
    
            if (s[i] == ' ' && nospace == 1)
                space = 1;
            
            if (nospace == 1 && space == 1 && s[i] != ' ')
                return 0;
                
            if (s[i] != ' ')
                nospace = 1;
            
            
            if ((s [i] > '9' || s [i] < '0') && s[i] != ' ')
            {
                if ((flaga && s[i] == '.') || (flagb && s[i] == 'e'))
                {
                    if (num == 0 && s[i] == 'e') return 0;
                    if (nospace == 0)  return 0;
                    if (i == 0 && (s [i] == 'e' || strlen (s) == 1))  return 0;  
                    else         
                    {
                        if (s[i] == '.') flaga = 0;
                        if (s[i] == 'e') 
                        {
                           num = 0;
                           flagb = 0;
                           flaga = 0;
                           ex = 1;
                           minus = 1;
                        }
                    }
                }
                else
                {
                    if (s [i] == '-' || s [i] == '+')
                    {
                        if (minus && num == 0 && (ex == 1 || flaga == 1) )
                            minus = 0;
                        else 
                            return 0;
                    }
                    else
                        return 0;
                }
            }
    
        }
        
        if (nospace == 0 || num == 0)  return 0;
        else               return 1;
    }
    

    };


Log in to reply
 

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