Skip to content

Commit 7400e85

Browse files
committed
add problem 71
1 parent d281c9d commit 7400e85

File tree

13 files changed

+364
-39
lines changed

13 files changed

+364
-39
lines changed

cmd/README.md

-6
This file was deleted.

cmd/contribution.go

+80
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package main
2+
3+
import (
4+
"bytes"
5+
"encoding/json"
6+
"fmt"
7+
"html/template"
8+
"log"
9+
)
10+
11+
type Contributor struct {
12+
Login string `json:"login"`
13+
Avatar_Url string `json:"avatar_url"`
14+
Name string `json:"name"`
15+
Url string `json:"url"`
16+
Profile string `json:"profile"`
17+
Contributions int `json:"contributions"`
18+
ContributionType []string `json:"contribution_type"`
19+
}
20+
21+
const GITHUB_CONTRIBUTOR_API_URL = "https://api.github.com/repos/kylesliu/awesome-golang-leetcode/contributors"
22+
const GITHUB_CONTRIBUTOR_TMPL_PATH = "./tpl/.all-contributorsrc"
23+
24+
func getContributorBufer() []byte {
25+
26+
contributor_buffer := Request("GET", GITHUB_CONTRIBUTOR_API_URL, nil)
27+
28+
return contributor_buffer
29+
}
30+
31+
func GetContributorString() string {
32+
str := string(getContributorBufer())
33+
return str
34+
}
35+
36+
// get the josnmarshal json
37+
func GetContributorJosnMarshal(prefix, indent string) string {
38+
contributors := []Contributor{}
39+
40+
if err := json.Unmarshal(getContributorBufer(), &contributors); err != nil {
41+
log.Fatalln(err.Error())
42+
}
43+
44+
contributors_buffer, err := json.MarshalIndent(contributors, prefix, indent)
45+
if err != nil {
46+
log.Fatalln(err.Error())
47+
}
48+
49+
return string(contributors_buffer)
50+
}
51+
52+
func GetContributorInstance() []Contributor {
53+
contributors := []Contributor{}
54+
55+
if err := json.Unmarshal(getContributorBufer(), &contributors); err != nil {
56+
log.Fatalln(err.Error())
57+
}
58+
return contributors
59+
}
60+
func getContributorTemplate() string {
61+
buffer := ReadFile(GITHUB_CONTRIBUTOR_TMPL_PATH)
62+
return string(buffer)
63+
}
64+
65+
func GenerateContributorTemplete() {
66+
tpl_str := getContributorTemplate()
67+
//fmt.Println(tpl_str)
68+
contributors := GetContributorInstance()
69+
70+
fmt.Println(contributors)
71+
// Tmpl
72+
var tmpRes bytes.Buffer
73+
74+
tmpl, err := template.New("Contributors: ").Parse(tpl_str)
75+
if err != nil {
76+
log.Printf("%s", err)
77+
}
78+
err = tmpl.Execute(&tmpRes, contributors)
79+
fmt.Println(string(tmpRes.Bytes()))
80+
}

cmd/contribution_test.go

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
)
7+
8+
func TestGetContributorString(t *testing.T) {
9+
str := GetContributorString()
10+
fmt.Println(str)
11+
}
12+
13+
func TestGetContributorJosnMarshal(t *testing.T) {
14+
str := GetContributorJosnMarshal("", " ")
15+
fmt.Println(str)
16+
}
17+
18+
func TestGetContributorInstance(t *testing.T) {
19+
contributors := GetContributorInstance()
20+
fmt.Println(contributors)
21+
}
22+
23+
func TestGenerateContributorTemplete(t *testing.T) {
24+
GenerateContributorTemplete()
25+
}

cmd/getinfo.sh

-7
This file was deleted.

cmd/github/contribution.go

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

15-
const GITHUB_CONTRIBUTOR_API_PATH = "https://api.github.com/repos/kylesliu/awesome-golang-leetcode/contributors"
15+
const GITHUB_CONTRIBUTOR_API_URL = "https://api.github.com/repos/kylesliu/awesome-golang-leetcode/contributors"
16+
const GITHUB_CONTRIBUTOR_TMPL_PATH = "cmd/template/contributors/.all-contributorsrc"
1617

