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 };
  • 相关阅读:
    C# sqlhelp
    vs2015 C#打包程序为exe
    python3.6安装docx模块
    python 第八天
    python 第七天
    python 选课系统
    python 第六天
    python 模拟实现一个ATM + 购物商城程序
    python 计算器
    python 第五天
  • 原文地址:https://www.cnblogs.com/easonliu/p/3699525.html
Copyright © 2011-2022 走看看