zoukankan      html  css  js  c++  java
  • [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 };

  • 相关阅读:
    Java面向对象之封装静态
    分布式平台Spark环境的搭建
    高斯混合模型
    异常排除: 调用方未由服务进行身份验证
    HttpClient介绍和简单使用流程
    阿里短信服务的使用流程
    笔记工具选择
    特效图文制作
    语言基础(23):智能指针
    无线通信基础(一):无线网络演进
  • 原文地址:https://www.cnblogs.com/lxd2502/p/4245538.html
Copyright © 2011-2022 走看看