Skip to content

Commit 5bfb52d

Browse files
committed
add some solutions
1 parent c43d16b commit 5bfb52d

File tree

1,419 files changed

+2170
-14938
lines changed

Some content is hidden

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

1,419 files changed

+2170
-14938
lines changed

cmd/config.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
11
package main
22

3-
type GitHub struct {
4-
}
3+
type GitHub struct{}

cmd/contribution.go

+6-4
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,12 @@ type Contributor struct {
1818
ContributionType []string `json:"contribution_type"`
1919
}
2020

21-
const GITHUB_CONTRIBUTOR_API_URL = "https://api.github.com/repos/kylesliu/awesome-golang-algorithm/contributors"
22-
const GITHUB_CONTRIBUTOR_TMPL_PATH = "./tpl/.all-contributorsrc"
21+
const (
22+
GITHUB_CONTRIBUTOR_API_URL = "https://api.github.com/repos/kylesliu/awesome-golang-algorithm/contributors"
23+
GITHUB_CONTRIBUTOR_TMPL_PATH = "./tpl/.all-contributorsrc"
24+
)
2325

2426
func getContributorBufer() []byte {
25-
2627
contributor_buffer := Request("GET", GITHUB_CONTRIBUTOR_API_URL, nil)
2728

2829
return contributor_buffer
@@ -57,14 +58,15 @@ func GetContributorInstance() []Contributor {
5758
}
5859
return contributors
5960
}
61+
6062
func getContributorTemplate() string {
6163
buffer := ReadFile(GITHUB_CONTRIBUTOR_TMPL_PATH)
6264
return string(buffer)
6365
}
6466

