regular expression solution


  • 0
    R

    regexp solution :

    ^\s*[-+]?(\d+\.?\d*|\.\d+)(e[-+]?\d+)?\s*$
    
    /**
     * @param {string} s
     * @return {boolean}
     */
    var isNumber = function(s) {
        // method 1
        /*s = s.trim();
        if (s === '')
            return false;
        return !isNaN(s);*/
    
        // method 2
       const reg = /^\s*[-+]?(\d+\.?\d*|\.\d+)(e[-+]?\d+)?\s*$/;
       return reg.test(s);
    };
    
    

Log in to reply
 

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