코딩테스트 풀이/JAVA
[LeetCode/Easy] 119. Pascal's Triangle II
무지맘
2023. 5. 12. 22:01
1. Input
1) int rowIndex
2. Output
1) 파스칼 삼각형에서 위에서 rowIndex+1층에 위치한 수를 리스트에 담아 반환
3. Constraint
1) 0 <= rowIndex <= 33
4. Example
Input: rowIndex = 3 -> Output: [1,3,3,1]
5. Code
1) 첫 코드(2023/05/12)
class Solution {
public List<Integer> getRow(int rowIndex) {
int[][] triangle = new int[rowIndex+1][rowIndex+1];
for(int[] a : triangle)
Arrays.fill(a, 1);
for(int i=2 ; i<=rowIndex ; i++)
for(int j=1 ; j<i ; j++)
triangle[i][j] = triangle[i-1][j-1] + triangle[i-1][j];
List<Integer> answer = new ArrayList<Integer>();
for(int i=0 ; i<=rowIndex ; i++)
answer.add(triangle[rowIndex][i]);
return answer;
}
}