• [leetcode] Valid Number


    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 class Solution
     2 {
     3 public:
     4   bool isNumber(const char *s)
     5   {
     6     bool has_front_num = false, has_back_num = false, has_e = false;
     7     int len = strlen(s);
     8     int i = 0;
     9 
    10     while(i < len && ' ' == s[i])
    11       i++;
    12 
    13     if(i == len) return false;
    14 
    15     if(i < len && (s[i] == '+' || s[i] == '-'))
    16       i++;
    17 
    18     while(i < len && isdigit(s[i]))
    19     {
    20       i++;
    21       has_front_num = true;
    22     }
    23 
    24     if(i < len && s[i] == '.')
    25       i++;
    26    
    27     while(i < len && isdigit(s[i]))
    28     {
    29       i++;
    30       has_front_num = true;
    31     }
    32 
    33     if(i < len && (s[i] == 'e' || s[i] == 'E') && has_front_num)
    34     {
    35       i++;
    36       has_e = true;
    37       if(i == len) return false;
    38     }
    39 
    40    
    41     if(i < len && (s[i] == '+' || s[i] == '-') && has_e)
    42       i++;
    43    
    44     while(i < len && isdigit(s[i]) && has_e)
    45     {
    46       i++;
    47       has_back_num = true;
    48     }
    49 
    50     while(i < len && s[i] == ' ')
    51       i++;
    52 
    53     if(i == len && has_e && has_back_num)
    54       return true;
    55     else if(i == len && !has_back_num && !has_e && has_front_num)
    56       return true;
    57 
    58     return false;
    59   }
    60 };

  • 相关阅读:
    fescar源码解析系列(一)之启动详解
    dubbo源码解析二 invoker链
    dubbo源码解析一
    CSP-S 2021 游记
    使用SpEL记录操作日志的详细信息
    Router 重定向和别名是什么?
    vue项目做seo(prerender-spa-plugin预渲染)
    vue3.0初体验有哪些实用新功能
    uniapp弹窗踩坑
    Spring boot application.properties 配置
  • 原文地址:https://www.cnblogs.com/lxd2502/p/4245538.html
Copyright © 2020-2023  润新知