zoukankan      html  css  js  c++  java
  • #Leetcode# 65. Valid Number

    https://leetcode.com/problems/valid-number/

    Validate if a given string can be interpreted as a decimal number.

    Some examples:
    "0" => true
    " 0.1 " => true
    "abc" => false
    "1 a" => false
    "2e10" => true
    " -90e3   " => true
    " 1e" => false
    "e3" => false
    " 6e-1" => true
    " 99e2.5 " => false
    "53.5e93" => true
    " --6 " => false
    "-+3" => false
    "95a54e53" => false

    Note: It is intended for the problem statement to be ambiguous. You should gather all requirements up front before implementing one. However, here is a list of characters that can be in a valid decimal number:

    • Numbers 0-9
    • Exponent - "e"
    • Positive/negative sign - "+"/"-"
    • Decimal point - "."

    Of course, the context of these characters also matters in the input.

    Update (2015-02-10):
    The signature of the C++ function had been updated. If you still see your function signature accepts a const char * argument, please click the reload button to reset your code definition.

    代码:

    class Solution {
    public:
        bool isNumber(string s) {
            bool num = false, numAfterE = true, dot = false, exp = false, sign = false;
            int n = s.size();
            for (int i = 0; i < n; ++i) {
                if (s[i] == ' ') {
                    if (i < n - 1 && s[i + 1] != ' ' && (num || dot || exp || sign)) return false;
                } else if (s[i] == '+' || s[i] == '-') {
                    if (i > 0 && s[i - 1] != 'e' && s[i - 1] != ' ') return false;
                    sign = true;
                } else if (s[i] >= '0' && s[i] <= '9') {
                    num = true;
                    numAfterE = true;
                } else if (s[i] == '.') {
                    if (dot || exp) return false;
                    dot = true;
                } else if (s[i] == 'e') {
                    if (exp || !num) return false;
                    exp = true;
                    numAfterE = false;
                } else return false;
            }
            return num && numAfterE;
        }
    };
    

       这道题 杀了我吧

  • 相关阅读:
    bzoj1607: [Usaco2008 Dec]Patting Heads 轻拍牛头
    bzoj1016: [JSOI2008]最小生成树计数
    bzoj1051: [HAOI2006]受欢迎的牛
    bzoj1003: [ZJOI2006]物流运输
    bzoj1079: [SCOI2008]着色方案
    bzoj1179: [Apio2009]Atm
    bzoj1877: [SDOI2009]晨跑
    bzoj1821: [JSOI2010]Group 部落划分 Group
    bzoj1305: [CQOI2009]dance跳舞
    bzoj1858: [Scoi2010]序列操作
  • 原文地址:https://www.cnblogs.com/zlrrrr/p/10686180.html
Copyright © 2011-2022 走看看