Skip to content

Commit 8cb3da8

Browse files
committed
added problem 119
1 parent 4cec516 commit 8cb3da8

File tree

3 files changed

+71
-0
lines changed

3 files changed

+71
-0
lines changed

problems/problem0119/pascal_ii.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package problem0119
2+
3+
/*
4+
Given an integer rowIndex, return the rowIndexth (0-indexed) row of the Pascal's triangle.
5+
In Pascal's triangle, each number is the sum of the two numbers directly above it as shown:
6+
*/
7+
8+
func getRow(rowIndex int) []int {
9+
if rowIndex == 0 {
10+
return []int{1}
11+
} else if rowIndex == 1 {
12+
return []int{1, 1}
13+
}
14+
15+
var cur = make([]int, 1, rowIndex+1)
16+
var prev = make([]int, 2, rowIndex+1)
17+
cur[0] = 1
18+
prev[0] = 1
19+
prev[1] = 1
20+
21+
for i := 2; i <= rowIndex+1; i++ {
22+
cur = cur[:i]
23+
cur[0] = 1
24+
for j := 1; j < len(cur)-1; j++ {
25+
cur[j] = prev[j-1] + prev[j]
26+
}
27+
cur[len(cur)-1] = 1
28+
prev, cur = cur, prev
29+
}
30+
return prev
31+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package problem0119
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/stretchr/testify/assert"
8+
)
9+
10+
type TestCase struct {
11+
Input int
12+
Expected []int
13+
}
14+
15+
var TestCases = []TestCase{
16+
{3, []int{1, 3, 3, 1}},
17+
{0, []int{1}},
18+
{1, []int{1, 1}},
19+
{10, []int{1, 10, 45, 120, 210, 252, 210, 120, 45, 10, 1}},
20+
{33, []int{1, 33, 528, 5456, 40920, 237336, 1107568, 4272048, 13884156, 38567100, 92561040, 193536720, 354817320, 573166440, 818809200, 1037158320, 1166803110, 1166803110, 1037158320, 818809200, 573166440, 354817320, 193536720, 92561040, 38567100, 13884156, 4272048, 1107568, 237336, 40920, 5456, 528, 33, 1}},
21+
}
22+
23+
func TestGetPascalRowII(t *testing.T) {
24+
assert := assert.New(t)
25+
26+
for _, tc := range TestCases {
27+
want := tc.Expected
28+
got := getRow(tc.Input)
29+
assert.Equal(want, got, fmt.Sprintf("%+v", tc))
30+
}
31+
}
32+
33+
func BenchmarkGetPascalRowII(b *testing.B) {
34+
for _, tc := range TestCases {
35+
for i := 0; i < b.N; i++ {
36+
getRow(tc.Input)
37+
}
38+
}
39+
}

readme.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ Each problem is in it's own directory, with test files. There are helper package
106106
| 0116 | [Populating Next Right Pointers in Each Node](https://leetcode.com/problems/populating-next-right-pointers-in-each-node) | [My Solution](./problems/problem0116) ||
107107
| 0117 | [Populating Next Right Pointers in Each Node II](https://leetcode.com/problems/populating-next-right-pointers-in-each-node-ii) | [My Solution](./problems/problem0117) ||
108108
| 0118 | [Pascal's Triangle](https://leetcode.com/problems/pascals-triangle) | [My Solution](./problems/problem0118) ||
109+
| 0119 | [Pascal's Triangle II](https://leetcode.com/problems/pascals-triangle-ii) | [My Solution](./problems/problem0119) ||
109110
| 0120 | [Triangle](https://leetcode.com/problems/triangle) | [My Solution](./problems/problem0120) ||
110111
| 0121 | [Best Time to Buy and Sell Stock](https://leetcode.com/problems/best-time-to-buy-and-sell-stock) | [My Solution](./problems/problem0121) ||
111112
| 0122 | [Best Time to Buy and Sell Stock II](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii) | [My Solution](./problems/problem0122) ||

0 commit comments

Comments
 (0)