Skip to content

Commit 7015b87

Browse files
committed
add some solution
1 parent 27068e4 commit 7015b87

17 files changed

+533
-11
lines changed

lcof/of010-I/Solution.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,29 @@ package Solution
33
import "math"
44

55
// 递归题解
6-
func Fibonacci(n int) int {
6+
func fib(n int) int {
77
if n == 0 || n == 1 {
88
return n
99
}
10-
return Fibonacci(n-1) + Fibonacci(n-2)
10+
return (fib(n-1) + fib(n-2)) % 1000000007
1111
}
1212

1313
// 自顶向下推导
14-
func Fibonacci2(n int) int {
15-
a, b := 0, 1
14+
func fib2(n int) int {
15+
a, b, sum := 0, 1, 0
1616

1717
for i := 1; i <= n; i++ {
18-
a = a + b
19-
b = a - b
18+
sum = (a + b) % 1000000007
19+
a = b
20+
b = sum
2021
}
2122
return a
2223
}
2324

2425
// 推导公式1
25-
func Fibonacci3(n int) int {
26+
func fib3(n int) int {
2627
a := math.Sqrt(5) / 5
2728
b := math.Pow((1+math.Sqrt(5))/2, float64(n))
2829
c := math.Pow((1-math.Sqrt(5))/2, float64(n))
29-
return int(a * (b - c))
30+
return int(a*(b-c)) % 1000000007
3031
}

lcof/of010-I/Solution_test.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ import (
1111
type SolutionFuncType func(int) int
1212

1313
var SolutionFuncList = []SolutionFuncType{
14-
Fibonacci,
15-
Fibonacci2,
16-
Fibonacci3,
14+
fib,
15+
fib2,
16+
fib3,
1717
}
1818

1919
// test info struct
@@ -40,6 +40,11 @@ var cases = []Case{
4040
inputs: 13,
4141
expect: 233,
4242
},
43+
{
44+
name: "TestCase 3",
45+
inputs: 45,
46+
expect: 134903163,
47+
},
4348
}
4449

4550
// TestSolution Example for solution test cases

lcof/of042/README.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# [OF0.Template Title][title]
2+
3+
> [!WARNING|style:flat]
4+
> This problem is temporarily not PR, please submit [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
5+
6+
7+
## 题目描述
8+
....
9+
10+
**范例 :**
11+
12+
```
13+
Input: n=1
14+
Output: 1
15+
```
16+
17+
## 题意
18+
> ...
19+
20+
## 题解
21+
22+
### 思路1
23+
> ...
24+
```go
25+
```
26+
27+
## 结语
28+
29+
如果你同我一样热爱数据结构、算法、LeetCode,可以关注我 GitHub 上的 LeetCode 题解:[awesome-golang-algorithm][me]
30+
31+
[title]: https://www.nowcoder.com/practice/c6c7742f5ba7442aada113136ddea0c3/
32+
[me]: https://github.com/kylesliu/awesome-golang-algorithm

lcof/of042/Solution.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package Solution
2+
3+
func maxSubArray(nums []int) int {
4+
ans := nums[0]
5+
for i := 1; i < len(nums); i++ {
6+
if nums[i-1] > 0 {
7+
nums[i] += nums[i-1]
8+
}
9+
ans = max(ans, nums[i])
10+
}
11+
return ans
12+
}
13+
14+
func max(x, y int) int {
15+
if x > y {
16+
return x
17+
}
18+
return y
19+
}

lcof/of042/Solution_test.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package Solution
2+
3+
import (
4+
"github.com/stretchr/testify/assert"
5+
"reflect"
6+
"runtime"
7+
"testing"
8+
)
9+
10+
// solution func Info
11+
type SolutionFuncType func([]int) int
12+
13+
var SolutionFuncList = []SolutionFuncType{
14+
maxSubArray,
15+
}
16+
17+
// test info struct
18+
type Case struct {
19+
name string
20+
inputs []int
21+
expect int
22+
}
23+
24+
// test case
25+
var cases = []Case{
26+
{
27+
name: "TestCase 1",
28+
inputs: []int{-2, 1, -3, 4, -1, 2, 1, -5, 4},
29+
expect: 6,
30+
},
31+
}
32+
33+
// TestSolution Example for solution test cases
34+
func TestSolution(t *testing.T) {
35+
ast := assert.New(t)
36+
37+
for _, f := range SolutionFuncList {
38+
for _, c := range cases {
39+
t.Run(c.name, func(t *testing.T) {
40+
actual := f(c.inputs)
41+
ast.Equal(c.expect, actual,
42+
"func: %v case: %v ",
43+
runtime.FuncForPC(reflect.ValueOf(f).Pointer()).Name(), c.name)
44+
})
45+
}
46+
}
47+
}

lcof/of047/README.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# [OF0.Template Title][title]
2+
3+
> [!WARNING|style:flat]
4+
> This problem is temporarily not PR, please submit [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
5+
6+
7+
## 题目描述
8+
....
9+
10+
**范例 :**
11+
12+
```
13+
Input: n=1
14+
Output: 1
15+
```
16+
17+
## 题意
18+
> ...
19+
20+
## 题解
21+
22+
### 思路1
23+
> ...
24+
```go
25+
```
26+
27+
## 结语
28+
29+
如果你同我一样热爱数据结构、算法、LeetCode,可以关注我 GitHub 上的 LeetCode 题解:[awesome-golang-algorithm][me]
30+
31+
[title]: https://www.nowcoder.com/practice/c6c7742f5ba7442aada113136ddea0c3/
32+
[me]: https://github.com/kylesliu/awesome-golang-algorithm

lcof/of047/Solution.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package Solution
2+
3+
func maxValue(grid [][]int) int {
4+
row, col := len(grid), len(grid[0])
5+
for i := 0; i < row; i++ {
6+
for j := 0; j < col; j++ {
7+
if i == 0 && j == 0 {
8+
continue
9+
} else if i == 0 {
10+
grid[i][j] += grid[i][j-1]
11+
} else if j == 0 {
12+
grid[i][j] += grid[i-1][j]
13+
} else {
14+
grid[i][j] += max(grid[i][j-1], grid[i-1][j])
15+
}
16+
}
17+
}
18+
return grid[row-1][col-1]
19+
}
20+
21+
func max(x, y int) int {
22+
if x > y {
23+
return x
24+
}
25+
return y
26+
}

lcof/of047/Solution_test.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package Solution
2+
3+
import (
4+
"github.com/stretchr/testify/assert"
5+
"reflect"
6+
"runtime"
7+
"testing"
8+
)
9+
10+
// solution func Info
11+
type SolutionFuncType func([][]int) int
12+
13+
var SolutionFuncList = []SolutionFuncType{
14+
maxValue,
15+
}
16+
17+
// test info struct
18+
type Case struct {
19+
name string
20+
inputs [][]int
21+
expect int
22+
}
23+
24+
// test case
25+
var cases = []Case{
26+
{
27+
name: "TestCase 1",
28+
inputs: [][]int{
29+
{1, 3, 1},
30+
{1, 5, 1},
31+
{4, 2, 1},
32+
},
33+
expect: 12,
34+
},
35+
}
36+
37+
// TestSolution Example for solution test cases
38+
func TestSolution(t *testing.T) {
39+
ast := assert.New(t)
40+
41+
for _, f := range SolutionFuncList {
42+
for _, c := range cases {
43+
t.Run(c.name, func(t *testing.T) {
44+
actual := f(c.inputs)
45+
ast.Equal(c.expect, actual,
46+
"func: %v case: %v ",
47+
runtime.FuncForPC(reflect.ValueOf(f).Pointer()).Name(), c.name)
48+
})
49+
}
50+
}
51+
}

lcof/of049/README.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# [OF0.Template Title][title]
2+
3+
> [!WARNING|style:flat]
4+
> This problem is temporarily not PR, please submit [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)
5+
6+
7+
## 题目描述
8+
....
9+
10+
**范例 :**
11+
12+
```
13+
Input: n=1
14+
Output: 1
15+
```
16+
17+
## 题意
18+
> ...
19+
20+
## 题解
21+
22+
### 思路1
23+
> ...
24+
```go
25+
```
26+
27+
## 结语
28+
29+
如果你同我一样热爱数据结构、算法、LeetCode,可以关注我 GitHub 上的 LeetCode 题解:[awesome-golang-algorithm][me]
30+
31+
[title]: https://www.nowcoder.com/practice/c6c7742f5ba7442aada113136ddea0c3/
32+
[me]: https://github.com/kylesliu/awesome-golang-algorithm

lcof/of049/Solution.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package Solution
2+
3+
func nthUglyNumber(n int) int {
4+
a, b, c, dp := 0, 0, 0, make([]int, n)
5+
dp[0] = 1
6+
7+
for i := 1; i < n; i++ {
8+
n2, n3, n5 := dp[a]*2, dp[b]*3, dp[c]*5
9+
dp[i] = min(min(n2, n3), n5)
10+
if dp[i] == n2 {
11+
a++
12+
}
13+
if dp[i] == n3 {
14+
b++
15+
}
16+
if dp[i] == n5 {
17+
c++
18+
}
19+
}
20+
return dp[n-1]
21+
}
22+
func min(x, y int) int {
23+
if x > y {
24+
return y
25+
}
26+
return x
27+
}

lcof/of049/Solution_test.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package Solution
2+
3+
import (
4+
"github.com/stretchr/testify/assert"
5+
"reflect"
6+
"runtime"
7+
"testing"
8+
)
9+
10+
// solution func Info
11+
type SolutionFuncType func(int) int
12+
13+
var SolutionFuncList = []SolutionFuncType{
14+
nthUglyNumber,
15+
}
16+
17+
// test info struct
18+
type Case struct {
19+
name string
20+
inputs int
21+
expect int
22+
}
23+
24+
// test case
25+
var cases = []Case{
26+
{
27+
name: "TestCase 1",
28+
inputs: 10,
29+
expect: 12,
30+
},
31+
}
32+
33+
// TestSolution Example for solution test cases
34+
func TestSolution(t *testing.T) {
35+
ast := assert.New(t)
36+
37+
for _, f := range SolutionFuncList {
38+
for _, c := range cases {
39+
t.Run(c.name, func(t *testing.T) {
40+
actual := f(c.inputs)
41+
ast.Equal(c.expect, actual,
42+
"func: %v case: %v ",
43+
runtime.FuncForPC(reflect.ValueOf(f).Pointer()).Name(), c.name)
44+
})
45+
}
46+
}
47+
}

0 commit comments

Comments
 (0)