List all tricky examples cover all test case and simple java method with only three flag


  • 0
    F

    all the test case has been shown

    public class Solution {
        // e part
            // "-e23" f
            // "1e1e" f
            // "2e"
        // . part
            // "-." f
            // "1e2." f
            // "2.3." f
        // '-' or '+' part
            // "+1" t
            // "2e5-2" f
            // "2e-5" t
            // "2e+5" t
            
        // not a valid number
            // "a" f
        public boolean isNumber(String s) {
            s = s.trim();
            if(s.length() == 0) return false;
            char[] sArr = s.toCharArray();
            boolean flagDot =false;
            boolean flagE =false;
            boolean hasNum = false;
            for(int i = 0 ;i< sArr.length; i++){
                if(sArr[i] == 'e'){
                    if(!hasNum||flagE|| i == sArr.length - 1){
                        return false;
                    }
                    flagE = true;
                }else if(sArr[i] == '.' ){
                    if(!hasNum && i == sArr.length - 1){
                        return false;
                    }
                    if(flagE||flagDot){
                        return false;
                    }
                    flagDot = true;
                }else if(sArr[i] == '-' || sArr[i] == '+'){
                    if(i ==0) continue;
                    if(sArr[i - 1] != 'e'||i == sArr.length - 1) return false;
                }else if(sArr[i] < '0' || sArr[i] > '9'){
                    return false;
                }
                else{
                    hasNum = true;
                }
            }
            return true;
        }
    }
    

Log in to reply
 

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