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

表示数值的字符串

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

画dfa解的,比正则块一毫秒😁

class Solution {
  public:
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     *
     * @param str string字符串
     * @return bool布尔型
     */
    bool isNumeric(string str) {
        if (str.empty())return false;
        int state = 0;
        for (int i = 0; i < str.size(); i++) {
            switch (state) {
                case 0:
                    if (str[i] == '+' || str[i] == '-') {
                        state = 1;
                        break;
                    } else if (str[i] == ' ') {
                        break;
                    } else if (str[i] == '.') {
                        state = 9;
                        break;
                    } else if (isdigit(str[i])) {
                        state = 2;
                        break;
                    } else return false;
                case 1:
                    if (isdigit(str[i])) {
                        state = 2;
                        break;
                    } else if (str[i] == '.') {
                        state = 9;
                        break;
                    } else return false;
                case 2:
                    if (isdigit(str[i])) {
                        state = 2;
                        break;
                    }else if (str[i] == ' ') {
                        state = 8;
                        break;
                    } else if (str[i] == 'e' || str[i] == 'E') {
                        state = 5;
                        break;
                    } else if (str[i] == '.') {
                        state = 3;
                        break;
                    } else return false;
                case 3:
                    if (isdigit(str[i])) {
                        state = 4;
                        break;
                    }else if (str[i] == 'e' || str[i] == 'E') {
                        state = 5;
                        break;
                    } else return false;
                case 4:
                    if (isdigit(str[i])) {
                        break;
                    } else if (str[i] == 'e' || str[i] == 'E') {
                        state = 5;
                        break;
                    } else return false;
                case 5:
                    if (isdigit(str[i])) {
                        state = 7;
                        break;
                    } else if (str[i] == '+' || str[i] == '-') {
                        state = 6;
                        break;
                    } else return false;
                case 6:
                    if (isdigit(str[i])) {
                        state = 7;
                        break;
                    } else return false;
                case 7:
                    if (isdigit(str[i])) {
                        break;
                    } else if (str[i] == ' ') {
                        state = 8;
                        break;
                    } else return false;
                case 8:
                    if (str[i] == ' ') {
                        break;
                    } else return false;
                case 9:
                    if (isdigit(str[i])) {
                        state = 4;
                        break;
                    } else return false;
            }
        }
        // write code here
        return state == 2 || state == 4 || state == 7 || state == 8 || state == 3;
    }
};

全部评论

相关推荐

10-31 14:54
已编辑
门头沟学院 算法工程师
点赞 评论 收藏
分享
10-29 15:38
门头沟学院 Java
榕城小榕树:难道你简历里写了配送路径优化算法?
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务