65.有效数字

class Solution {
    public boolean isNumber(String s) {
        boolean point = false, flagE = false, flagNum = false;
        s = s.trim();
        if(s.length() > 0 && (s.charAt(0) == '-'||s.charAt(0) == '+' )){
            s = s.substring(1);
        }
        for(int i = 0; i < s.length(); i++){
            char c = s.charAt(i);
            if((c >= 48 && c <= 57) ){
                flagNum = true;
                continue;
            }
            else if(c == ' '){
                return false;
            }
            else if(c == '.' ){
                if(point || flagE){ //e 不能在点前面
                    return false;
                }
                point = true;
            }
            else if(c == 'e' || c == 'E'){
                if(!flagNum || i == s.length()-1 || flagE){
                    return false;
                }
                if((s.charAt(i+1) == '+'||s.charAt(i+1) == '-') && i < s.length() - 2){
                    i++;
                }
                flagE = true;
            }
            else {
                return  false;
            }
        }
        return flagNum;
    }
}

你可能感兴趣的:(65.有效数字)