118. Pascal’s Triangle
Problem
1
2
3
4
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:
(그림)
Questions before reading example
Example
1
2
3
4
5
Input: numRows = 5
Output: [[1],[1,1],[1,2,1],[1,3,3,1],[1,4,6,4,1]]
Input: numRows = 1
Output: [[1]]
Solution
- 나의 풀이
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
class Solution {
public List<List<Integer>> generate(int numRows) {
List<List<Integer>> results = new ArrayList<>();
results.add(List.of(1));
if (numRows == 1) {
return results;
}
results.add(List.of(1, 1));
if (numRows == 2) {
return results;
}
for (int i = 2; i < numRows; i++) {
List<Integer> previousRow = results.get(i - 1);
List<Integer> newRow = new ArrayList<>();
newRow.add(1);
for (int j = 1; j < previousRow.size(); j++) {
newRow.add(previousRow.get(j-1) + previousRow.get(j));
}
newRow.add(1);
results.add(newRow);
}
return results;
}
}
Spent time
Review
- Easy 난이도여서 그런지 무식하게 푸니 풀렸다.