Java solution


  • 0
    S

    import java.util.regex.Matcher;
    import java.util.regex.Pattern;

    class Solution {

    public boolean isNumber(String s) {
     if (s == null || s.trim().length() == 0) {
         return false;
     }
     s = s.trim();
     Pattern p = Pattern.compile("(^[-+]?[0-9]+(\\.[0-9]+)?([Ee][-+]?[0-9]+)?$|^[+-]?([0-9]+)?\\.[0-9]+([Ee][+-]?[0-9]+)?$|^[-+]?[0-9]+\\.([0-9]+)?([Ee][-+]?[0-9]+)?$)");
     Matcher m = p.matcher(s);
     if (m.find()){
        return true;
     }
    return false;
    

    }
    }


Log in to reply
 

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