Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 6f0fd6f

Browse files
committedNov 14, 2021
add some solution
1 parent c72f59d commit 6f0fd6f

File tree

4 files changed

+74
-10
lines changed

4 files changed

+74
-10
lines changed
 

‎lcof/of024/Solution.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,19 @@ func reverseList(head *ListNode) *ListNode {
1515
return newHead
1616
}
1717

18-
//func reverseList2(head *ListNode) *ListNode {
19-
// var pre *ListNode
20-
// var next *ListNode
21-
// for head != nil {
22-
// next = head.Next
23-
// head.Next = pre
24-
// pre = head
25-
// head = next
18+
//func reverseList_2(head *ListNode) *ListNode {
19+
// var prev *ListNode
20+
// curr := head
21+
// for curr != nil {
22+
// next := curr.Next
23+
// curr.Next = prev
24+
// prev = curr
25+
// curr = next
2626
// }
27-
// return pre
27+
// return prev
2828
//}
2929

30-
//func reverseList3(head *ListNode) *ListNode {
30+
//func reverseList_3(head *ListNode) *ListNode {
3131
// var prev *ListNode
3232
//
3333
// for head != nil {

‎lcof/of024/Solution_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ type SolutionFuncType func(*ListNode) *ListNode
1313

1414
var SolutionFuncList = []SolutionFuncType{
1515
reverseList,
16+
//reverseList_2,
17+
//reverseList_3,
1618
}
1719

1820
// test info struct

‎lcof/of040/Solution.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package Solution
2+
3+
import (
4+
"sort"
5+
)
6+
7+
func getLeastNumbers(arr []int, k int) []int {
8+
ans := make([]int, k)
9+
sort.Ints(arr)
10+
for i := 0; i < k; i++ {
11+
ans[i] = arr[i]
12+
}
13+
return ans
14+
}

‎lcof/of040/Solution_test.go

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

0 commit comments

Comments
 (0)
Please sign in to comment.