1718
func getContributorBufer() []byte {
18-
request, err := http.Get(GITHUB_CONTRIBUTOR_API_PATH)
19+
request, err := http.Get(GITHUB_CONTRIBUTOR_API_URL)
1920

2021
if err != nil {
2122
log.Panicln("Lettcode Problem 接口获取失败:", err)

cmd/main.go

+2
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,7 @@ func main() {
1616

1717
//leetcode.GetReadmeTemplateBuffer()
1818

19+
// GitBook
1920
leetcode.MakeGitbookSummary(problems)
21+
2022
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"projectName": "awesome-golang-leetcode",
3+
"projectOwner": "kylesliu",
4+
"repoType": "github",
5+
"repoHost": "https://github.com",
6+
"files": [
7+
"README.md"
8+
],
9+
"imageSize": 100,
10+
"commit": true,
11+
"contributors": [
12+
{
13+
"login": "kylesliu",
14+
"name": "Kyle Liu ",
15+
"avatar_url": "https://avatars0.githubusercontent.com/u/26195433?v=4",
16+
"profile": "https://kyle.link",
17+
"contributions": [
18+
"code",
19+
"blog",
20+
"design",
21+
"doc"
22+
]
23+
}
24+
]
25+
}

cmd/tpl/.all-contributorsrc

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
{
2+
"projectName": "awesome-golang-leetcode",
3+
"projectOwner": "kylesliu",
4+
"repoType": "github",
5+
"repoHost": "https://github.com",
6+
"files": [
7+
"README.md"
8+
],
9+
"imageSize": 100,
10+
"commit": true,
11+
"contributors": [
12+
13+
{{ range $i, $v := . }}
14+
{{ if eq $v.Login "kylesiu"}}
15+
{
16+
"login": "kylesliu",
17+
"name": "Kyle Liu",
18+
"avatar_url": "https://avatars0.githubusercontent.com/u/26195433?v=4",
19+
"profile": "https://github.com/kylesliu",
20+
"contributions": [
21+
"code",
22+
"blog",
23+
"design",
24+
"doc"
25+
]
26+
},
27+
{{else}}
28+
{
29+
"login": "{{ .Login }}",
30+
"name": "{{$v.Login}}",
31+
"avatar_url": "{{ .Avatar_Url }}",
32+
"profile": "{{ .Url }}",
33+
"contributions": [
34+
"code"
35+
]
36+
},
37+
{{ end }}
38+
{{ end }}
39+
]
40+
}

cmd/util.go

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package main
2+
3+
import (
4+
"io"
5+
"io/ioutil"
6+
"log"
7+
"net/http"
8+
"os"
9+
)
10+
11+
// Get file by path
12+
func ReadFile(path string) []byte {
13+
file, err := os.Open(path)
14+
defer file.Close()
15+
if err != nil {
16+
log.Fatalln(err.Error())
17+
}
18+
19+
data, err := ioutil.ReadAll(file)
20+
return data
21+
}
22+
23+
// Write file
24+
func WriteFile(path, content string) {
25+
err := ioutil.WriteFile(path, []byte(content), 0755)
26+
if err != nil {
27+
log.Fatalln(err.Error())
28+
}
29+
}
30+
31+
func Request(method, url string, data interface{}) []byte {
32+
if method == "GET" {
33+
resp, err := http.Get(url)
34+
if err != nil || resp.StatusCode != 200 {
35+
log.Printf("HTTP Request Get Fail URL: %s, Status: %d, Data: %s", url, resp.StatusCode, ReadRequest(resp.Body))
36+
}
37+
resp_data, err := ioutil.ReadAll(resp.Body)
38+
if err != nil {
39+
log.Fatalln(err.Error())
40+
}
41+
return resp_data
42+
} else if method == "POST" {
43+
return nil
44+
}
45+
46+
return nil
47+
}
48+
49+
func ReadRequest(read io.Reader) string {
50+
req_buffer, err := ioutil.ReadAll(read)
51+
if err != nil {
52+
log.Printf("ReadRequest: %s", err.Error())
53+
}
54+
return string(req_buffer)
55+
}

cmd/util_test.go

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
)
7+
8+
func TestRequest(t *testing.T) {
9+
resp := Request("GET", GITHUB_CONTRIBUTOR_API_URL, nil)
10+
fmt.Println(string(resp))
11+
}

src/0071.Simplify-Path/README.md

+62-14
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,96 @@
1-
# [1. Add Sum][title]
1+
# [71. Simplify Path][title]
22

33
## Description
44

5-
Given two binary strings, return their sum (also a binary string).
5+
Given an absolute path for a file (Unix-style), simplify it. Or in other words, convert it to the canonical path.
66

7-
The input strings are both **non-empty** and contains only characters `1` or `0`.
7+
In a UNIX-style file system, a period . refers to the current directory. Furthermore, a double period .. moves the directory up a level. For more information, see: Absolute path vs relative path in Linux/Unix
88

9+
Note that the returned canonical path must always begin with a slash /, and there must be only a single slash / between two directory names. The last directory name (if it exists) must not end with a trailing /. Also, the canonical path must be the shortest string representing the absolute path.
910
**Example 1:**
1011

1112
```
12-
Input: a = "11", b = "1"
13-
Output: "100"
13+
Input: "/home/"
14+
Output: "/home"
15+
Explanation: Note that there is no trailing slash after the last directory name.
1416
```
1517

1618
**Example 2:**
1719

1820
```
19-
Input: a = "1010", b = "1011"
20-
Output: "10101"
21+
Input: "/../"
22+
Output: "/"
23+
Explanation: Going one level up from the root directory is a no-op, as the root level is the highest level you can go.
24+
```
25+
26+
**Example 3:**
27+
28+
```
29+
Input: "/home//foo/"
30+
Output: "/home/foo"
31+
Explanation: In the canonical path, multiple consecutive slashes are replaced by a single one.
32+
```
33+
34+
**Example 4:**
35+
36+
```
37+
Input: "/a/./b/../../c/"
38+
Output: "/c"
39+
```
40+
41+
**Example 5:**
42+
43+
```
44+
Input: "/a/../../b/../c//.//"
45+
Output: "/c"
46+
```
47+
**Example 5:**
48+
49+
```
50+
Input: "/a//b////c/d//././/.."
51+
Output: "/a/b/c"
2152
```
2253

2354
**Tags:** Math, String
2455

2556
## 题意
26-
>给你两个二进制串,求其和的二进制串。
57+
>大概意思就是讲输入的绝对路径简化
2758
2859
## 题解
2960

3061
### 思路1
31-
> 按照小学算数那么来做,用 `carry` 表示进位,从后往前算,依次往前,每算出一位就插入到最前面即可,直到把两个二进制串都遍历完即可
32-
62+
> 利用golang自带的函数解决
63+
> 注意:此方法会根据平台变换 【/】【\】
3364
```go
34-
65+
func simplifyPath(path string) string {
66+
return filepath.Clean(path)
67+
}
3568
```
3669

3770
### 思路2
38-
> 思路2
71+
>
3972
```go
40-
73+
func simplifyPath(path string) string {
74+
var stack []string
75+
for _, component := range strings.Split(path, "/") {
76+
if component == "" || component == "." {
77+
continue
78+
}
79+
if component == ".." {
80+
if len(stack) > 0 {
81+
stack = stack[:len(stack)-1]
82+
}
83+
} else {
84+
stack = append(stack, component)
85+
}
86+
}
87+
return "/" + strings.Join(stack, "/")
88+
}
4189
```
4290

4391
## 结语
4492

4593
如果你同我一样热爱数据结构、算法、LeetCode,可以关注我 GitHub 上的 LeetCode 题解:[awesome-golang-leetcode][me]
4694

47-
[title]: https://leetcode.com/problems/two-sum/description/
95+
[title]: hhttps://leetcode.com/problems/simplify-path/
4896
[me]: https://github.com/kylesliu/awesome-golang-leetcode

0 commit comments

Comments
 (0)