稍微改了一行就超时了、不明所以


  • 0
    4

    var cal = function(nums, target, cache) {
    if (cache[target] !== undefined) {
    return cache[target];
    }
    var ans = 0;
    for (var i = 0; i < nums.length; i++) {
    if (nums[i] <= target) {
    ans += cal(nums, target - nums[i], cache);
    }
    }
    cache[target] = ans;
    return ans;
    }
    /**

    • @param {number[]} nums
    • @param {number} target
    • @return {number}
      */
      var combinationSum4 = function(nums, target) {
      return cal(nums, target, [1]);
      };

    第二行 if (cache[target] !== undefined) 改成 if (cache[target]) 竟然超时了、按理说应该差不多吧,求指点


Log in to reply
 

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