6567
func GenerateContributorTemplete() {
6668
tpl_str := getContributorTemplate()
67-
//fmt.Println(tpl_str)
69+
// fmt.Println(tpl_str)
6870
contributors := GetContributorInstance()
6971

7072
fmt.Println(contributors)

cmd/github/contribution.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,13 @@ type Contributor struct {
1212
Url string `json:"url"`
1313
}
1414

15-
const GITHUB_CONTRIBUTOR_API_URL = "https://api.github.com/repos/kylesliu/awesome-golang-algorithm/contributors"
16-
const GITHUB_CONTRIBUTOR_TMPL_PATH = "cmd/template/contributors/.all-contributorsrc"
15+
const (
16+
GITHUB_CONTRIBUTOR_API_URL = "https://api.github.com/repos/kylesliu/awesome-golang-algorithm/contributors"
17+
GITHUB_CONTRIBUTOR_TMPL_PATH = "cmd/template/contributors/.all-contributorsrc"
18+
)
1719

1820
func getContributorBufer() []byte {
1921
request, err := http.Get(GITHUB_CONTRIBUTOR_API_URL)
20-
2122
if err != nil {
2223
log.Panicln("Lettcode Problem 接口获取失败:", err)
2324
}

cmd/leetcode.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ type Problem struct {
2424
Progress int `json:"progress"`
2525
PathName string `json:"path_name"`
2626
}
27+
2728
type Stat struct {
2829
QuestionID int `json:"question_id"`
2930
QuestionArticleLive bool `json:"question__article__live"`
@@ -42,5 +43,4 @@ type Difficulty struct {
4243
}
4344

4445
func GetAllleetcodeInstance() {
45-
4646
}

cmd/leetcode/makedir.go

+13-11
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,17 @@ import (
77
"os"
88
)
99

10-
const SOLUTIONS_PATH = "leetcode/"
11-
const SOURCE_SOLUTION_FILE_PATH = "cmd/template/solution/solution.go"
12-
const SOURCE_SOLUTION_TEST_FILE_PATH = "cmd/template/solution/solution_test.go"
13-
const SOURCE_SOLUTION_README_FILE_PATH = "cmd/template/solution/README.md"
10+
const (
11+
SOLUTIONS_PATH = "leetcode/"
12+
SOURCE_SOLUTION_FILE_PATH = "cmd/template/solution/solution.go"
13+
SOURCE_SOLUTION_TEST_FILE_PATH = "cmd/template/solution/solution_test.go"
14+
SOURCE_SOLUTION_README_FILE_PATH = "cmd/template/solution/README.md"
15+
)
1416

1517
// 生成木目录Dir
1618
func MakeDir(problems []Problem) {
1719
for i := 0; i < len(problems); i++ {
18-
//fmt.Println(problems[i].PathName)
20+
// fmt.Println(problems[i].PathName)
1921
log.Printf("~~ 开始生成第 %d 题的文件夹 ~~", problems[i].Stat.FrontendQuestionID)
2022

2123
// 检查数据
@@ -46,29 +48,29 @@ func MakeDir(problems []Problem) {
4648
}
4749
}
4850

49-
//拷贝文件 要拷贝的文件路径 拷贝到哪里
51+
// 拷贝文件 要拷贝的文件路径 拷贝到哪里
5052
func copyFile(source, dest string) bool {
5153
if source == "" || dest == "" {
5254
log.Println("source or dest is null")
5355
return false
5456
}
55-
//打开文件资源
57+
// 打开文件资源
5658
source_open, err := os.Open(source)
57-
//养成好习惯。操作文件时候记得添加 defer 关闭文件资源代码
59+
// 养成好习惯。操作文件时候记得添加 defer 关闭文件资源代码
5860
if err != nil {
5961
log.Println(err.Error())
6062
return false
6163
}
6264
defer source_open.Close()
63-
//只写模式打开文件 如果文件不存在进行创建 并赋予 644的权限。详情查看linux 权限解释
65+
// 只写模式打开文件 如果文件不存在进行创建 并赋予 644的权限。详情查看linux 权限解释
6466
dest_open, err := os.OpenFile(dest, os.O_CREATE|os.O_WRONLY, 644)
6567
if err != nil {
6668
log.Println(err.Error())
6769
return false
6870
}
69-
//养成好习惯。操作文件时候记得添加 defer 关闭文件资源代码
71+
// 养成好习惯。操作文件时候记得添加 defer 关闭文件资源代码
7072
defer dest_open.Close()
71-
//进行数据拷贝
73+
// 进行数据拷贝
7274
_, copy_err := io.Copy(dest_open, source_open)
7375
if copy_err != nil {
7476
log.Println(copy_err.Error())

cmd/leetcode/makedir_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,5 @@ func TestMakeDir(t *testing.T) {
1616
fmt.Println(v)
1717
}
1818

19-
//MakeDir(problems)
19+
// MakeDir(problems)
2020
}

cmd/leetcode/problem-readme.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ func GetReadmeTemplateBuffer() string {
2020

2121
func GenerateReadme(problem Problem) {
2222
log.Println("开始生成 README")
23-
file, err := os.OpenFile(SOURCE_SOLUTION_README_FILE_PATH, os.O_RDONLY, 0600)
23+
file, err := os.OpenFile(SOURCE_SOLUTION_README_FILE_PATH, os.O_RDONLY, 0o600)
2424
defer file.Close()
2525
if err != nil {
2626
log.Panicf("README 模板读取失败1:%s", err.Error())

cmd/leetcode/problem.go

+3-4
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ type Problem struct {
3636
PathName string `json:"path_name"`
3737
DirPath string `json:"dir_path"`
3838
}
39+
3940
type Stat struct {
4041
QuestionID int `json:"question_id"`
4142
QuestionArticleLive bool `json:"question__article__live"`
@@ -59,15 +60,14 @@ func GetAllProblemsPath() []string {
5960
res := []string{}
6061
time.Sleep(time.Second)
6162

62-
for i, _ := range problems {
63+
for i := range problems {
6364
res = append(res, problems[i].PathName)
64-
//fmt.Println(problems[i].Stat.QuestionTitle)
65+
// fmt.Println(problems[i].Stat.QuestionTitle)
6566
}
6667
return res
6768
}
6869

6970
func GetProblemsInstance() []Problem {
70-
7171
leetcode := new(LeetCode)
7272
Problemsbuffer := getProblemsBuffer()
7373

@@ -118,7 +118,6 @@ func GetProblemsJson() string {
118118
// 获取题目Buffer
119119
func getProblemsBuffer() []byte {
120120
request, err := http.Get("https://leetcode.com/api/problems/Algorithms/")
121-
122121
if err != nil {
123122
log.Panicln("Lettcode Problem 接口获取失败:", err)
124123
}

cmd/leetcode/summary.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ type TodoPageData struct {
2525
func MakeGitbookSummary(problems []Problem) {
2626
problems = CheckProblemExists(problems)
2727

28-
file, err := os.OpenFile(SOURCE_SOLUTION_SUMMARY_FILE_PATH, os.O_RDONLY, 0600)
28+
file, err := os.OpenFile(SOURCE_SOLUTION_SUMMARY_FILE_PATH, os.O_RDONLY, 0o600)
2929
defer file.Close()
3030
if err != nil {
3131
log.Panicf("README 模板读取失败1:%s", err.Error())
@@ -42,6 +42,7 @@ func MakeGitbookSummary(problems []Problem) {
4242
err = tmpl.Execute(&tmpRes, problems)
4343
write("SUMMARY.md", string(tmpRes.Bytes()))
4444
}
45+
4546
func CheckProblemExists(problems []Problem) []Problem {
4647
tmp := []Problem{}
4748

@@ -57,7 +58,7 @@ func CheckProblemExists(problems []Problem) []Problem {
5758
}
5859

5960
func write(path, content string) {
60-
err := ioutil.WriteFile(path, []byte(content), 0755)
61+
err := ioutil.WriteFile(path, []byte(content), 0o755)
6162
if err != nil {
6263
log.Fatal(err)
6364
}
@@ -79,5 +80,4 @@ func readTMPL(path string) string {
7980
}
8081

8182
func getSUMMARYBuffer(filepath string) {
82-
8383
}

cmd/main.go

+4-6
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
package main
22

33
import (
4-
"fmt"
5-
64
"awesome-golang-algorithm/cmd/leetcode"
5+
"fmt"
76
)
87

98
func main() {
@@ -25,15 +24,14 @@ func main() {
2524

2625
// 生成Problem 目录
2726

28-
//leetcode.MakeDir(problems)
27+
// leetcode.MakeDir(problems)
2928

30-
//leetcode.GetReadmeTemplateBuffer()
29+
// leetcode.GetReadmeTemplateBuffer()
3130

3231
// GitBook
33-
//leetcode.MakeGitbookSummary(problems)
32+
// leetcode.MakeGitbookSummary(problems)
3433

3534
// sitemap
3635
// s := sitemap.New(problems)
3736
// fmt.Println(s)
38-
3937
}

cmd/template/solution/Solution_test.go

-2
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,8 @@ func TestSolution(t *testing.T) {
3232

3333
// 压力测试
3434
func BenchmarkSolution(b *testing.B) {
35-
3635
}
3736

3837
// 使用案列
3938
func ExampleSolution() {
40-
4139
}

cmd/util.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ func ReadFile(path string) []byte {
2222

2323
// Write file
2424
func WriteFile(path, content string) {
25-
err := ioutil.WriteFile(path, []byte(content), 0755)
25+
err := ioutil.WriteFile(path, []byte(content), 0o755)
2626
if err != nil {
2727
log.Fatalln(err.Error())
2828
}

cmd/util/file.go

-1
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,4 @@ func WriteFile(path, s string) {
1313
defer f.Close()
1414

1515
f.WriteString(s)
16-
1716
}

lcof/of000/Solution_test.go

+10-16
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
package Solution
22

33
import (
4+
"fmt"
45
"reflect"
56
"runtime"
7+
"strings"
68
"testing"
79

810
"github.com/stretchr/testify/assert"
@@ -22,31 +24,23 @@ type Case struct {
2224
expect bool
2325
}
2426

25-
// test case
27+
// Test case
2628
var cases = []Case{
27-
{
28-
name: "TestCase 1",
29-
inputs: true,
30-
expect: true,
31-
},
32-
{
33-
name: "TestCase 2",
34-
inputs: false,
35-
expect: false,
36-
},
29+
{name: "TestCase 1", inputs: true, expect: true},
30+
{name: "TestCase 2", inputs: false, expect: false},
3731
}
3832

3933
// TestSolution Example for solution test cases
4034
func TestSolution(t *testing.T) {
4135
ast := assert.New(t)
4236

4337
for _, f := range SolutionFuncList {
38+
funcName := strings.Split(runtime.FuncForPC(reflect.ValueOf(f).Pointer()).Name(), ".")[1]
4439
for _, c := range cases {
45-
t.Run(c.name, func(t *testing.T) {
46-
actual := f(c.inputs)
47-
ast.Equal(c.expect, actual,
48-
"func: %v case: %v ",
49-
runtime.FuncForPC(reflect.ValueOf(f).Pointer()).Name(), c.name)
40+
t.Run(fmt.Sprintf("%s %s", funcName, c.name), func(t *testing.T) {
41+
got := f(c.inputs)
42+
ast.Equal(c.expect, got,
43+
"func: %v case: %v ", funcName, c.name)
5044
})
5145
}
5246
}

lcof/of003/Solution.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import "sort"
55
// 排序 + 遍历
66
func findRepeatNumber(nums []int) int {
77
sort.Ints(nums)
8-
for idx, _ := range nums {
8+
for idx := range nums {
99
if idx > 0 {
1010
if nums[idx] == nums[idx-1] {
1111
return nums[idx]

lcof/of003/Solution_test.go

-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ type SolutionFuncType func([]int) int
1414
var SolutionFuncList = []SolutionFuncType{
1515
findRepeatNumber,
1616
findRepeatNumber2,
17-
1817
findRepeatNumber3,
1918
}
2019

@@ -58,5 +57,4 @@ func TestSolution(t *testing.T) {
5857
})
5958
}
6059
}
61-
6260
}

lcof/of004/Solution_test.go

+7-6
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
package Solution
22

33
import (
4-
"github.com/stretchr/testify/assert"
54
"reflect"
65
"runtime"
76
"testing"
7+
8+
"github.com/stretchr/testify/assert"
89
)
910

1011
// solution func Info
@@ -27,18 +28,18 @@ var cases = []Case{
2728
{
2829
name: "TestCase 1",
2930
board: [][]int{
30-
[]int{1, 2, 8, 9},
31+
{1, 2, 8, 9},
3132
},
3233
target: 7,
3334
expect: false,
3435
},
3536
{
3637
name: "TestCase 2",
3738
board: [][]int{
38-
[]int{1, 2, 8, 9},
39-
[]int{2, 4, 9, 12},
40-
[]int{4, 7, 10, 13},
41-
[]int{6, 8, 11, 15},
39+
{1, 2, 8, 9},
40+
{2, 4, 9, 12},
41+
{4, 7, 10, 13},
42+
{6, 8, 11, 15},
4243
},
4344
target: 7,
4445
expect: true,

lcof/of006/Solution_test.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,10 @@ var cases = []Case{
3232
},
3333
{
3434
name: "TestCase 2",
35-
inputs: &ListNode{Val: 1,
36-
Next: &ListNode{Val: 2, Next: &ListNode{Val: 3}}},
35+
inputs: &ListNode{
36+
Val: 1,
37+
Next: &ListNode{Val: 2, Next: &ListNode{Val: 3}},
38+
},
3739
expect: []int{3, 2, 1},
3840
},
3941
}

lcof/of007/Solution.go

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ func buildTree(pre []int, in []int) *TreeNode {
1818
Right: buildTree(pre[mid+1:], in[mid+1:]),
1919
}
2020
}
21+
2122
func search(nodes []int, val int) int {
2223
for p, v := range nodes {
2324
if v == val {

0 commit comments

Comments
 (0)