My straightforward java solution, clear and logical, commented at every important step


  • 0
    H

    I tried to be as straightforward as possible, and comment every important step.

    I think there's nothing really tricky in this problem, of course there maybe some tricky solutions that I didn't come up with, but my point is, as long as you stay focus and think hard at each step, you should be able to cover every case and solve it.

    There are tests that are not needed because they will be covered by other tests, but I decided not to go back to find and delete them because I feel that the risk is high, might just leave them there

    public class Solution {
        // all possible chars are '.', '+', '-', 'e', and numbers
        // number of '.' or 'e' can't be larger than 1
        public boolean isNumber(String s) {
            if (s == null) return false;
            String str = s.trim();
            if (str.length() == 0) return false;
            if (str.length() == 1) return isNumber(str.charAt(0));
            char[] c = str.toCharArray();
            int size = c.length;
            int dot = 0;
            int e = 0;
            // start can only be '.', '+'', '-'', or a number
            // if strat with '.', next must be a number
            // if start start '+' or '-', next must be '.' or a number
            // start with other non-number chars, return false
            if (!isNumber(c[0])) {
                if (c[0] == '.') {
                    if (!isNumber(c[1])) return false;
                    dot++;
                } else if (c[0] == '+' || c[0] == '-') {
                    if (c[1] != '.' && !isNumber(c[1])) return false;
                } else {
                    return false;
                }
            }
            
            // inside the string, there can only be '.', 'e', '+', '-', or numbers
            // for '.', if a 'e' has already appeared, return false; if number of dot > 1, return false
            // for 'e', if previous char is not a number and is not a '.', return false; if next char 
            //          is not a number and is not a '+' or a '-', return false; if number of e > 1, return false
            // for '+' and '-', previous char must be a 'e' and next char must be a number
            // for any other non-number chars, return false
            for (int i = 1; i < size - 1; i++) {
                if (!isNumber(c[i])) {
                    if (c[i] == '.') {
                        if (e > 0) return false;
                        if (++dot > 1) return false;
                    } else if (c[i] == 'e') {
                        if (!isNumber(c[i - 1]) && c[i - 1] != '.') return false;
                        if (!isNumber(c[i + 1]) && c[i + 1] != '+' && c[i + 1] != '-') return false;
                        if (++e > 1) return false;
                    } else if (c[i] == '+' || c[i] == '-') {
                        if (c[i - 1] != 'e' || !isNumber(c[i + 1]))  return false;
                    } else {
                        return false;
                    }
                }
            }
            // end can only be . or a number
            if (c[size - 1] == '.') {
                if (e > 0) return false;
                if (!isNumber(c[size - 2])) return false;
                if (++dot > 1) return false;
            } else if (!isNumber(c[size - 1])) {
                return false;
            }
            return true;
        }
        
        private boolean isNumber(char c) {
            return c - '0' >= 0 && c - '9' <= 0;
        }
    }
    

Log in to reply
 

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