0065. Valid Number (H)

Valid Number (H)

题目

Validate if a given string can be interpreted as a decimal number.

Some examples:
"0" => true
" 0.1 " => true
"abc" => false
"1 a" => false
"2e10" => true
" -90e3 " => true
" 1e" => false
"e3" => false
" 6e-1" => true
" 99e2.5 " => false
"53.5e93" => true
" --6 " => false
"-+3" => false
"95a54e53" => false

Note: It is intended for the problem statement to be ambiguous. You should gather all requirements up front before implementing one. However, here is a list of characters that can be in a valid decimal number:

  • Numbers 0-9
  • Exponent - "e"
  • Positive/negative sign - "+"/"-"
  • Decimal point - "."

Of course, the context of these characters also matters in the input.

Update (2015-02-10):
The signature of the C++ function had been updated. If you still see your function signature accepts a const char * argument, please click the reload button to reset your code definition.


题意

给定一个字符串,判断它能不能正确表示一个数。满足条件的字符串中只可能包含以下字符:'0'-'9'、科学计数'e'、正负符号'+'/'-'、小数点'.'。

思路

坑点在于需要判断的情况比较多。我的方法是针对字符串中的每一个字符,只判断它与前后两个字符形成的组合是否有效。具体有效组合参考注释。(d代表数字)


代码实现

Java

class Solution {
    public boolean isNumber(String s) {
        boolean isExponentExist = false;	// 记录'e'是否已出现
        boolean isPointExist = false;		// 记录'.'是否已出现

        s = s.trim();
        
        if (s.length() == 0) {
            return false;
        }

        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            if (!isDigit(c) && c != '+' && c != '-' && c != 'e' && c != '.') {
                return false;
            }
            if ((c == '+' || c == '-') && !isPlusOrMinusValid(s, i)) {
                return false;
            }
            if (c == 'e') {
                if (isExponentExist || !isExponentValid(s, i)) {
                    return false;
                }
                isExponentExist = true;
            }
            if (c == '.') {
                if (isPointExist || isExponentExist || !isPointValid(s, i)) {
                    return false;
                }
                isPointExist = true;
            }
        }
        return true;
    }

    private boolean isDigit(char c) {
        return c >= '0' && c <= '9';
    }
	
    // '+'/'-'的有效组合:   [+-][d.]  e[+-]d
    private boolean isPlusOrMinusValid(String s, int i) {
        if (i == 0 && 1 < s.length() && (isDigit(s.charAt(1)) || s.charAt(1) == '.')) {
            return true;
        }
        if (i - 1 >= 0 && s.charAt(i - 1) == 'e' && i + 1 < s.length() && isDigit(s.charAt(i + 1))) {
            return true;
        }
        return false;
    }

    // 'E'的有效组合:   [d.]e[d+-] 
    private boolean isExponentValid(String s, int i) {
        if (i - 1 >= 0) {
            char left = s.charAt(i - 1);
            if (i + 1 < s.length()) {
                char right = s.charAt(i + 1);
                if ((isDigit(left) || left == '.') && (isDigit(right) || right == '+' || right == '-')) {
                    return true;
                }
            }
        }
        return false;
    }

    // '.'的有效组合:   d.d  d.e  [+-].d  d.  .d     
    private boolean isPointValid(String s, int i) {
        if (i - 1 >= 0) {
            char left = s.charAt(i - 1);
            if (i + 1 < s.length()) {
                char right = s.charAt(i + 1);
                if (isDigit(left) && (right == 'e' || isDigit(right))) {
                    return true;
                }
                if ((left == '+' || left == '-') && isDigit(right)) {
                    return true;
                }
            } else {
                if (isDigit(left)) {
                    return true;
                }
            }
        } else {
            if (i + 1 < s.length()) {
                char right = s.charAt(i + 1);
                if (isDigit(right)) {
                    return true;
                }
            }
        }
        return false;
    }
}

JavaScript

/**
 * @param {string} s
 * @return {boolean}
 */
var isNumber = function(s) {
    return !s.match(/.?Infinity/) && !Number.isNaN(Number(s))
};
上一篇:@valid和自定义异常


下一篇:成功解决localhost is not a valid address