我的js算法爬坑之旅-杨辉三角

225 阅读1分钟

第六十天:力扣118题,杨辉三角

地址:leetcode-cn.com/problems/pa…

思路:暴力解题,就是这样。

var generate = function(numRows) {
  let res = [];
  for(let i = 0; i < numRows; i++)
  {
    const arr = new Array(i+1).fill(1);
    for(let j = 1; j < arr.length - 1; j++)
    {
      arr[j] = res[i - 1][j - 1] + res[i - 1][j];
    }
    res.push(arr);
  }
  return res;
};

执行用时:80 ms, 在所有 JavaScript 提交中击败了73.17%的用户

内存消耗:37.7 MB, 在所有 JavaScript 提交中击败了29.14%的用户