美文网首页
53、表示数值的字符串

53、表示数值的字符串

作者: quiterr | 来源:发表于2019-06-05 15:51 被阅读0次

链接:https://www.nowcoder.com/practice/6f8c901d091949a5837e24bb82a731f2?tpId=13&tqId=11206&rp=2&ru=/ta/coding-interviews&qru=/ta/coding-interviews/question-ranking
来源:牛客网

题目描述
请实现一个函数用来判断字符串是否表示数值(包括整数和小数)。例如,字符串"+100","5e2","-123","3.1416"和"-1E-16"都表示数值。 但是"12e","1a3.14","1.2.3","+-5"和"12e+4.3"都不是。

这题真是令人作呕,各种情况都要考虑,很容易出错,以下是我写的代码,判题系统居然说-.123是数字,没有给ac。

public class Solution {
    public boolean isNumeric(char[] str) {
        if(str.length==0){
            return false;
        }
        //第一个字符是+、-
        if(str[0]=='+'||str[0]=='-'){
            return isNumeric2(str,1);
        }
        
        return isNumeric2(str,0); 
    }
    
    //没有正负号的数字
    public boolean isNumeric2(char[] str, int i){
        if(str[i]>='1'&&str[i]<='9'){
            //判断是否科学计数法
            if(isE(str,i+1)){
                return isNumeric3(str, i+1);
            }else{
                int count = 0; //小数点的个数
                for(int j=i+1; j<str.length; j++){
                    //最后一个字符是小数点
                    if(str[j]=='.'&&j==str.length-1){
                        return false;
                    }
                    
                    if(str[j]=='.'){
                        count++;
                        continue;
                    }
                    
                    if(str[j]<'0'||str[j]>'9'){
                        return false;
                    }
                }
                if(count>1){
                    return false;
                }
                return true;
            }
        }
        return false;
        
    }
    
    //科学计数法的数字
    public boolean isNumeric3(char[] str, int i){
        //遍历找到e或E的位置
        int j=i;
        for(; j<str.length; j++){
            if(str[j]=='e'||str[j]=='E'){
                break;
            }
        }
        
        //凡是e或E左边的必须是形如1.23这样的数字
        int count = 0; //小数点的个数
        for(int k=i; k<j; k++){
            //最后一个字符是小数点
            if(str[j-1]=='.'){
                return false;
            }

            if(str[k]=='.'){
                count++;
                continue;
            }

            if(str[k]<'0'||str[k]>'9'){
                return false;
            }
        }
        if(count>1){
            return false;
        }
        
        //凡是右边的必须是+5、-2、3这样的整数
        for(int k=j+1; k<str.length; k++){
            if(k==j+1&&str[k]=='+'||str[k]=='-'){
                //1e+这样的不是数字
                if(k+1==str.length){
                    return false;
                }
                continue;
            }
            if(str[k]<'0'||str[k]>'9'){
                return false;
            }
        }
        return true;
    }
    
    //是否科学计数法
    public boolean isE(char[] str, int i){
        for(int j=i; j<str.length; j++){
            if(str[j]=='e'||str[j]=='E'){
                return true;
            }
        }
        return false;
    }
}

如果允许用正则表达式或者库函数就另当别论了,以下是ac的代码。

public class Solution {
    public boolean isNumeric(char[] str) {
        String string = String.valueOf(str);
        return string.matches("[\\+-]?[0-9]*(\\.[0-9]*)?([eE][\\+-]?[0-9]+)?");
    }  
}
public class Solution {
    public boolean isNumeric(char[] str) {
        try {
            double re = Double.parseDouble(new String(str));
        } catch (NumberFormatException e) {
            return false;
        }
        return true;
    }  
}

相关文章

网友评论

      本文标题:53、表示数值的字符串

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