美文网首页
【Leetcode2017.5.18】65. Valid Num

【Leetcode2017.5.18】65. Valid Num

作者: 三生石上绛珠草 | 来源:发表于2017-05-19 11:10 被阅读16次
    1. Valid Number
      The idea is pretty straightforward. A valid number is composed of the significand and the exponent (which is optional). As we go through the string, do the following things one by one:

    skip the leading whitespaces;
    check if the significand is valid. To do so, simply skip the leading sign and count the number of digits and the number of points. A valid significand has no more than one point and at least one digit.
    check if the exponent part is valid. We do this if the significand is followed by 'e'. Simply skip the leading sign and count the number of digits. A valid exponent contain at least one digit.
    skip the trailing whitespaces. We must reach the ending 0 if the string is a valid number.

    bool isNumber(const char *s) 
    {
        int i = 0;
        
        // skip the whilespaces
        for(; s[i] == ' '; i++) {}
        
        // check the significand
        if(s[i] == '+' || s[i] == '-') i++; // skip the sign if exist
        
        int n_nm, n_pt;
        for(n_nm=0, n_pt=0; (s[i]<='9' && s[i]>='0') || s[i]=='.'; i++)
            s[i] == '.' ? n_pt++:n_nm++;       
        if(n_pt>1 || n_nm<1) // no more than one point, at least one digit
            return false;
        
        // check the exponent if exist
        if(s[i] == 'e') {
            i++;
            if(s[i] == '+' || s[i] == '-') i++; // skip the sign
            
            int n_nm = 0;
            for(; s[i]>='0' && s[i]<='9'; i++, n_nm++) {}
            if(n_nm<1)
                return false;
        }
        
        // skip the trailing whitespaces
        for(; s[i] == ' '; i++) {}
        
        return s[i]==0;  // must reach the ending 0 of the string
    }
    

    相关文章

      网友评论

          本文标题:【Leetcode2017.5.18】65. Valid Num

          本文链接:https://www.haomeiwen.com/subject/fmvsxxtx.html