Sword refers to the character string of Offer-19 representing a numeric value

public static boolean isNumber(String s) {
    s = s.trim();
    try {
        Double.parseDouble(s);
    }catch (NumberFormatException e){
        return false;
    }
    // 排除特殊情况
    char last = s.charAt(s.length()-1);
    return (last >= '0' && last <= '9') || last == '.';
}

Of course you can’t write like this in order to pass the interview hahaha

The idea is finite state automata! According to the string from left to right, define the following 9 states.

0. Space at the beginning
1. Sign before the power sign
2. Number before the
decimal point 3. Decimal point and digits after the
decimal point 4. When there is a space before the decimal point, the decimal point and digits after the decimal point
5. Power sign
6. Power The sign after the sign
7. The number after the power sign
8. The trailing space

Understand the code is actually very simple

public static boolean isNumber(String s) {
    // Mainly draw the finite state automata
    Map[] states = {
            new HashMap<Character, Integer>() {
  
  { put(' ', 0); put('s', 1); put('d', 2); put('.', 4); }},
            new HashMap<Character, Integer>() {
  
  { put('d', 2); put('.', 4); }},
            new HashMap<Character, Integer>() {
  
  { put('d', 2); put('.', 3); put('e', 5); put(' ', 8); }},
            new HashMap<Character, Integer>() {
  
  { put('d', 3); put('e', 5); put(' ', 8); }},
            new HashMap<Character, Integer>() {
  
  { put('d', 3); }},
            new HashMap<Character, Integer>() {
  
  { put('s', 6); put('d', 7); }},
            new HashMap<Character, Integer>() {
  
  { put('d', 7); }},
            new HashMap<Character, Integer>() {
  
  { put('d', 7); put(' ', 8); }},
            new HashMap<Character, Integer>() {
  
  { put(' ', 8); }}
    };
    // mark the current state
    int p = 0;
    // Used for number, symbol, power symbol conversion
    char t;
    for (char c: s.toCharArray()){
        if (c >= '0' && c <= '9'){
            t = 'd';
        }else if (c == '+' || c == '-'){
            t = 's';
        }else if (c == 'e' || c == 'E'){
            t = 'e';
        }
        else if (c == '.' || c == ' '){
            t = c;
        }else {
            t = '?';
        }
        if (!states[p].containsKey(t)){
            return false;
        }
        p = (int) states[p].get(t);
    }
    // The legal end states are 2, 3, 7, 8
    return p == 2 || p == 3 || p == 7 || p == 8;
}

The following idea is also great and easy to understand

public static boolean isNumber(String s) {
    if (s == null || s.length() == 0){
        return false;
    }
    //Remove leading and trailing spaces
    s = s.trim();
    boolean numFlag = false;
    boolean dotFlag = false;
    boolean eFlag = false;
    for (int i = 0; i < s.length(); i++) {
        //Determine as a number, then mark numFlag
        if (s.charAt(i) >= '0' && s.charAt(i) <= '9') {
            numFlag = true;
            //Determined as. Need not appear. And did not appear e
        } else if (s.charAt(i) == '.' && !dotFlag && !eFlag) {
            dotFlag = true;
            //Determined as e, you need to have never seen e, and have a number
        } else if ((s.charAt(i) == 'e' || s.charAt(i) == 'E') && !eFlag && numFlag) {
            eFlag = true;
            numFlag = false;//In order to avoid the request of 123e, after the occurrence of e, the flag is false
            //Determined as a +- sign, which can only appear in the first position or immediately after e
        } else if ((s.charAt(i) == '+' || s.charAt(i) == '-') && (i == 0 || s.charAt(i - 1) == 'e' || s.charAt(i - 1) == 'E')) {

            //Other situations are illegal
        } else {
            return false;
        }
    }
    return numFlag;
}

Guess you like

Origin blog.csdn.net/a792396951/article/details/113661804
Recommended