题解 | #表示数值的字符串#

表示数值的字符串

http://www.nowcoder.com/practice/e69148f8528c4039ad89bb2546fd4ff8

同样用有限状态机来进行建模处理,将其转化为一个停机问题,

这种方案的缺点在于需要定义状态空间与输出空间,以及相关的delta函数

优点在于流程的准确性,方便调整,如测试用例将3.e3作为有效的数字,可以直接在delta函数相关部分做少量调整 alt

alt

import java.util.*;


public class Solution {
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     * 
     * @param str string字符串 
     * @return bool布尔型
     */
    public boolean isNumeric (String str) {
        //采用状态判断

        //采用类似状态机的判断机制,delta转换通过代码来完成
        int curStatus = 0;
        char temp;
        int inputType;
        boolean termFlag = false;
        //输入类型 1.空格 2.小数点 3.数字 4.科学计数符号 5.正负符号 6.其他符号
        //前后的空格还是得去掉,以免每一部分都有空格影响
        str = str.trim();
        int strLen = str.length();
        boolean validDigit = false;
        for(int i=0;i<strLen;i++){
            temp = str.charAt(i);
            if(temp==' ') {
                inputType = 1;
            }else if(temp=='.') {
                inputType = 2;
            }else if(temp>='0' && temp<='9'){
                inputType = 3;
            }else if(temp=='e'||temp=='E'){
                inputType = 4;
            }else if(temp=='-'||temp=='+'){
                inputType = 5;
            }else{
                inputType = 6;
            }
            if(inputType==3){
                validDigit = true;
            }
            curStatus = delta(curStatus,inputType);
            switch (curStatus){
                //转换为一个停机判断
                case 8:
                    termFlag = true;
                    break;
            }
            if(termFlag){
                break;
            }
        }
        switch (curStatus){
            //转换为一个停机判断
            case 2:
            case 4:
            case 7:
                return true;
            //结果3.算true
            case 3:
                if(validDigit){
                    return true;
                }
                break;
            case 8:
                return false;
        }
        return false;
    }
    /*
    1.若干空格
    2.一个整数或者小数
    3.(可选)一个 'e' 或 'E' ,后面跟着一个整数(可正可负)
    4.若干空格
     */
    //FSM的核心,状态空间,初始-符号-整数-小数点-整数-科学计数符号-符号-整数 异常
    //输入类型空间 1.空格 2.小数点 3.数字 4.科学计数符号 5.正负符号 6.其他符号
    public int delta(int curStatus,int inputType){
        int nextStatus = curStatus;
        switch(curStatus){
            //初始状态
            case 0:
                switch(inputType) {
                    case 1:
                        nextStatus = 0;
                        break;
                    case 2:
                        nextStatus = 3;
                        break;
                    case 3:
                        nextStatus = 2;
                        break;
                    case 4:
                        nextStatus = 8;
                        break;
                    case 5:
                        nextStatus = 1;
                        break;
                    case 6:
                        nextStatus = 8;
                        break;
                }
                break;
            //符号
            case 1:
                switch(inputType) {
                    case 2:
                        nextStatus = 3;
                        break;
                    case 3:
                        nextStatus = 2;
                        break;
                    case 1:
                    case 4:
                    case 5:
                    case 6:
                        nextStatus = 8;
                        break;
                }
                break;
            //整数1
            case 2:
                switch(inputType) {
                    case 2:
                        nextStatus = 3;
                        break;
                    case 3:
                        nextStatus = 2;
                        break;
                    case 4:
                        nextStatus = 5;
                        break;
                    case 1:
                    case 5:
                    case 6:
                        nextStatus = 8;
                        break;
                }
                break;
            //小数点
            case 3:
                switch(inputType) {
                    case 3:
                        nextStatus = 4;
                        break;
                    //小数点后直接出现e或E
                    case 4:
                        nextStatus = 5;
                        break;
                    case 1:
                    case 2:

                    case 5:
                    case 6:
                        nextStatus = 8;
                        break;
                }
                break;
            //整数2
            case 4:
                switch(inputType) {
                    case 3:
                        nextStatus = 4;
                        break;
                    case 4:
                        nextStatus = 5;
                        break;
                    case 1:
                    case 2:
                    case 5:
                    case 6:
                        nextStatus = 8;
                        break;
                }
                break;
            //科学计数符号
            case 5:
                switch(inputType) {
                    case 3:
                        nextStatus = 7;
                        break;
                    case 5:
                        nextStatus = 6;
                        break;
                    case 1:
                    case 2:
                    case 4:
                    case 6:
                        nextStatus = 8;
                        break;
                }
                break;
            //符号
            case 6:
                switch(inputType) {
                    case 3:
                        nextStatus = 7;
                        break;
                    case 1:
                    case 2:
                    case 4:
                    case 5:
                    case 6:
                        nextStatus = 8;
                        break;
                }
                break;
            //整数
            case 7:
                switch(inputType) {
                    case 3:
                        nextStatus = 7;
                        break;
                    case 1:
                    case 2:
                    case 4:
                    case 5:
                    case 6:
                        nextStatus = 8;
                        break;
                }
                break;

        }
        //System.out.printf("%d-%d-%d\n",curStatus,nextStatus,inputType);
        return nextStatus;

    }
}
全部评论
你真牛逼
点赞 回复
分享
发布于 2022-06-08 16:08

相关推荐

1 收藏 评论
分享
牛客网
牛客企业服务