zoukankan      html  css  js  c++  java
  • [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 class Solution {
     2 public:
     3     bool isNumber(const char *s) {
     4         while (*s == ' ') ++s;
     5         while (*s == '+' || *s == '-') ++s;
     6         bool exp = false, space = false, point = false;
     7         bool number = false;
     8         while (*s != '') {
     9             if (isdigit(*s)) {
    10                 if (space) return false;
    11                 else number = true;
    12             } else if (*s == '.') {
    13                 if (!point && !space && !exp) {
    14                     point = true;
    15                 } else {
    16                     return false;
    17                 }
    18             } else if (*s == 'e') {
    19                 if (!exp && number && !space) {
    20                     exp = true;
    21                     number = false;
    22                     while (*(s+1) == '+' || *(s+1) == '-') ++s;
    23                 } else {
    24                     return false;
    25                 }
    26             } else if (*s == ' ') {
    27                 if (!space) {
    28                     space = true;
    29                 }
    30             } else {
    31                 return false;
    32             }
    33             ++s;
    34         }
    35         return number;
    36     }
    37 };
  • 相关阅读:
    jQuery 2.0.3 源码分析 回调对象
    JQuery+JQuery ui实现的弹出窗口+遮罩层+拖动+更改大小~!
    2019.8.25 小结
    2019.8.23 小结
    宜中食堂游记
    2019.8.21小结
    2019.8.22小结
    2019.8.19小结
    题解 CF499A 【Watching a movie】
    2019.8.18小结
  • 原文地址:https://www.cnblogs.com/easonliu/p/3699525.html
Copyright © 2011-2022 走看看