Java Solution with regex

  • 0

    using regular expression is the instinct.

    import java.util.regex.*;
    class Solution {
        public boolean isNumber(String s) {
            Pattern p = Pattern.compile("\\s*[+-]?(\\.\\d+|\\d+\\.|\\d+(\\.\\d+)?)(e[+-]?\\d+)?\\s*");
            Matcher m = p.matcher(s);
            if (m.matches()) {
                return true;
            return false;

Log in to reply

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