有效Valid Number .

问题:给出一个字符串,判断它是否是一个有效的数字。

什么叫有效的数字呢?

整数 ,小数 "2.5",正数 "+25",负数"-25",科学计数法"-2e1+0"。

特殊用例:

“.5”, “5.” 认为是有效数字。

无效用例:

“.", "e25", "25e", "2e5e5", "2.5.5", "2e2.5"

输入检查:左右两端出现空格是允许的,内部不能有空格。

  1. class Solution {  
  2. public:  
  3.     bool isNumber(const char *s) {  
  4.         if(s == NULL)  
  5.             return false;  
  6.               
  7.         int ex, dot;  
  8.         ex = dot = 0; // only once   
  9.           
  10.         while(*s == ' '// space   
  11.             s++;  
  12.               
  13.         if(*s == '+' || *s == '-'// sign   
  14.             s++;  
  15.               
  16.         if(isNum(*s)) // first char is numeric   
  17.             s++;  
  18.         else if(*s == '.'// if first char is '.'   
  19.         {  
  20.             s++;  
  21.             dot = 1;  
  22.             if(isNum(*s)) // then second char has to be numeric   
  23.                 s++;  
  24.             else  
  25.                 return false;  
  26.         }  
  27.         else  
  28.             return false;  
  29.           
  30.         while(*s != '\0')  
  31.         {  
  32.             if(*s == '.')  
  33.             {  
  34.                 if(ex == 1 || dot == 1)  
  35.                     return false;  
  36.                 dot = 1;  
  37.             }  
  38.             else if(*s == 'e')  
  39.             {  
  40.                 if(ex == 1)  
  41.                     return false;  
  42.                 ex = 1;  
  43.                 if(*(s+1) == '+' || *(s+1) == '-'//sign for exponent   
  44.                     s++;  
  45.                 if(!isNum(*(s+1))) // numeric after 'e'    
  46.                     return false;  
  47.             }  
  48.             else if(*s == ' ')  // space has to be on the right side   
  49.             {  
  50.                 break;  
  51.             }  
  52.             else if(!isNum(*s)) //other letter   
  53.             {  
  54.                 return false;  
  55.             }  
  56.               
  57.             s++;  
  58.         }  
  59.         while(*s == ' '// space   
  60.             s++;  
  61.         if(*s == '\0')  
  62.             return true;  
  63.         else  
  64.             return false;  
  65.     }  
  66.       
  67.     bool isNum(const char c)  
  68.     {  
  69.         return (c >= '0' && c <= '9'); // 0<=c<=9   
  70.     }  
  71. };  
class Solution {
public:
    bool isNumber(const char *s) {
        if(s == NULL)
            return false;
            
        int ex, dot;
        ex = dot = 0; // only once
        
        while(*s == ' ') // space
            s++;
            
        if(*s == '+' || *s == '-') // sign
            s++;
            
        if(isNum(*s)) // first char is numeric
            s++;
        else if(*s == '.') // if first char is '.'
        {
            s++;
            dot = 1;
            if(isNum(*s)) // then second char has to be numeric
                s++;
            else
                return false;
        }
        else
            return false;
        
        while(*s != '\0')
        {
            if(*s == '.')
            {
                if(ex == 1 || dot == 1)
                    return false;
                dot = 1;
            }
            else if(*s == 'e')
            {
                if(ex == 1)
                    return false;
                ex = 1;
                if(*(s+1) == '+' || *(s+1) == '-') //sign for exponent
                    s++;
                if(!isNum(*(s+1))) // numeric after 'e' 
                    return false;
            }
            else if(*s == ' ')  // space has to be on the right side
            {
                break;
            }
            else if(!isNum(*s)) //other letter
            {
                return false;
            }
            
            s++;
        }
        while(*s == ' ') // space
            s++;
        if(*s == '\0')
            return true;
        else
            return false;
    }
    
    bool isNum(const char c)
    {
        return (c >= '0' && c <= '9'); // 0<=c<=9
    }
};


可以使用Python的内置函数isnumeric()和isdecimal()来判断给定字符串是否为有效数字。isnumeric()函数用于判断字符串是否只包含数字字符,包括全角字符和汉字数字。isdecimal()函数用于判断字符串是否只包含十进制字符。 下面是一个判断给定字符串是否为有效数字的函数: ```python def is_valid_number(s): if s.isnumeric() or (s[0] == '-' and s[1:].isnumeric()) or (s[0] == '-' and s[1:].replace('.', '', 1).isnumeric()) or (s[0] == ' ' and s[1:].isnumeric()) or (s.count('.') == 1 and s.replace('.', '', 1).isnumeric()) or (s.count('e') == 1 and s.split('e')[0].replace('.', '', 1).isnumeric() and s.split('e')[1].isnumeric()) or (s.count('E') == 1 and s.split('E')[0].replace('.', '', 1).isnumeric() and s.split('E')[1].isnumeric()): return True else: return False ``` 这个函数首先使用isnumeric()函数判断字符串是否只包含数字字符。然后使用一系列条件判断字符串是否为有效数字,包括带负号的数字、带小数点的数字、带空格的数字、指数表示的数字等等。 测试代码如下: ```python numbers = ["2", "089", "-0.1", " 3.14", "2e10", "3E-7", "abc", "1.2.3", "-"] for number in numbers: if is_valid_number(number): print(f"{number} is a valid number.") else: print(f"{number} is not a valid number.") ``` 输出结果: ``` 2 is a valid number. 089 is a valid number. -0.1 is a valid number. 3.14 is a valid number. 2e10 is a valid number. 3E-7 is a valid number. abc is not a valid number. 1.2.3 is not a valid number. - is not a valid number. ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值