리트코드/easy
[리트코드] 118. Pascal's Triangle - js
bbeyak
2023. 7. 18. 13:11
반응형
1. 문제
https://leetcode.com/problems/pascals-triangle/
Pascal's Triangle - LeetCode
Can you solve this real interview question? Pascal's Triangle - Given an integer numRows, return the first numRows of Pascal's triangle. In Pascal's triangle, each number is the sum of the two numbers directly above it as shown: [https://upload.wikimedia.o
leetcode.com
2. 코드
/**
* @param {number} numRows
* @return {number[][]}
*/
var generate = function(numRows) {
let answer = Array.from({length:numRows},(_,idx) => new Array(idx+1).fill(1));
for(let i=0;i<answer.length;i++){
for(let j=0;j<i;j++){
if(answer[i-1][j-1] && answer[i-1][j]){
answer[i][j] = answer[i-1][j-1] + answer[i-1][j];
}
}
}
return answer;
};
반응형