반응형
1. 문제
https://leetcode.com/problems/pascals-triangle/
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;
};
반응형
'리트코드 > easy' 카테고리의 다른 글
[리트코드] 345. Reverse Vowels of a String - js (0) | 2023.08.09 |
---|---|
[리트코드] 136. Single Number - js (0) | 2023.07.19 |
[리트코드] 206. Reverse Linked List - js (0) | 2023.07.12 |
[리트코드] 104. Maximum Depth of Binary Tree - js (0) | 2023.07.11 |
[리트코드] 94. Binary Tree Inorder Traversal - js (0) | 2023.07.10 |