Skip to content

Commit 1399cd9

Browse files
committed
feat: commit solution 118
1 parent 5d16014 commit 1399cd9

File tree

6 files changed

+175
-17
lines changed

6 files changed

+175
-17
lines changed

_sidebar.md

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
- [66.加一](solution/1-99/0066.plus-one/)
2222
- [83.删除排序链表中的重复元素](solution/1-99/0083.remove-duplicates-from-sorted-list/)
2323
- [88.合并两个有序数组](solution/1-99/0088.merge-sorted-array/)
24+
- [118.杨辉三角](solution/100-199/0118.pascals-triangle/)
2425
- [125.验证回文串](solution/100-199/0125.valid-palindrome/)
2526
- [169.多数元素](solution/100-199/0169.majority-element/)
2627
- [217.存在重复元素](solution/200-299/0217.contains-duplicate/)

index-tags.md

+17-16
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,22 @@
22

33
<!-- tabs:start -->
44

5-
#### **数学**
5+
#### **数组**
66

77
| 题号 | 题解 | 标签 | 难度 | 是否解题 |
88
| --- | --- | --- | --- | --- |
9-
| [9](https://leetcode-cn.com/problems/palindrome-number) | [回文数](/solution/1-99/0009.palindrome-number/) | `数学` | <font color=green>简单</font> ||
10-
| [7](https://leetcode-cn.com/problems/reverse-integer) | [整数反转](/solution/1-99/0007.reverse-integer/) | `数学` | <font color=green>简单</font> ||
11-
| [13](https://leetcode-cn.com/problems/roman-to-integer) | [罗马数字转整数](/solution/1-99/0013.roman-to-integer/) | `数学`,`字符串` | <font color=green>简单</font> ||
9+
| [1](https://leetcode-cn.com/problems/two-sum) | [两数之和](/solution/1-99/0001.two-sum/) | `数组`,`哈希表` | <font color=green>简单</font> ||
10+
| [26](https://leetcode-cn.com/problems/remove-duplicates-from-sorted-array) | [删除排序数组中的重复项](/solution/1-99/0026.remove-duplicates-from-sorted-array/) | `数组`,`双指针` | <font color=green>简单</font> ||
11+
| [27](https://leetcode-cn.com/problems/remove-element) | [移除元素](/solution/1-99/0027.remove-element/) | `数组`,`双指针` | <font color=green>简单</font> ||
12+
| [35](https://leetcode-cn.com/problems/search-insert-position) | [搜索插入位置](/solution/1-99/0035.search-insert-position/) | `数组`,`二分查找` | <font color=green>简单</font> ||
13+
| [66](https://leetcode-cn.com/problems/plus-one) | [加一](/solution/1-99/0066.plus-one/) | `数组` | <font color=green>简单</font> ||
14+
| [88](https://leetcode-cn.com/problems/merge-sorted-array) | [合并两个有序数组](/solution/1-99/0088.merge-sorted-array/) | `数组`,`双指针` | <font color=green>简单</font> ||
15+
| [118](https://leetcode-cn.com/problems/pascals-triangle) | [杨辉三角](/solution/100-199/0118.pascal%27s-triangle/) | `数组` | <font color=green>简单</font> ||
16+
| [169](https://leetcode-cn.com/problems/majority-element) | [多数元素](/solution/100-199/0169.majority-element/) | `位运算`,`数组`,`分治算法` | <font color=green>简单</font> ||
17+
| [217](https://leetcode-cn.com/problems/contains-duplicate) | [存在重复元素](/solution/200-299/0217.contains-duplicate/) | `数组`,`哈希表` | <font color=green>简单</font> ||
18+
| [219](https://leetcode-cn.com/problems/contains-duplicate-ii) | [存在重复元素 ii](/solution/200-299/0219.contains-duplicate-ii/) | `数组`,`哈希表` | <font color=green>简单</font> ||
19+
| [283](https://leetcode-cn.com/problems/move-zeroes) | [移动零](/solution/200-299/0283.move-zeroes/) | `数组`,`双指针` | <font color=green>简单</font> ||
20+
1221

1322
#### **字符串**
1423

@@ -19,21 +28,13 @@
1928
| [28](https://leetcode-cn.com/problems/implement-strstr) | [实现 strstr()](/solution/1-99/0028.implement-strstr%28%29/) | `双指针`,`字符串` | <font color=green>简单</font> ||
2029
| [125](https://leetcode-cn.com/problems/valid-palindrome) | [验证回文串](/solution/100-199/0125.valid-palindrome/) | `双指针`,`字符串` | <font color=green>简单</font> ||
2130

22-
23-
#### **数组**
31+
#### **数学**
2432

2533
| 题号 | 题解 | 标签 | 难度 | 是否解题 |
2634
| --- | --- | --- | --- | --- |
27-
| [1](https://leetcode-cn.com/problems/two-sum) | [两数之和](/solution/1-99/0001.two-sum/) | `数组`,`哈希表` | <font color=green>简单</font> ||
28-
| [26](https://leetcode-cn.com/problems/remove-duplicates-from-sorted-array) | [删除排序数组中的重复项](/solution/1-99/0026.remove-duplicates-from-sorted-array/) | `数组`,`双指针` | <font color=green>简单</font> ||
29-
| [27](https://leetcode-cn.com/problems/remove-element) | [移除元素](/solution/1-99/0027.remove-element/) | `数组`,`双指针` | <font color=green>简单</font> ||
30-
| [35](https://leetcode-cn.com/problems/search-insert-position) | [搜索插入位置](/solution/1-99/0035.search-insert-position/) | `数组`,`二分查找` | <font color=green>简单</font> ||
31-
| [66](https://leetcode-cn.com/problems/plus-one) | [加一](/solution/1-99/0066.plus-one/) | `数组` | <font color=green>简单</font> ||
32-
| [88](https://leetcode-cn.com/problems/merge-sorted-array) | [合并两个有序数组](/solution/1-99/0088.merge-sorted-array/) | `数组`,`双指针` | <font color=green>简单</font> ||
33-
| [169](https://leetcode-cn.com/problems/majority-element) | [多数元素](/solution/100-199/0169.majority-element/) | `位运算`,`数组`,`分治算法` | <font color=green>简单</font> ||
34-
| [217](https://leetcode-cn.com/problems/contains-duplicate) | [存在重复元素](/solution/200-299/0217.contains-duplicate/) | `数组`,`哈希表` | <font color=green>简单</font> ||
35-
| [219](https://leetcode-cn.com/problems/contains-duplicate-ii) | [存在重复元素 ii](/solution/200-299/0219.contains-duplicate-ii/) | `数组`,`哈希表` | <font color=green>简单</font> ||
36-
| [283](https://leetcode-cn.com/problems/move-zeroes) | [移动零](/solution/200-299/0283.move-zeroes/) | `数组`,`双指针` | <font color=green>简单</font> ||
35+
| [9](https://leetcode-cn.com/problems/palindrome-number) | [回文数](/solution/1-99/0009.palindrome-number/) | `数学` | <font color=green>简单</font> ||
36+
| [7](https://leetcode-cn.com/problems/reverse-integer) | [整数反转](/solution/1-99/0007.reverse-integer/) | `数学` | <font color=green>简单</font> ||
37+
| [13](https://leetcode-cn.com/problems/roman-to-integer) | [罗马数字转整数](/solution/1-99/0013.roman-to-integer/) | `数学`,`字符串` | <font color=green>简单</font> ||
3738

3839
#### **哈希表**
3940

index-type.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
| [110](https://leetcode-cn.com/problems/balanced-binary-tree) | [平衡二叉树](/solution/100-199/0110.balanced-binary-tree/) | ``,`深度优先搜索` | <font color=green>简单</font> |
3636
| [111](https://leetcode-cn.com/problems/minimum-depth-of-binary-tree) | [二叉树的最小深度](/solution/100-199/0111.minimum-depth-of-binary-tree/) | ``,`深度优先搜索`,`广度优先搜索` | <font color=green>简单</font> |
3737
| [112](https://leetcode-cn.com/problems/path-sum) | [路径总和](/solution/100-199/0112.path-sum/) | ``,`深度优先搜索` | <font color=green>简单</font> |
38-
| [118](https://leetcode-cn.com/problems/pascals-triangle) | [杨辉三角](/solution/100-199/0118.pascal%27s-triangle/) | `数组` | <font color=green>简单</font> |
38+
| [118](https://leetcode-cn.com/problems/pascals-triangle) | [杨辉三角](/solution/100-199/0118.pascal%27s-triangle/) | `数组` | <font color=green>简单</font> ||
3939
| [119](https://leetcode-cn.com/problems/pascals-triangle-ii) | [杨辉三角 ii](/solution/100-199/0119.pascal%27s-triangle-ii/) | `数组` | <font color=green>简单</font> |
4040
| [121](https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock) | [买卖股票的最佳时机](/solution/100-199/0121.best-time-to-buy-and-sell-stock/) | `数组`,`动态规划` | <font color=green>简单</font> |
4141
| [122](https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock-ii) | [买卖股票的最佳时机 ii](/solution/100-199/0122.best-time-to-buy-and-sell-stock-ii/) | `贪心算法`,`数组` | <font color=green>简单</font> |
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# [118.杨辉三角](https://leetcode-cn.com/problems/pascals-triangle/description/)
2+
3+
4+
### 题目描述
5+
6+
<div class="notranslate"><p>给定一个非负整数&nbsp;<em>numRows,</em>生成杨辉三角的前&nbsp;<em>numRows&nbsp;</em>行。</p>
7+
8+
<p><img src="https://upload.wikimedia.org/wikipedia/commons/0/0d/PascalTriangleAnimated2.gif" alt=""></p>
9+
10+
<p><small>在杨辉三角中,每个数是它左上方和右上方的数的和。</small></p>
11+
12+
<p><strong>示例:</strong></p>
13+
14+
<pre><strong>输入:</strong> 5
15+
<strong>输出:</strong>
16+
[
17+
[1],
18+
[1,1],
19+
[1,2,1],
20+
[1,3,3,1],
21+
[1,4,6,4,1]
22+
]</pre>
23+
</div>
24+
25+
### 解题思路
26+
27+
1. 递归实现
28+
29+
### 代码实现
30+
31+
<!-- tabs:start -->
32+
33+
#### **Golang**
34+
```go
35+
func generate(numRows int) [][]int {
36+
if numRows == 0 {
37+
return [][]int{}
38+
} else if numRows == 1 {
39+
return [][]int{{1}}
40+
} else if numRows == 2 {
41+
return [][]int{{1}, {1, 1}}
42+
}
43+
var res [][]int
44+
res = append(res, []int{1})
45+
res = append(res, []int{1, 1})
46+
recursive(2, numRows, &res)
47+
return res
48+
}
49+
50+
func recursive(current, numRows int, res *[][]int) {
51+
row := make([]int, current+1)
52+
row[0], row[current] = 1, 1
53+
for i := 1; i < current; i++ {
54+
row[i] = (*res)[current-1][i-1] + (*res)[current-1][i]
55+
}
56+
*res = append(*res, row)
57+
if current+1 == numRows {
58+
return
59+
} else {
60+
recursive(current+1, numRows, res)
61+
}
62+
}
63+
```
64+
65+
66+
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package leetcode
2+
3+
/*
4+
* @lc app=leetcode.cn id=118 lang=golang
5+
*
6+
* [118] 杨辉三角
7+
*/
8+
9+
// @lc code=start
10+
func generate(numRows int) [][]int {
11+
if numRows == 0 {
12+
return [][]int{}
13+
} else if numRows == 1 {
14+
return [][]int{{1}}
15+
} else if numRows == 2 {
16+
return [][]int{{1}, {1, 1}}
17+
}
18+
var res [][]int
19+
res = append(res, []int{1})
20+
res = append(res, []int{1, 1})
21+
recursive(2, numRows, &res)
22+
return res
23+
}
24+
25+
func recursive(current, numRows int, res *[][]int) {
26+
row := make([]int, current+1)
27+
row[0], row[current] = 1, 1
28+
for i := 1; i < current; i++ {
29+
row[i] = (*res)[current-1][i-1] + (*res)[current-1][i]
30+
}
31+
*res = append(*res, row)
32+
if current+1 == numRows {
33+
return
34+
} else {
35+
recursive(current+1, numRows, res)
36+
}
37+
}
38+
39+
// @lc code=end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package leetcode
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func TestGenerate(t *testing.T) {
8+
9+
var target int
10+
var ret [][]int
11+
12+
ret = [][]int{}
13+
target = 0
14+
for k, nums := range generate(target) {
15+
for i, v := range nums {
16+
if v != ret[k][i] {
17+
t.Fatalf("case fails: %v\n", ret)
18+
}
19+
}
20+
}
21+
22+
ret = [][]int{{1}}
23+
target = 1
24+
for k, nums := range generate(target) {
25+
for i, v := range nums {
26+
if v != ret[k][i] {
27+
t.Fatalf("case fails: %v\n", ret)
28+
}
29+
}
30+
}
31+
32+
ret = [][]int{{1}, {1, 1}}
33+
target = 2
34+
for k, nums := range generate(target) {
35+
for i, v := range nums {
36+
if v != ret[k][i] {
37+
t.Fatalf("case fails: %v\n", ret)
38+
}
39+
}
40+
}
41+
42+
ret = [][]int{{1}, {1, 1}, {1, 2, 1}, {1, 3, 3, 1}}
43+
target = 4
44+
for k, nums := range generate(target) {
45+
for i, v := range nums {
46+
if v != ret[k][i] {
47+
t.Fatalf("case fails: %v\n", ret)
48+
}
49+
}
50+
}
51+
}

0 commit comments

Comments
 (0)