File tree Expand file tree Collapse file tree 1 file changed +49
-0
lines changed Expand file tree Collapse file tree 1 file changed +49
-0
lines changed Original file line number Diff line number Diff line change
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
+
You can’t perform that action at this time.
0 commit comments