Skip to content

Commit cc67879

Browse files
committed
feat: add the solution of Pascal's Triangle II(119) with kotlin.
1 parent 22b8c60 commit cc67879

File tree

4 files changed

+67
-4
lines changed

4 files changed

+67
-4
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@
5656
| [111][111-question] | [Minimum Depth of Binary Tree][111-tips] | [][111-java] | | [][111-kotlin] |
5757
| [112][112-question] | [Path Sum][112-tips] | [][112-java] | | [][112-kotlin] |
5858
| [118][118-question] | [Pascal's Triangle][118-tips] | [][118-java] | | [][118-kotlin] |
59-
| [119][119-question] | [Pascal's Triangle II][119-tips] | [][119-java] | | |
59+
| [119][119-question] | [Pascal's Triangle II][119-tips] | [][119-java] | | [][119-kotlin] |
6060
| [121][121-question] | [Best Time to Buy and Sell Stock][121-tips] | [][121-java] | | |
6161
| [122][122-question] | [Best Time to Buy and Sell Stock II][122-tips] | [][122-java] | | |
6262
| [226][226-question] | [Invert Binary Tree][226-tips] | [][226-java] | [][226-js] | |
@@ -512,4 +512,5 @@
512512
[111-kotlin]: ./src/_111/kotlin/Solution.kt
513513
[112-kotlin]: ./src/_112/kotlin/Solution.kt
514514
[118-kotlin]: ./src/_118/kotlin/Solution.kt
515+
[119-kotlin]: ./src/_119/kotlin/Solution.kt
515516
[771-kotlin]: ./src/_771/kotlin/Solution.kt

src/_118/kotlin/Solution.kt

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,10 @@ class Solution {
2424
}
2525

2626
fun main(args: Array<String>) {
27-
println(Solution().generate(7))
28-
}
27+
val message = Solution().generate(10)
28+
for(item in message){
29+
println(item)
30+
}
31+
}
32+
//2 3 4 5 6 7 8
33+
//3 6 10 15 21 28

src/_119/kotlin/Solution.kt

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package _119.kotlin
2+
3+
/**
4+
* @author relish
5+
* @since 2018/05/05
6+
*/
7+
class Solution {
8+
fun getRow1(rowIndex: Int): List<Int> {
9+
val list = Array(rowIndex + 1, { i -> Array(i + 1, { 1 }) })
10+
for (i in 0 until rowIndex + 1) {
11+
for (j in 0 until i) {
12+
if (j == 0 || j == i) {
13+
list[i][j] = 1
14+
} else {
15+
list[i][j] = list[i - 1][j - 1] + list[i - 1][j]
16+
}
17+
}
18+
}
19+
return list[rowIndex].asList()
20+
}
21+
22+
fun getRow(rowIndex: Int): List<Int> {
23+
val res = Array(rowIndex + 1, { 1 })
24+
for (i in 1 until rowIndex + 1) {
25+
if (i > (rowIndex + 1) / 2) {
26+
res[i] = res[rowIndex - i]
27+
continue
28+
}
29+
res[i] = ((res[i - 1]) * (rowIndex - i + 1).toLong() / i).toInt()
30+
}
31+
return res.asList()
32+
}
33+
}
34+
35+
fun main(args: Array<String>) {
36+
println(Solution().getRow(3))
37+
println(Solution().getRow(30))
38+
}

tips/119/README.md

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,11 @@ Could you optimize your algorithm to use only *O*(*k*) extra space?
1515
**Tags:** Array
1616

1717

18-
## 思路
18+
## 思路0
1919

2020
题意是指定输出帕斯卡尔三角形的某一行,模拟即可,优化后的代码如下所示。
2121

22+
java:
2223
```java
2324
class Solution {
2425
public List<Integer> getRow(int rowIndex) {
@@ -33,7 +34,25 @@ class Solution {
3334
}
3435
}
3536
```
37+
## 思路1
3638

39+
题目提示我们可以思考一下时间复杂度为O(n)的算法。仔细观察每一行数字即可找出规律。
40+
kotlin(184ms/66.67%):
41+
```kotlin
42+
class Solution {
43+
fun getRow(rowIndex: Int): List<Int> {
44+
val res = Array(rowIndex + 1, { 1 })
45+
for (i in 1 until rowIndex + 1) {
46+
if (i > (rowIndex + 1) / 2) {
47+
res[i] = res[rowIndex - i]
48+
continue
49+
}
50+
res[i] = ((res[i - 1]) * (rowIndex - i + 1).toLong() / i).toInt()
51+
}
52+
return res.asList()
53+
}
54+
}
55+
```
3756

3857
## 结语
3958

0 commit comments

Comments
 (0)