Skip to content

Commit 355a205

Browse files
committed
solve 119.pascals-triangle-ii
1 parent 1235e87 commit 355a205

File tree

1 file changed

+49
-0
lines changed

1 file changed

+49
-0
lines changed

vscode/119.pascals-triangle-ii.java

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* @lc app=leetcode id=119 lang=java
3+
*
4+
* [119] Pascal's Triangle II
5+
*
6+
* https://leetcode.com/problems/pascals-triangle-ii/description/
7+
*
8+
* algorithms
9+
* Easy (42.10%)
10+
* Total Accepted: 195.6K
11+
* Total Submissions: 455.8K
12+
* Testcase Example: '3'
13+
*
14+
* Given a non-negative index k where k ≤ 33, return the k^th index row of the
15+
* Pascal's triangle.
16+
*
17+
* Note that the row index starts from 0.
18+
*
19+
*
20+
* In Pascal's triangle, each number is the sum of the two numbers directly
21+
* above it.
22+
*
23+
* Example:
24+
*
25+
*
26+
* Input: 3
27+
* Output: [1,3,3,1]
28+
*
29+
*
30+
* Follow up:
31+
*
32+
* Could you optimize your algorithm to use only O(k) extra space?
33+
*
34+
*/
35+
class Solution {
36+
public List<Integer> getRow(int rowIndex) {
37+
List<Integer> result = new ArrayList<>();
38+
if (rowIndex < 0) return result;
39+
result.add(1);
40+
for (int i = 1; i <= rowIndex; i++) {
41+
for (int j = i - 1; j > 0; j--) {
42+
result.set(j, result.get(j) + result.get(j-1));
43+
}
44+
result.add(1);
45+
}
46+
return result;
47+
}
48+
}
49+

0 commit comments

Comments
 (0)