Skip to content

Commit 428eb5a

Browse files
committed
add some solution for offer
1 parent d74da70 commit 428eb5a

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

67 files changed

+878
-1368
lines changed

Makefile

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@ VERSION = `git rev-parse --short HEAD`
22

33
SHELL := /bin/bash
44

5-
all: test-nowcoder test-leetcode
5+
all: test-lcof test-leetcode
66
@echo all
77
#make test-nowcoder
88
#make test-leetcode
99

10-
test-nowcoder:
11-
@echo start test neworder
12-
sh test.sh nowcoder
13-
@echo end test neworder
10+
test-lcof:
11+
@echo start test lcof
12+
sh test.sh lcof
13+
@echo end test lcof
1414

1515
test-leetcode:
1616
@echo start test leetcode

nowcoder/jz-offer/jz000/README.md renamed to lcof/of017/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# [JZ0.Template Title][title]
1+
# [OF0.Template Title][title]
22

33
> [!WARNING|style:flat]
44
> This problem is temporarily not PR, please submit [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)

lcof/of017/Solution.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package Solution
2+
3+
func printNumbers(n int) []int {
4+
sum, ans := 1, []int{}
5+
for i := 0; i < n; i++ {
6+
sum *= 10
7+
}
8+
for i := 0; i < sum-1; i++ {
9+
ans = append(ans, i+1)
10+
}
11+
return ans
12+
}
13+
14+
func printNumbers2(n int) []int {
15+
sum, ans := 1, []int{}
16+
for i := 0; i < n; i++ {
17+
sum *= 10
18+
}
19+
for i := 0; i < sum-1; i++ {
20+
ans = append(ans, i+1)
21+
}
22+
return ans
23+
}

lcof/of017/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+
printNumbers,
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: 1,
29+
expect: []int{1, 2, 3, 4, 5, 6, 7, 8, 9},
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+
}

nowcoder/jz-offer/jz010/README.md renamed to lcof/of021/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# [JZ10.矩形覆盖][title]
1+
# [OF0.Template Title][title]
22

33
> [!WARNING|style:flat]
44
> This problem is temporarily not PR, please submit [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)

lcof/of021/Solution.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package Solution
2+
3+
func exchange(nums []int) []int {
4+
start, end := 0, len(nums)-1
5+
6+
for start < end {
7+
for start < end && (nums[start]%2 == 1) {
8+
start++
9+
}
10+
for start < end && (nums[end]%2 == 0) {
11+
end--
12+
}
13+
nums[start], nums[end] = nums[end], nums[start]
14+
}
15+
return nums
16+
}
17+
18+
func exchange2(nums []int) []int {
19+
ans := make([]int, 0, 0)
20+
for _, v := range nums {
21+
if v&1 != 0 {
22+
ans = append(ans, v)
23+
}
24+
}
25+
for _, v := range nums {
26+
if v&1 == 0 {
27+
ans = append(ans, v)
28+
}
29+
}
30+
return ans
31+
}
32+
33+
func exchange3(nums []int) []int {
34+
i := 0
35+
36+
for j := 0; j < len(nums); j++ {
37+
if nums[j]&1 == 1 {
38+
tmp := nums[j]
39+
for k := j - 1; k >= i; k-- {
40+
nums[k+1] = nums[k]
41+
}
42+
nums[i] = tmp
43+
i += 1
44+
}
45+
}
46+
return nums
47+
}

lcof/of021/Solution_test.go

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

nowcoder/jz-offer/jz022/README.md renamed to lcof/of029/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# [JZ0.Template Title][title]
1+
# [OF0.Template Title][title]
22

33
> [!WARNING|style:flat]
44
> This problem is temporarily not PR, please submit [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)

lcof/of029/Solution.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package Solution
2+
3+
func spiralOrder(matrix [][]int) []int {
4+
ans := []int{}
5+
6+
if len(matrix) == 0 {
7+
return ans
8+
}
9+
top, bottom, left, right := 0, len(matrix)-1, 0, len(matrix[0])-1
10+
11+
for top <= bottom && left <= right {
12+
for i := left; i <= right; i++ {
13+
ans = append(ans, matrix[top][i])
14+
}
15+
top++
16+
for i := top; i <= bottom; i++ {
17+
ans = append(ans, matrix[i][right])
18+
}
19+
right--
20+
if top <= bottom {
21+
for i := right; i >= left; i-- {
22+
ans = append(ans, matrix[bottom][i])
23+
}
24+
}
25+
bottom--
26+
if left <= right {
27+
for i := bottom; i >= top; i-- {
28+
ans = append(ans, matrix[i][left])
29+
}
30+
}
31+
left++
32+
}
33+
return ans
34+
}

lcof/of029/Solution_test.go

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
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+
spiralOrder,
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, 2, 3},
30+
{4, 5, 6},
31+
{7, 8, 9},
32+
},
33+
expect: []int{1, 2, 3, 6, 9, 8, 7, 4, 5},
34+
},
35+
{
36+
name: "TestCase 2",
37+
inputs: [][]int{
38+
{1, 2, 3, 4},
39+
{5, 6, 7, 8},
40+
{9, 10, 11, 12},
41+
},
42+
expect: []int{1, 2, 3, 4, 8, 12, 11, 10, 9, 5, 6, 7},
43+
},
44+
{
45+
name: "TestCase 3",
46+
inputs: [][]int{
47+
{1, 2, 3, 4},
48+
{5, 6, 7, 8},
49+
{9, 10, 11, 12},
50+
{13, 14, 15, 16},
51+
},
52+
expect: []int{1, 2, 3, 4, 8, 12, 16, 15, 14, 13, 9, 5, 6, 7, 11, 10},
53+
},
54+
{
55+
name: "TestCase 3",
56+
inputs: [][]int{
57+
{1, 2, 3, 4},
58+
},
59+
expect: []int{1, 2, 3, 4},
60+
},
61+
}
62+
63+
// TestSolution Example for solution test cases
64+
func TestSolution(t *testing.T) {
65+
ast := assert.New(t)
66+
67+
for _, f := range SolutionFuncList {
68+
for _, c := range cases {
69+
t.Run(c.name, func(t *testing.T) {
70+
actual := f(c.inputs)
71+
ast.Equal(c.expect, actual,
72+
"func: %v case: %v ",
73+
runtime.FuncForPC(reflect.ValueOf(f).Pointer()).Name(), c.name)
74+
})
75+
}
76+
}
77+
}

nowcoder/jz-offer/jz009/README.md renamed to lcof/of039/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# [JZ9.变态跳台阶][title]
1+
# [OF0.Template Title][title]
22

33
> [!WARNING|style:flat]
44
> This problem is temporarily not PR, please submit [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm)

lcof/of039/Solution.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package Solution
2+
3+
func majorityElement(nums []int) int {
4+
cur, count := 0, 0
5+
for _, v := range nums {
6+
if count == 0 {
7+
cur = v
8+
}
9+
if v == cur {
10+
count++
11+
} else {
12+
count--
13+
}
14+
}
15+
return cur
16+
}

lcof/of039/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+
majorityElement,
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{1, 2, 3, 2, 2, 2, 5, 4, 2},
29+
expect: 2,
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)