Beats 98% o(n) simple java


  • 0
    S
     public boolean isNumber(String s) {
        if(s.length()==0) return false;
        char[] c=s.toCharArray();
        int scount=0,esign=-1,ecount=0,dsign=-1,dcount=0,start=0,end=s.length()-1;
        while(start<=end&&c[start]==' '){
            start++;
        }
        while(end>=start&&c[end]==' '){
            end--;
        }
        for(int i=start;i<=end;i++){
            if(c[i]<='9'&&c[i]>='0') scount++;
            else if(c[i]=='.'){
                dcount++;
                dsign=i;
                if(dcount>1) return false;
            }
            else if(c[i]=='e'){
                ecount++;
                esign=i;
                if(ecount>1) return false;
            }
            else if((c[i]=='-'||c[i]=='+')&&(i==start||i==esign+1)) ;
            else return false;
        }
        if(scount==0) return false;
        if(ecount==0) return true;
        if(esign<dsign||esign==0||esign==end) return false;
        return isNumber(s.substring(0,esign))&&isNumber(s.substring(esign+1,end+1));
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.