Java Solution, Easy to understand and easy to implement, though kinda slow


  • 0

    The string may contain various symbols. I feel it's not that clear to solve the problem within one pass.

    My logic is simple:

    Partition the string according to the position of 'e'. If 'e' is not found, then, the entire string must be a floating point number without 'e' for it to be valid.

    If 'e' is found, then, the part of string before the 'e' must be a floating point number without 'e', and the part of the string after 'e' must be an integer (signed or unsigned).

    It's easy to determine whether a string is an integer.

    To check whether a string is a floating point number without 'e', I first remove a possible sign at the beginning of it. Then, I adopt a similar approach: partition the string according to '.'. If '.' is not found, then the string must be an unsigned integer for it to be valid. If '.' is found, the the part of the string before '.' (s1), and the part of the string after '.' (s2) must satisfy the following conditions for the string to be valid:

    • if (s1.length() == 0), s2 must be a valid unsigned integer.
    • if (s2.length() == 0), s1 must be a valid unsigned integer.
    • if both of them are not empty, both of them must be unsigned
      integers.

    It's slow, because we need to traverse the string multiple times.

    public class Solution {
    public boolean isNumber(String s) {
        s = s.trim();
        if (s.length() == 0) return false;
        int i = 0;
        int ePos = -1; // position of an 'e' in the string.
        while (i < s.length()) {
            if (s.charAt(i) == 'e') {
                ePos = i; break;
            }
            ++i;
        }
        if (ePos == -1) return isFloating(s);
        else return isFloating(s.substring(0, ePos)) && isInteger(s.substring(ePos + 1));
    }
    private boolean isFloating(String s) {
        if (s.length() == 0) return false;
        int dotPos = -1; // position of an '.' in the string.
        int i = 0; // need to remove the symbol first. 
        if (s.charAt(i) == '+' || s.charAt(i) == '-') ++i;
        int start = i;
        
        while (i < s.length()) {
            if (s.charAt(i) == '.') {
                dotPos = i;
                break;
            }
            ++i;
        }
        if (dotPos == -1) return isUnsignedInteger(s.substring(start));
        else {
            if (start == dotPos) return isUnsignedInteger(s.substring(dotPos + 1));
            else if (dotPos == s.length() - 1) return isUnsignedInteger(s.substring(start, dotPos));
            else {
                return isUnsignedInteger(s.substring(start, dotPos)) && isUnsignedInteger(s.substring(dotPos + 1));
            }
        }
    }
    
    private boolean isInteger(String s) {
        if (s.length() == 0) return false;
        int i = 0;
        if (s.charAt(i) == '+' || s.charAt(i) == '-') ++i;
        return isUnsignedInteger(s.substring(i));
        
    }
    private boolean isUnsignedInteger(String s) {
        if (s.length() == 0) return false;
        for (int i = 0; i < s.length(); ++i) {
            if (!Character.isDigit(s.charAt(i))) return false;
        }
        return true;
    }
    

    }


Log in to reply
 

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