• leetcode记录——65. 有效数字


    有效数字(按顺序)可以分成以下几个部分:

    1. 一个 小数 或者 整数
    2. (可选)一个 'e' 或 'E' ,后面跟着一个 整数

    小数(按顺序)可以分成以下几个部分:

    1. (可选)一个符号字符('+' 或 '-'
    2. 下述格式之一:
      1. 至少一位数字,后面跟着一个点 '.'
      2. 至少一位数字,后面跟着一个点 '.' ,后面再跟着至少一位数字
      3. 一个点 '.' ,后面跟着至少一位数字

    整数(按顺序)可以分成以下几个部分:

    1. (可选)一个符号字符('+' 或 '-'
    2. 至少一位数字

    部分有效数字列举如下:

    • ["2", "0089", "-0.1", "+3.14", "4.", "-.9", "2e10", "-90E3", "3e+7", "+6e-1", "53.5e93", "-123.456e789"]

    部分无效数字列举如下:

    • ["abc", "1a", "1e", "e3", "99e2.5", "--6", "-+3", "95a54e53"]

    给你一个字符串 s ,如果 s 是一个 有效数字 ,请返回 true 。

    示例 1:

    输入:s = "0"
    输出:true
    

    示例 2:

    输入:s = "e"
    输出:false
    

    示例 3:

    输入:s = "."
    输出:false
    

    示例 4:

    输入:s = ".1"
    输出:true
    

    提示:

    • 1 <= s.length <= 20
    • s 仅含英文字母(大写和小写),数字(0-9),加号 '+' ,减号 '-' ,或者点 '.' 。

    题解:

    方法1:分类讨论

    class Solution {
        public boolean isInteger(String s){
            if(s.length()==0)
                return false;
            int i=0;
            boolean ints=false;
            if(s.charAt(0)=='+'||s.charAt(0)=='-'){
                i+=1;
            }
            for(;i<s.length();++i){
                if(s.charAt(i)<'0'||s.charAt(i)>'9')
                    return false;
                ints=true;
            }
            return ints;
        }
        public boolean isFloat(String s){
            if(s.length()==0)
                return false;
            boolean floats=false;
            boolean num=false;
            int i=0;
            if(s.charAt(0)=='+'||s.charAt(0)=='-'){
                i+=1;
            }
            for(;i<s.length();++i){
                if(s.charAt(i)=='.'&&floats==false){
                    floats=true;
                    continue;
                }
                if(s.charAt(i)<'0'||s.charAt(i)>'9')
                    return false;
                num=true;
            }
            return num;
        }
        public boolean isNumber(String s) {
            if(s.length()==0)
                return false;
            String str=s.toLowerCase();
            String[] temp1 = str.split("e");
            if(temp1.length>=3||temp1.length<=0)
            {
                return false;
            }else{
                if(temp1.length==1){
                    if(temp1[0].length()==s.length())
                        return (isFloat(temp1[0])||isInteger(temp1[0]));
                    else
                        return false;
                }else{
                    if(str.charAt(s.length()-1)=='e')
                    return false;
                    return (isFloat(temp1[0])||isInteger(temp1[0]))&&isInteger(temp1[1]);
                }
                    
            }
        }
    }

    方法2:有限状态机

    class Solution {
    public boolean isNumber(String s) {
    Map<State, Map<CharType, State>> transfer = new HashMap<State, Map<CharType, State>>();
    Map<CharType, State> initialMap = new HashMap<CharType, State>() {{
    put(CharType.CHAR_NUMBER, State.STATE_INTEGER);
    put(CharType.CHAR_POINT, State.STATE_POINT_WITHOUT_INT);
    put(CharType.CHAR_SIGN, State.STATE_INT_SIGN);
    }};
    transfer.put(State.STATE_INITIAL, initialMap);
    Map<CharType, State> intSignMap = new HashMap<CharType, State>() {{
    put(CharType.CHAR_NUMBER, State.STATE_INTEGER);
    put(CharType.CHAR_POINT, State.STATE_POINT_WITHOUT_INT);
    }};
    transfer.put(State.STATE_INT_SIGN, intSignMap);
    Map<CharType, State> integerMap = new HashMap<CharType, State>() {{
    put(CharType.CHAR_NUMBER, State.STATE_INTEGER);
    put(CharType.CHAR_EXP, State.STATE_EXP);
    put(CharType.CHAR_POINT, State.STATE_POINT);
    }};
    transfer.put(State.STATE_INTEGER, integerMap);
    Map<CharType, State> pointMap = new HashMap<CharType, State>() {{
    put(CharType.CHAR_NUMBER, State.STATE_FRACTION);
    put(CharType.CHAR_EXP, State.STATE_EXP);
    }};
    transfer.put(State.STATE_POINT, pointMap);
    Map<CharType, State> pointWithoutIntMap = new HashMap<CharType, State>() {{
    put(CharType.CHAR_NUMBER, State.STATE_FRACTION);
    }};
    transfer.put(State.STATE_POINT_WITHOUT_INT, pointWithoutIntMap);
    Map<CharType, State> fractionMap = new HashMap<CharType, State>() {{
    put(CharType.CHAR_NUMBER, State.STATE_FRACTION);
    put(CharType.CHAR_EXP, State.STATE_EXP);
    }};
    transfer.put(State.STATE_FRACTION, fractionMap);
    Map<CharType, State> expMap = new HashMap<CharType, State>() {{
    put(CharType.CHAR_NUMBER, State.STATE_EXP_NUMBER);
    put(CharType.CHAR_SIGN, State.STATE_EXP_SIGN);
    }};
    transfer.put(State.STATE_EXP, expMap);
    Map<CharType, State> expSignMap = new HashMap<CharType, State>() {{
    put(CharType.CHAR_NUMBER, State.STATE_EXP_NUMBER);
    }};
    transfer.put(State.STATE_EXP_SIGN, expSignMap);
    Map<CharType, State> expNumberMap = new HashMap<CharType, State>() {{
    put(CharType.CHAR_NUMBER, State.STATE_EXP_NUMBER);
    }};
    transfer.put(State.STATE_EXP_NUMBER, expNumberMap);

    int length = s.length();
    State state = State.STATE_INITIAL;

    for (int i = 0; i < length; i++) {
    CharType type = toCharType(s.charAt(i));
    if (!transfer.get(state).containsKey(type)) {
    return false;
    } else {
    state = transfer.get(state).get(type);
    }
    }
    return state == State.STATE_INTEGER || state == State.STATE_POINT || state == State.STATE_FRACTION || state == State.STATE_EXP_NUMBER || state == State.STATE_END;
    }

    public CharType toCharType(char ch) {
    if (ch >= '0' && ch <= '9') {
    return CharType.CHAR_NUMBER;
    } else if (ch == 'e' || ch == 'E') {
    return CharType.CHAR_EXP;
    } else if (ch == '.') {
    return CharType.CHAR_POINT;
    } else if (ch == '+' || ch == '-') {
    return CharType.CHAR_SIGN;
    } else {
    return CharType.CHAR_ILLEGAL;
    }
    }

    enum State {
    STATE_INITIAL,
    STATE_INT_SIGN,
    STATE_INTEGER,
    STATE_POINT,
    STATE_POINT_WITHOUT_INT,
    STATE_FRACTION,
    STATE_EXP,
    STATE_EXP_SIGN,
    STATE_EXP_NUMBER,
    STATE_END
    }

    enum CharType {
    CHAR_NUMBER,
    CHAR_EXP,
    CHAR_POINT,
    CHAR_SIGN,
    CHAR_ILLEGAL
    }
    }

    方法3:正则匹配

    class Solution {
    public:
        static const regex pattern;
    
        bool isNumber(string str) {
            return regex_match(str, pattern);
        }
    };
    
    const regex Solution::pattern("[+-]?(?:\d+\.?\d*|\.\d+)(?:[Ee][+-]?\d+)?");
  • 相关阅读:
    华为云招募工业智能领域合作伙伴,强力扶持+商业变现
    “不敢去怀疑代码,又不得不怀疑代码”记一次网络请求超时分析
    详解openGauss多线程架构启动过程
    站在数字化风口,工装企业如何“飞起来”
    如何化解35岁危机?华为云数据库首席架构师20年技术经验分享
    解读2022年度敏捷教练行业现状报告
    理论+案例,带你掌握Angular依赖注入模式的应用
    机器学习实践:基于支持向量机算法对鸢尾花进行分类
    8种桌面IDE CodeArts智能代码补全类型
    【CVPR2022】用于域适应语义分割的域无关先验
  • 原文地址:https://www.cnblogs.com/big-zoo/p/14892431.html
Copyright © 2020-2023  润新知