zoukankan      html  css  js  c++  java
  • 剑指 Offer 20. 表示数值的字符串

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

    “1.为什么是啊???”

    class Solution {
        public boolean isNumber(String s) {
            if(s==null||s.length()==0){
                return false;
            }
            boolean intSeen=false;
            boolean dotSeen=false;
            boolean eSeen=false;
            char []c=s.trim().toCharArray();
            if(c==null||c.length==0){
                return false;
            }
            for(int i=0;i<c.length;i++){
                if(c[i]>='0'&&c[i]<='9'){
                    intSeen=true;
                }else if(c[i]=='.'){
                    if(dotSeen||eSeen){
                        return false;
                    }
                    dotSeen=true;
                }else if(c[i]=='e'||c[i]=='E'){
                    if(eSeen||!intSeen){
                        return false;
                    }
                    eSeen=true;
                    intSeen=false;
                }else if(c[i]=='+'||c[i]=='-'){
                    if(i != 0 && c[i-1] != 'e' && c[i-1] != 'E'){
                        return false;
                    }
    
                }else{
                    return false;
                }
    
            }
            return intSeen;
            
        }
    }
  • 相关阅读:
    E
    牛客比赛—身体训练
    前缀和例题
    欧拉函数模板
    3.30训练题
    poj1321棋盘问题
    记set学习
    B. K-th Beautiful String
    codeforces1293C
    LightOJ 1370 Bi-shoe and Phi-shoe
  • 原文地址:https://www.cnblogs.com/jieyi/p/14331368.html
Copyright © 2011-2022 走看看