【leetcode】Valid Number

Question : 

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.

Anwser 1 :     

class Solution {
public:
    bool isNumber(const char *s) {
        // Start typing your C/C++ solution below
        // DO NOT write int main() function
        
        if (s == NULL)
             return false;
             
         while(isspace(*s))
             s++;
             
         if (*s == '+' || *s == '-')
             s++;
             
         bool eAppear = false;
         bool dotAppear = false;
         bool firstPart = false;
         bool secondPart = false;
         bool spaceAppear = false;
         while(*s != '\0')
         {
             if (*s == '.')
             {
                 if (dotAppear || eAppear || spaceAppear)
                     return false;
                 else
                     dotAppear = true;
             }
             else if (*s == 'e' || *s == 'E')
             {
                 if (eAppear || !firstPart || spaceAppear)
                     return false;
                 else
                     eAppear = true;
             }
             else if (isdigit(*s))
             {
                 if (spaceAppear)
                     return false;
                     
                 if (!eAppear)
                     firstPart = true;
                 else
                     secondPart = true;
             }
             else if (*s == '+' || *s == '-')   // behind of e/E
             {
                 if (spaceAppear)
                     return false;
                     
                 if (!eAppear || !(*(s-1) == 'e' || *(s-1) == 'E'))
                     return false;
             }
             else if (isspace(*s))
                 spaceAppear = true;
             else
                 return false;
                 
             s++;            
         }
         
         if (!firstPart) {
             return false;
         }  else if (eAppear && !secondPart) {
             return false;
         } 
         return true;  
    }
};


Anwser 2 :     

class Solution {
public:
    bool isNumber(const char *s) {
        // Start typing your C/C++ solution below
        // DO NOT write int main() function
        
        int mat[11][7] = {
                      0 ,0 ,0 ,0 ,0 ,0 ,0,      // false
                      0 ,2 ,3 ,0 ,1 ,4 ,0,      // 1
                      0 ,2 ,5 ,6 ,9 ,0 ,10,     // 2
                      0 ,5 ,0 ,0 ,0 ,0 ,0,      // 3
                      0 ,2 ,3 ,0 ,0 ,0 ,0,      // 4
                      0 ,5 ,0 ,6 ,9 ,0 ,10,     // 5
                      0 ,7 ,0 ,0 ,0 ,8 ,0,      // 6
                      0 ,7 ,0 ,0 ,9 ,0 ,10,     // 7
                      0 ,7 ,0 ,0 ,0 ,0 ,0,      // 8
                      0 ,0 ,0 ,0 ,9 ,0 ,10,     // 9
                      10,10,10,10,10,10,10      // 10
                    };
                    
        int i = 0;
        int stat = 1;
        while(s[i] != '\0') 
        {
            int type = 0;
            if(s[i] >= '0' && s[i] <= '9')
                type = 1;
            else if(s[i] == '.')
                type = 2;
            else if(s[i] == 'e')
                type = 3;
            else if(s[i] == ' ')
                type = 4;
            else if(s[i] == '+' || s[i] == '-')
                type = 5;
            if(stat == 0)
                return false;
                
            stat = mat[stat][type];
            i++;
        }
        stat = mat[stat][6];
        if(stat == 10) {
            return true;
        }
        
        return false;
    }
};



参考推荐:

Valid Number


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值