杨辉三角

108 阅读1分钟
function fun(numRows) {
  if (numRows === 0) {
    return [];
  }
  let result = Array.from(new Array(numRows), () => []);
  for (let i = 0; i < numRows; i++) {
    result[i][0] = 1;
    result[i][i] = 1;
    for (let j = 1; j < i; j++) {
      result[i][j] = result[i - 1][j - 1] + result[i - 1][j];
    }
  }
  return result;
};

console.log(fun(0));
console.log(fun(1));
console.log(fun(5));
console.log(fun(7));

image.png