Basic Calculator II Submission Result: Memory Limit Exceeded


  • 0
    P
    /**
     * @param {string} s
     * @return {number}
     */
    var calculate = function (s) {
        //handle for space
      s = s.replace(/\s/g,"");
      //handle for two num calculation
      function cal(s){
                     var nums = s.split(/[+-/*//]/);
                     var op = s.match(/[+-/*//]/)[0];
                     switch(op){
                        case "+": return +nums[0]+(+nums[1]);
                        case "-": return +nums[0]-(+nums[1]);
                        case "*": return +nums[0]*(+nums[1]);
                        case "/": return +nums[0]/(+nums[1]);
                     }
      }
      function doCal(s,pattern){
        while(s.match(pattern)){
        s = s.replace(pattern,
          function(matchstr,pos,originaltext){
           return cal(matchstr);
          });
        }
        return s;
      }
      //do divide and multiply first;
      var pattern = /([\d]+)[/*//](\d+)/g;
      s = doCal(s,pattern);
      //do add and subtract;
      var pattern2 =  /(\d+)[+-](\d+)/g;
      s = doCal(s,pattern2);
      return parseInt(s);
    
    };

  • 0

    Please try again, I have increased the memory limit. Your solution is now judged as Wrong Answer, which failed for the input "2-3+4".


Log in to reply
 

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