Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit c41da11

Browse files
author
kkarpyshev
committed
Easy118 challenge
1 parent 5551b1c commit c41da11

File tree

1 file changed

+7
-4
lines changed

1 file changed

+7
-4
lines changed

‎src/easy/118. Pascal's Triangle .kt‎

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,31 @@
11
package easy
22

3+
import ArraysTopic
4+
import DynamicProgrammingTopic
5+
36
/**
47
* 118. Pascal's Triangle
58
* https://leetcode.com/problems/pascals-triangle/
69
*
710
* Given an integer numRows, return the first numRows of Pascal's triangle.
811
*/
912

10-
class Easy118 {
13+
class Easy118 : ArraysTopic, DynamicProgrammingTopic{
1114

1215
fun generate(numRows: Int): List<List<Int>> {
1316
if (numRows == 0) return emptyList()
1417
val result = ArrayList<List<Int>>(numRows)
1518
result.add(listOf(1))
1619
for (i in 1 until numRows) {
17-
val row = arrayOfNulls<Int>(i + 1)
20+
val row = IntArray(i + 1)
1821
row[0] = 1
1922
row[i] = 1
20-
for (j in 1..(i/2)) {
23+
for (j in 1..(i/2)) {
2124
val t = result[i - 1][j - 1] + result[i - 1][j]
2225
row[j] = t
2326
row[i - j] = t
2427
}
25-
result.add(i, row.filterNotNull())
28+
result.add(i, row.toList())
2629
}
2730
return result
2831
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /