Clear, fast C# solution in less than 90 lines


  • 0
    P
    public bool IsNumber(string s)
    {
        var trimmed = s.Trim();
        var state = State.Start;
    
        for (var index = 0; index < trimmed.Length; ++index)
        {
            state = GetNewState(state, trimmed[index]);
        }
    
        return (state == State.InInteger) || (state == State.InFraction) || (state == State.InExponent);
    }
    
    private enum State
    {
        Start, OnSign, OnInitialDecimalPoint, InInteger, InFraction, OnExponentSymbol, OnExponentSign, InExponent, Invalid
    }
    
    private static State GetNewState(State state, char current)
    {
        switch (state)
        {
            case State.Start:
                switch (current)
                {
                    case var c when char.IsDigit(c):
                        return State.InInteger;
                    case '+':
                    case '-':
                        return State.OnSign;
                    case '.':
                        return State.OnInitialDecimalPoint;
                    default:
                        return State.Invalid;
                }
            case State.OnSign:
                switch (current)
                {
                    case var c when char.IsDigit(c):
                        return State.InInteger;
                    case '.':
                        return State.OnInitialDecimalPoint;
                    default:
                        return State.Invalid;
                }
            case State.OnInitialDecimalPoint:
                return char.IsDigit(current) ? State.InFraction : State.Invalid;
            case State.InInteger:
                switch (current)
                {
                    case var c when char.IsDigit(c):
                        return State.InInteger;
                    case '.':
                        return State.InFraction;
                    case 'e':
                    case 'E':
                        return State.OnExponentSymbol;
                    default:
                        return State.Invalid;
                }
            case State.InFraction:
                switch (current)
                {
                    case var c when char.IsDigit(c):
                        return State.InFraction;
                    case 'e':
                    case 'E':
                        return State.OnExponentSymbol;
                    default:
                        return State.Invalid;
                }
            case State.OnExponentSymbol:
                switch (current)
                {
                    case var c when char.IsDigit(c):
                        return State.InExponent;
                    case '+':
                    case '-':
                        return State.OnExponentSign;
                    default:
                        return State.Invalid;
                }
            case State.OnExponentSign:
            case State.InExponent:
                return char.IsDigit(current) ? State.InExponent : State.Invalid;
            default:
                return state;
        }
    }
    

Log in to reply
 

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