• 每日刷题打卡Day21


    题目概览

    在这里插入图片描述

    题解

    按照字符串从左到右的顺序,定义以下 9 种状态。

    1. 开始的空格
    2. 幂符号前的正负号
    3. 小数点前的数字
    4. 小数点、小数点后的数字
    5. 当小数点前为空格时,小数点、小数点后的数字
    6. 幂符号
    7. 幂符号后的正负号
    8. 幂符号后的数字
    9. 结尾的空格

    并且把相应的一些情况进行排除,比如不能两个-+连在一起,e前后都必须要有数字,e后不能有小数点之类的。

    Code

    class Solution {
    private:
        // 整数的格式可以用[+|-]B表示, 其中B为无符号整数
        bool scanInteger(const string s, int& index){
    
            if(s[index] == '+' || s[index] == '-')
                ++index;
    
            return scanUnsignedInteger(s, index);
        }
        
        bool scanUnsignedInteger(const string s, int& index){
    
            int befor = index;
            while(index != s.size() && s[index] >= '0' && s[index] <= '9')
                index ++;
    
            return index > befor;
        }
    public:
        // 数字的格式可以用A[.[B]][e|EC]或者.B[e|EC]表示,
        // 其中A和C都是整数(可以有正负号,也可以没有),而B是一个无符号整数
        bool isNumber(string s) {
    
            if(s.size() == 0)
                return false;
            int index = 0;
    
            //字符串开始有空格,可以返回true
            while(s[index] == ' ')  //书中代码没有该项测试
                ++index;
    
            bool numeric = scanInteger(s, index);
    
            // 如果出现'.',接下来是数字的小数部分
            if(s[index] == '.'){
    
                ++index;
    
                // 下面一行代码用||的原因:
                // 1. 小数可以没有整数部分,例如.123等于0.123;
                // 2. 小数点后面可以没有数字,例如233.等于233.0;
                // 3. 当然小数点前面和后面可以有数字,例如233.666
                numeric = scanUnsignedInteger(s, index) || numeric;
            }
    
            // 如果出现'e'或者'E',接下来跟着的是数字的指数部分
            if(s[index] == 'e' || s[index] == 'E'){
    
                ++index;
    
                // 下面一行代码用&&的原因:
                // 1. 当e或E前面没有数字时,整个字符串不能表示数字,例如.e1、e1;
                // 2. 当e或E后面没有整数时,整个字符串不能表示数字,例如12e、12e+5.4
                numeric = numeric && scanInteger(s ,index);
            }
    
            //字符串结尾有空格,可以返回true
            while(s[index] == ' ')
                ++index;
            cout << s.size() << " " << index;   //调试用
    
            return numeric && index == s.size();
        }
    };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    class Solution {
        public boolean isNumber(String s) {
            if(s == null || s.length() == 0) return false; // s为空对象或 s长度为0(空字符串)时, 不能表示数值
            boolean isNum = false, isDot = false, ise_or_E = false; // 标记是否遇到数位、小数点、‘e’或'E'
            char[] str = s.trim().toCharArray();  // 删除字符串头尾的空格,转为字符数组,方便遍历判断每个字符
            for(int i=0; i<str.length; i++) {
                if(str[i] >= '0' && str[i] <= '9') isNum = true; // 判断当前字符是否为 0~9 的数位
                else if(str[i] == '.') { // 遇到小数点
                    if(isDot || ise_or_E) return false; // 小数点之前可以没有整数,但是不能重复出现小数点、或出现‘e’、'E'
                    isDot = true; // 标记已经遇到小数点
                }
                else if(str[i] == 'e' || str[i] == 'E') { // 遇到‘e’或'E'
                    if(!isNum || ise_or_E) return false; // ‘e’或'E'前面必须有整数,且前面不能重复出现‘e’或'E'
                    ise_or_E = true; // 标记已经遇到‘e’或'E'
                    isNum = false; // 重置isNum,因为‘e’或'E'之后也必须接上整数,防止出现 123e或者123e+的非法情况
                }
                else if(str[i] == '-' ||str[i] == '+') { 
                    if(i!=0 && str[i-1] != 'e' && str[i-1] != 'E') return false; // 正负号只可能出现在第一个位置,或者出现在‘e’或'E'的后面一个位置
                }
                else return false; // 其它情况均为不合法字符
            }
            return isNum;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24

    结果

    在这里插入图片描述

  • 相关阅读:
    代码交付自动化 4项非常重要
    Transformer13~目标检测算法汇总
    【极客时间2】左耳听风
    工程水文学复习资料
    乒乓球发球技巧
    已分割的视频怎么合并
    C++对象模型学习笔记
    创新无处不在的便利体验——基于智能视频和语音技术的安防监控系统EasyCVR
    猿创征文|Java中的IO流大家族 (两万字详解)
    数据结构与算法之从前序与中序遍历序列构造二叉树
  • 原文地址:https://blog.csdn.net/weixin_44673253/article/details/126329521