杨辉三角
给定一个函数 function triangle(n){} ,实现输入n就返回杨辉三角的第n行
function triangle(n) {
let arr1 = []
let arr2 = []
for (var i = 0; i < n; i++) {
for (var j = 0; j <= i; j++) {
if (j == 0 || j == i) {
arr2[j] = 1
} else {
arr2[j] = arr1[j - 1] + arr1[j]
}
}
arr1 = [...arr2]
}
console.log(arr2);
}
triangle(5);
给定一个函数 function triangle(n){} ,实现输入n就返回杨辉三角的前n行
function triangle(n) {
let arr = [];
for (var i = 0; i < n; i++) {
arr[i] = [];
for (var j = 0; j <= i; j++) {
if ((j == 0) || (j == i)) {
arr[i][j] = 1;
} else {
arr[i][j] = arr[i - 1][j - 1] + arr[i - 1][j];
}
}
}
console.log(arr);
}
triangle(10);