g0101_0200.s0118_pascals_triangle.Solution Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of leetcode-in-java17 Show documentation
Show all versions of leetcode-in-java17 Show documentation
Java Solution for LeetCode algorithm problems, continually updating
package g0101_0200.s0118_pascals_triangle;
// #Easy #Top_Interview_Questions #Array #Dynamic_Programming #Data_Structure_I_Day_4_Array
// #Dynamic_Programming_I_Day_12 #Udemy_Dynamic_Programming
// #2022_06_23_Time_1_ms_(67.08%)_Space_42.4_MB_(5.58%)
import java.util.ArrayList;
import java.util.List;
@SuppressWarnings("java:S2589")
public class Solution {
public List> generate(int numRows) {
List> output = new ArrayList<>();
for (int i = 0; i < numRows; i++) {
List currRow = new ArrayList<>();
for (int j = 0; j <= i; j++) {
if (j == 0 || j == i || i <= 1) {
currRow.add(1);
} else {
int currCell = output.get(i - 1).get(j - 1) + output.get(i - 1).get(j);
currRow.add(currCell);
}
}
output.add(currRow);
}
return output;
}
}