65. Valid Number

来源:互联网 发布:马云在贵州大数据 编辑:程序博客网 时间:2024/05/19 12:26
原题:

Validate if a given string is numeric.

Some examples:
"0" => true
" 0.1 " => true
"abc" => false
"1 a" => false
"2e10" => true

Note: It is intended for the problem statement to be ambiguous. You should gather all requirements up front before implementing one.

Update (2015-02-10):
The signature of the C++ function had been updated. If you still see your function signature accepts aconst char * argument, please click the reload button to reset your code definition.


思考过程:

一开始还各种分情况判断各种试,后来看到有优秀的人用自动机解决的。于是用自动机解决这个问题。


解题思路:

先把首尾的空格去除,然后遍历字符串跟着自动机走。自动机结构从代码里看比较明显(changeState函数输入的三个参数分别是:遇到的字符类型(数字、点还是'e')、字符、转换后的状态)。


AC代码::

public static int DIGIT = 0;    public static int SIGN = 1;    public static int DOT = 2;    public static int EXP = 3;    int state = 0;    public boolean isNumber(String s) {        int begin = 0,end = s.length() - 1;//state标记状态        while (begin <= end && s.charAt(begin) == ' ') begin++;//处理前面空格        while (end >= begin && s.charAt(end) == ' ') end--;//处理后面空格        while (begin <= end) {            char c = s.charAt(begin);            switch (state) {                case 0: {                    if (!changeState(DIGIT, c, 2) && !changeState(SIGN, c, 1) && !changeState(DOT, c, 3))                        return false;                    break;                }                case 1: {                    if (!changeState(DIGIT, c, 2) && !changeState(DOT, c, 3))                        return false;                    break;                }                case 2:{                    if (!changeState(DIGIT,c,2) && !changeState(EXP,c,5) && !changeState(DOT,c,4))                        return false;                    break;                }                case 3:{                    if (!changeState(DIGIT,c,4))                        return false;                    break;                }                case 4:{                    if (!changeState(DIGIT,c,4) && !changeState(EXP,c,5))                        return false;                    break;                }                case 5:{                    if (!changeState(DIGIT,c,7) && !changeState(SIGN,c,6))                        return false;                    break;                }                case 6:{                    if (!changeState(DIGIT,c,7))                        return false;                    break;                }                case 7:{                    if (!changeState(DIGIT,c,7))                        return false;                }            }            begin++;        }        return (state == 4 || state == 7 || state == 2);//是否在自动机终点集    }    public boolean changeState(int type,char c,int newState){//用于转换自动机状态,属于代码复用        switch (type){            case 0: {                if (c >= '0' && c <= '9') {                    state = newState;                    return true;                }                return false;            }            case 1:{                if (c == '+' || c == '-'){                    state = newState;                    return true;                }                return false;            }            case 2:{                if (c == '.'){                    state = newState;                    return true;                }                return false;            }            case 3:{                if (c == 'e'){                    state = newState;                    return true;                }                return false;            }        }        return false;    }