File tree 4 files changed +67
-4
lines changed
4 files changed +67
-4
lines changed Original file line number Diff line number Diff line change 56
56
| [ 111] [ 111-question ] | [ Minimum Depth of Binary Tree] [ 111-tips ] | [ ✅] [ 111-java ] | | [ ✅] [ 111-kotlin ] |
57
57
| [ 112] [ 112-question ] | [ Path Sum] [ 112-tips ] | [ ✅] [ 112-java ] | | [ ✅] [ 112-kotlin ] |
58
58
| [ 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 ] |
60
60
| [ 121] [ 121-question ] | [ Best Time to Buy and Sell Stock] [ 121-tips ] | [ ✅] [ 121-java ] | | |
61
61
| [ 122] [ 122-question ] | [ Best Time to Buy and Sell Stock II] [ 122-tips ] | [ ✅] [ 122-java ] | | |
62
62
| [ 226] [ 226-question ] | [ Invert Binary Tree] [ 226-tips ] | [ ✅] [ 226-java ] | [ ✅] [ 226-js ] | |
512
512
[ 111-kotlin ] : ./src/_111/kotlin/Solution.kt
513
513
[ 112-kotlin ] : ./src/_112/kotlin/Solution.kt
514
514
[ 118-kotlin ] : ./src/_118/kotlin/Solution.kt
515
+ [ 119-kotlin ] : ./src/_119/kotlin/Solution.kt
515
516
[ 771-kotlin ] : ./src/_771/kotlin/Solution.kt
Original file line number Diff line number Diff line change @@ -24,5 +24,10 @@ class Solution {
24
24
}
25
25
26
26
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
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change @@ -15,10 +15,11 @@ Could you optimize your algorithm to use only *O*(*k*) extra space?
15
15
** Tags:** Array
16
16
17
17
18
- ## 思路
18
+ ## 思路0
19
19
20
20
题意是指定输出帕斯卡尔三角形的某一行,模拟即可,优化后的代码如下所示。
21
21
22
+ java:
22
23
``` java
23
24
class Solution {
24
25
public List<Integer > getRow (int rowIndex ) {
@@ -33,7 +34,25 @@ class Solution {
33
34
}
34
35
}
35
36
```
37
+ ## 思路1
36
38
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
+ ```
37
56
38
57
## 结语
39
58
You can’t perform that action at this time.
0 commit comments