C# solution for Integer, Double and Exponetial


  • 0
    K
    public class Solution {
        public bool IsNumber(string s) {
            s = s.Trim();
            return IsIntegerNumber(s) || IsDoubleNumber(s) || IsExponetialNumber(s);
        }
        
        public bool IsIntegerNumber(string s)
        {
            bool firstChar = true;
            bool digitsFound = false;
            
            foreach(char c in s)
            {
                if (firstChar)
                {
                    firstChar = false;
                    if( c == '-' || c == '+')
                    {
                        continue;
                    }
                }
                
                if ( c >= '0' && c<='9')
                {
                    digitsFound = true;
                    continue;
                }
                else
                {
                    return false;
                }
            }
            
            Console.WriteLine("IsIntegerNumber: {0}", digitsFound);
            return digitsFound;
        }
        
        public bool IsDoubleNumber(string s)
        {
            bool firstChar = true;
            bool decimalEncountered = false;
            bool digitsFound = false;
            
            foreach(char c in s)
            {
                if (firstChar)
                {
                    firstChar = false;
                    if( c == '-' || c =='+')
                    {
                        continue;
                    }
                }
                
                if( c == '.')
                {
                    if (decimalEncountered == true)
                    {
                        return false;
                    }
                    decimalEncountered = true;
                    continue;
                }
                
                if ( c >= '0' && c<='9')
                {
                    digitsFound = true;
                    continue;
                }
                else
                {
                    return false;
                }
            }
            
            Console.WriteLine("IsDoubleNumber: {0}", digitsFound);
            return digitsFound;
        }
        
        public bool IsExponetialNumber(string s)
        {
            bool firstChar = true;
            bool decimalEncountered = false;
            bool beforeDigitsFound = false;
            bool eFound = false;
            bool afterDigitsFound = false;
            
            bool firstCharAfterE = false;
            
            foreach(char c in s)
            {
                if (firstChar || firstCharAfterE)
                {
                    firstChar = false;
                    firstCharAfterE = false;
                    if( c == '-' || c == '+')
                    {
                        continue;
                    }
                }
                
                if( c == '.')
                {
                    if (decimalEncountered == true || eFound)
                    {
                        return false;
                    }
                    decimalEncountered = true;
                    continue;
                }
                
                if( c == 'e')
                {
                    if (eFound == true || !beforeDigitsFound)
                    {
                        return false;
                    }
                    eFound = true;
                    firstCharAfterE = true;
                    continue;
                }
                
                if ( c >= '0' && c<='9')
                {
                    if(eFound)
                    {
                        afterDigitsFound = true;
                    }
                    else
                    {
                        beforeDigitsFound = true;
                    }
                    continue;
                }
                else
                {
                    return false;
                }
            }
            
            Console.WriteLine("IsExpoNumer: {0}", (beforeDigitsFound && !eFound) || (eFound && beforeDigitsFound && afterDigitsFound));
            return (beforeDigitsFound && !eFound) || (eFound && beforeDigitsFound && afterDigitsFound);
        }
    }
    

Log in to reply
 

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