Skip to content

Commit a673eb1

Browse files
committed
feat: add template
1 parent 76966de commit a673eb1

File tree

5 files changed

+180
-2
lines changed

5 files changed

+180
-2
lines changed

.vscode/launch.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
"showLog": true,
3030
"asRoot": false,
3131
"trace": "verbose",
32-
"stopOnEntry": true,
32+
// "stopOnEntry": true,
3333
"debugAdapter": "dlv-dap"
3434
},
3535
]

Leetcode/1.Two-Sum/README.md

+110
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
---
2+
title: 1.Two-Sum
3+
subtitle: "https://leetcode.com/problems/two-sum/description/"
4+
date: 2025-04-11T20:16:46+08:00
5+
lastmod: 2025-04-11T20:16:46+08:00
6+
draft: false
7+
author: "Kimi.Tsai"
8+
authorLink: "https://kimi0230.github.io/"
9+
description: "Two-Sum"
10+
license: ""
11+
images: []
12+
13+
tags: [LeetCode, Go, Easy, Array, Hash Table]
14+
categories: [LeetCode]
15+
16+
featuredImage: ""
17+
featuredImagePreview: ""
18+
19+
hiddenFromHomePage: false
20+
hiddenFromSearch: false
21+
twemoji: false
22+
lightgallery: true
23+
ruby: true
24+
fraction: true
25+
fontawesome: true
26+
linkToMarkdown: false
27+
rssFullText: false
28+
29+
toc:
30+
enable: true
31+
auto: true
32+
code:
33+
copy: true
34+
maxShownLines: 200
35+
math:
36+
enable: false
37+
mapbox:
38+
share:
39+
enable: true
40+
comment:
41+
enable: true
42+
library:
43+
css:
44+
js:
45+
seo:
46+
images: []
47+
---
48+
49+
# [1.Two-Sum](https://leetcode.com/problems/two-sum/description/)
50+
51+
## 題目
52+
Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target.
53+
54+
You may assume that each input would have exactly one solution, and you may not use the same element twice.
55+
56+
You can return the answer in any order.
57+
58+
 
59+
Example 1:
60+
61+
62+
Input: nums = [2,7,11,15], target = 9
63+
Output: [0,1]
64+
Explanation: Because nums[0] + nums[1] == 9, we return [0, 1].
65+
66+
67+
Example 2:
68+
69+
70+
Input: nums = [3,2,4], target = 6
71+
Output: [1,2]
72+
73+
74+
Example 3:
75+
76+
77+
Input: nums = [3,3], target = 6
78+
Output: [0,1]
79+
80+
81+
 
82+
Constraints:
83+
84+
85+
2 <= nums.length <= 104
86+
-109 <= nums[i] <= 109
87+
-109 <= target <= 109
88+
Only one valid answer exists.
89+
90+
91+
 
92+
Follow-up: Can you come up with an algorithm that is less than O(n2) time complexity?
93+
94+
## 題目大意
95+
96+
97+
## 解題思路
98+
99+
100+
## 來源
101+
* https://leetcode.com/problems/two-sum/description/
102+
103+
## 解答
104+
https://github.com/kimi0230/LeetcodeGolang/blob/master/Leetcode/1.Two-Sum/main.go
105+
106+
107+
108+
## Benchmark
109+
110+

Leetcode/1.Two-Sum/main.go

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
func twoSum(nums []int, target int) []int {
2+
3+
}

Leetcode/1.Two-Sum/main_test.go

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package main
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func TestExample(t *testing.T) {
8+
// TODO: add test cases
9+
}

cmd/leetcode-readme-gen.go

+57-1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ type LeetcodeResponse struct {
2525
Tags []struct {
2626
Name string `json:"name"`
2727
} `json:"topicTags"`
28+
CodeSnippets []struct {
29+
Lang string `json:"lang"`
30+
Code string `json:"code"`
31+
} `json:"codeSnippets"`
2832
} `json:"question"`
2933
} `json:"data"`
3034
}
@@ -117,6 +121,7 @@ type ReadmeData struct {
117121
Difficulty string
118122
Tags []string
119123
Problem string
124+
Code string
120125
Now string
121126
}
122127

@@ -157,6 +162,10 @@ func fetchLeetcode(slug string) (*ReadmeData, error) {
157162
topicTags {
158163
name
159164
}
165+
codeSnippets {
166+
lang
167+
code
168+
}
160169
}
161170
}`
162171

@@ -184,13 +193,22 @@ func fetchLeetcode(slug string) (*ReadmeData, error) {
184193
tags = append(tags, tag.Name)
185194
}
186195

196+
code := ""
197+
for _, snippet := range q.CodeSnippets {
198+
if snippet.Lang == "Go" || snippet.Lang == "Golang" {
199+
code = snippet.Code
200+
break
201+
}
202+
}
203+
187204
return &ReadmeData{
188205
ID: q.QuestionID,
189206
Title: strings.ReplaceAll(q.Title, " ", "-"),
190207
Slug: slug,
191208
Difficulty: q.Difficulty,
192209
Tags: tags,
193210
Problem: stripHTML(q.Content),
211+
Code: code,
194212
Now: time.Now().Format(time.RFC3339),
195213
}, nil
196214
}
@@ -228,6 +246,38 @@ func writeREADME(data *ReadmeData) error {
228246
return tmpl.Execute(f, data)
229247
}
230248

249+
func writeMainGo(data *ReadmeData) error {
250+
dir := fmt.Sprintf("Leetcode/%s.%s", data.ID, data.Title)
251+
f, err := os.Create(filepath.Join(dir, "main.go"))
252+
if err != nil {
253+
return err
254+
}
255+
defer f.Close()
256+
_, err = f.WriteString(data.Code)
257+
return err
258+
}
259+
260+
func writeMainTest(data *ReadmeData) error {
261+
dir := fmt.Sprintf("Leetcode/%s.%s", data.ID, data.Title)
262+
f, err := os.Create(filepath.Join(dir, "main_test.go"))
263+
if err != nil {
264+
return err
265+
}
266+
defer f.Close()
267+
testContent := `package main
268+
269+
import (
270+
"testing"
271+
)
272+
273+
func TestExample(t *testing.T) {
274+
// TODO: add test cases
275+
}
276+
`
277+
_, err = f.WriteString(testContent)
278+
return err
279+
}
280+
231281
func main() {
232282
if len(os.Args) < 2 {
233283
fmt.Println("Usage: leetcode-readme-gen <id>")
@@ -252,5 +302,11 @@ func main() {
252302
if err := writeREADME(data); err != nil {
253303
panic(err)
254304
}
255-
fmt.Printf("README generated for %s (%s)\n", data.Title, data.ID)
305+
if err := writeMainGo(data); err != nil {
306+
panic(err)
307+
}
308+
if err := writeMainTest(data); err != nil {
309+
panic(err)
310+
}
311+
fmt.Printf("README, main.go, and main_test.go generated for %s (%s)\n", data.Title, data.ID)
256312
}

0 commit comments

Comments
 (0)