• LeetCode | Valid Number


    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.

    再加几个测试用例:

    "1." => true

    ".3" => true

    "." => false

    "1e+10" =>true

    "1e0.6" =>false

    "1e" => false

    "e1" => false

    "e" => false

    "   1   " => true

     1 class Solution {
     2 public:
     3     bool isNumber(const char *s) {
     4         while (*s != '' && *s == ' ') s++; // 左侧空白
     5         
     6         int n = strlen(s);
     7         if (n == 0) return false; // 空串
     8         
     9         // 正负号
    10         if (s[0] == '+' || s[0] == '-') {
    11             s++; 
    12         }
    13         
    14         
    15         int e = 0, dot = 0;
    16         int elnum = 0, ernum = 0, num = 0, dlnum = 0, drnum = 0;
    17         while (*s != '') {
    18             if (*s < '0' || *s > '9') {
    19                 if (*s == 'e' || *s == 'E') { 
    20                     e++;
    21                     if (e == 1 && *(s + 1) != '' && (*(s + 1) == '-' || *(s + 1) == '+')) { //1e-10, 1e+10,e的后面允许有一个符号
    22                         s++;
    23                     }
    24                     if (e > 1) return false; // 只能有一个e. 1e10, 1e-10
    25                 } else if (*s == '.') {
    26                     dot++;
    27                     if (dot > 1) return false; // 只能有一个小数点
    28                     if (e == 1) return false; // 如果在e的后面,不允许有小数点
    29                     
    30                 } else {
    31                     break; // 其他字符不合法
    32                 }
    33             } else {
    34                 num++;
    35                 if (e == 0) elnum++; //统计e左右的数字个数
    36                 else ernum++;
    37                 
    38                 if (dot == 0) dlnum++; //统计小数点左右的数字个数
    39                 else drnum++;  
    40             }
    41             s++;
    42         }
    43         if (e == 1 && (elnum ==0 || ernum == 0)) return false; // 如果e的左或右没有数字 e10, 1e不合法
    44         if (dot == 1 && dlnum == 0 && drnum == 0) return false; // 如果小数点左右都没有数字,1. 合法,.3合法,但是.不合法。
    45         
    46         while (*s != '' && *s == ' ') s++; //忽略右边空格
    47         if (*s != '') return false; // 如果右边空格之后还有字符,比如"1e10  a"        
    48         
    49         return num > 0;
    50     }
    51 };

    第二遍写的。

    要统计的东西:

    1. e左边的数字;

    2. e右边的数字;

    3. dot左边的数字;

    4. dot右边的数字;

    5. e和dot的次数;

    +-可以直接处理;

     1 class Solution {
     2 public:
     3     bool isNumber(const char *s) {
     4         while (*s && *s == ' ') s++;
     5         if (!*s) return false;
     6         
     7         if (*s == '+' || *s == '-') s++;
     8         
     9         int e = 0, dot = 0, ld = 0, rd = 0, re = 0, digit = 0; 
    10         while (*s && *s != ' ') {
    11             if (*s >= '0' && *s <= '9') {
    12                 digit++;
    13                 rd++;
    14                 re++;
    15             } else if (*s == '.') {
    16                 ld = digit;
    17                 rd = 0;
    18                 dot++;
    19                 if (dot > 1) return false;
    20                 if (e > 0) return false;
    21             } else if (*s == 'e') {
    22                 e++;
    23                 if (e > 1) return false;
    24                 if (digit == 0) return false;
    25                 re = 0;
    26                 if (*(s+1) && (*(s+1) == '+' || *(s+1) == '-')) s++;
    27             } else {
    28                 return false;
    29             }
    30             s++;
    31         }
    32         if (dot> 0 && ld == 0 && rd == 0) return false;
    33         if (e > 0 && re == 0) return false;
    34         
    35         while (*s && *s == ' ') s++;
    36         return !*s;
    37     }
    38 };
  • 相关阅读:
    Stm32设置串口300波特率
    STM32F103ZET6移植FreeRTOS过程
    什么时候该用裸机?什么时候该用RTOS?
    又到了立flag时间
    关于掉电数据保存的心得
    一个教训
    下个月回国给自己定目标
    GPRS模块/4G开发过程
    ftp登陆失败,check pass; user unknown
    python深浅拷贝
  • 原文地址:https://www.cnblogs.com/linyx/p/3656091.html
Copyright © 2020-2023  润新知