코딩테스트 풀이/JAVA
[LeetCode/Easy] 118. Pascal's Triangle
무지맘
2023. 5. 12. 21:55
1. Input
1) int numRows
2. Output
1) numRows층으로 구성된 파스칼 삼각형을 리스트의 리스트로 반환
3. Constraint
1) 1 <= numRows <= 30
4. Example
Input: numRows = 5 -> Output: [[1],[1,1],[1,2,1],[1,3,3,1],[1,4,6,4,1]]
5. Code
1) 첫 코드(2023/05/12)
class Solution {
public List<List<Integer>> generate(int numRows) {
int[][] triangle = new int[numRows][numRows];
for(int[] a : triangle)
Arrays.fill(a, 1);
for(int i=2 ; i<triangle.length ; i++)
for(int j=1 ; j<i ; j++)
triangle[i][j] = triangle[i-1][j-1] + triangle[i-1][j];
List<List<Integer>> answer = new ArrayList<List<Integer>>();
for(int i=0 ; i<numRows ; i++){
List<Integer> list = new ArrayList<>();
for(int j=0 ; j<=i ; j++)
list.add(triangle[i][j]);
answer.add(list);
}
return answer;
}
}