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.(题目标签: 笔试题,面试题,php面试题,java面试题,阿里巴巴面试题,腾讯面试题,小米面试题,区块链面试题,字符串)"/>
经典指数          
原因
1064
浏览数
0
收藏数
 

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 { public: bool isNumber(const char *str) { int zhuang[9][6]={{-1,0,1,2,-1,3}, {-1,-1,-1,2,-1,3}, {-1,-1,-1,-1,-1,4}, {-1,5,-1,4,6,3}, {-1,5,-1,-1,6,4}, {-1,5,-1,-1,-1,-1}, {-1,-1,7,-1,-1,8}, {-1,-1,-1,-1,-1,8}, {-1,5,-1,-1,-1,8}},i,zhuangtai=0,jieshou; for(i=0;str[i]!='\0';i++) { if(str[i]==' ') jieshou=1; else if(str[i]=='.') jieshou=3; else if('0'<=str[i]&&str[i]<='9') jieshou=5; else if(str[i]=='+'||str[i]=='-') jieshou=2; else if(str[i]=='e') jieshou=4; else jieshou=0; zhuangtai=zhuang[zhuangtai][jieshou]; if(zhuangtai==-1) return false; } if(zhuangtai==3||zhuangtai==4||zhuangtai==5||zhuangtai==8) return true; else return false; } };
 
切换
撰写答案
扫描后移动端查看本题