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 };

  • 相关阅读:
    Github账户注册的过程
    目前流行的源程序版本管理软件和项目管理软件都有哪些, 各有什么优缺点?
    作业二:四则运算
    学习进度
    对构建之法的一些问题
    个人介绍
    对《软件工程》课程的总结
    作业八 更新版
    作业八
    冲刺总结博客
  • 原文地址:https://www.cnblogs.com/lxd2502/p/4245538.html
Copyright © 2011-2022 走看看