Skip to content

Commit 90736be

Browse files
author
Kyle Liu
committed
update for docs
1 parent 953dfef commit 90736be

File tree

3,233 files changed

+102850
-104
lines changed

Some content is hidden

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

3,233 files changed

+102850
-104
lines changed

SUMMARY.md

Lines changed: 124 additions & 100 deletions
Large diffs are not rendered by default.

cmd/template/gitbook/SUMMARY.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@
33
* [Summary](SUMMARY-LIST.md)
44
* [Contributor](CONTRIBUTOR.md)
55
* Soutions(1-976)
6-
{{ range . }}* [{{.PathName}}]({{.PathName}}/README.md)
6+
{{ range . }}* [{{.PathName}}](leetcode/{{.PathName}}/README.md)
77
{{ end }}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# [1. Add Sum][title]
2+
3+
## Description
4+
5+
Given two binary strings, return their sum (also a binary string).
6+
7+
The input strings are both **non-empty** and contains only characters `1` or `0`.
8+
9+
**Example 1:**
10+
11+
```
12+
Input: a = "11", b = "1"
13+
Output: "100"
14+
```
15+
16+
**Example 2:**
17+
18+
```
19+
Input: a = "1010", b = "1011"
20+
Output: "10101"
21+
```
22+
23+
**Tags:** Math, String
24+
> [!NOTE|style:flat]
25+
> An alert of type 'note' using global style 'callout'.
26+
27+
* 1
28+
> [!TIP|style:flat]
29+
> An alert of type 'note' using global style 'callout'.
30+
31+
* 2
32+
33+
> [!WARNING|style:flat]
34+
> An alert of type 'note' using global style 'callout'.
35+
36+
37+
38+
39+
## 题意
40+
>给你两个二进制串,求其和的二进制串。
41+
42+
## 题解
43+
44+
### 思路1
45+
> 按照小学算数那么来做,用 `carry` 表示进位,从后往前算,依次往前,每算出一位就插入到最前面即可,直到把两个二进制串都遍历完即可。
46+
47+
```go
48+
49+
```
50+
51+
### 思路2
52+
> 思路2
53+
```go
54+
55+
```
56+
57+
## 结语
58+
59+
如果你同我一样热爱数据结构、算法、LeetCode,可以关注我 GitHub 上的 LeetCode 题解:[awesome-golang-leetcode][me]
60+
61+
[title]: https://leetcode.com/problems/two-sum/description/
62+
[me]: https://github.com/kylesliu/awesome-golang-algorithm
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package Solution
2+
3+
func TwoSum1(nums []int, target int) []int {
4+
5+
n := len(nums)
6+
7+
for i, v := range nums {
8+
for j := i + 1; j < n; j++ {
9+
if v+nums[j] == target {
10+
return []int{i, j}
11+
}
12+
}
13+
}
14+
15+
return nil
16+
}
17+
18+
func TwoSum2(nums []int, target int) []int {
19+
20+
m := make(map[int]int, len(nums))
21+
22+
for i, v := range nums {
23+
sub := target - v
24+
if j, ok := m[sub]; ok {
25+
return []int{j, i}
26+
} else {
27+
m[v] = i
28+
}
29+
}
30+
31+
return nil
32+
}
33+
34+
func TwoSum3(nums []int, target int) []int {
35+
if nums == nil || len(nums) < 2 {
36+
return []int{-1, -1}
37+
}
38+
39+
res := []int{-1, -1}
40+
41+
// MAP的KEY表示值,MAP的VAL表示nums的下标
42+
intMap := map[int]int{}
43+
for i := 0; i < len(nums); i++ {
44+
// 判断MAP中是否纯在一个Key满足 KEY + nums[i] = target
45+
// 如果满足则返回相关地址,不满足则将数组中的值PUSH到MAP
46+
if _, ok := intMap[target-nums[i]]; ok {
47+
res[0] = intMap[target-nums[i]]
48+
res[1] = i
49+
break
50+
}
51+
intMap[nums[i]] = i
52+
}
53+
return res
54+
}
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
package Solution
2+
3+
import (
4+
"math/rand"
5+
"reflect"
6+
"testing"
7+
)
8+
9+
func TestTwoSum1(t *testing.T) {
10+
// 测试用例
11+
cases := []struct {
12+
name string
13+
inputs [][]int
14+
expect []int
15+
}{
16+
{"1 test 1", [][]int{{2, 7, 11, 15}, {9}}, []int{0, 1}},
17+
{"2 test 2", [][]int{{3, 2, 4}, {6}}, []int{1, 2}},
18+
{"3 test 3", [][]int{{2, 7, 11, 15}, {9}}, []int{0, 1}},
19+
{"4 test 4", [][]int{{7, 6, 5, 3, 2, 1, 4, 9, 10}, {17}}, []int{0, 8}},
20+
}
21+
22+
// 开始测试
23+
for _, c := range cases {
24+
t.Run(c.name, func(t *testing.T) {
25+
ret := TwoSum1(c.inputs[0], c.inputs[1][0])
26+
if !reflect.DeepEqual(ret, c.expect) {
27+
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
28+
c.expect, ret, c.inputs)
29+
}
30+
})
31+
}
32+
}
33+
34+
func TestTwoSum2(t *testing.T) {
35+
// 测试用例
36+
cases := []struct {
37+
name string
38+
inputs [][]int
39+
expect []int
40+
}{
41+
{"1 test 1", [][]int{{2, 7, 11, 15}, {9}}, []int{0, 1}},
42+
{"2 test 2", [][]int{{3, 2, 4}, {6}}, []int{1, 2}},
43+
{"3 test 3", [][]int{{2, 7, 11, 15}, {9}}, []int{0, 1}},
44+
{"4 test 4", [][]int{{7, 6, 5, 3, 2, 1, 4, 9, 10}, {17}}, []int{0, 8}},
45+
}
46+
47+
// 开始测试
48+
for _, c := range cases {
49+
t.Run(c.name, func(t *testing.T) {
50+
ret := TwoSum2(c.inputs[0], c.inputs[1][0])
51+
if !reflect.DeepEqual(ret, c.expect) {
52+
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
53+
c.expect, ret, c.inputs)
54+
}
55+
})
56+
}
57+
}
58+
59+
func TestTwoSum3(t *testing.T) {
60+
// 测试用例
61+
cases := []struct {
62+
name string
63+
inputs [][]int
64+
expect []int
65+
}{
66+
{"1 test 1", [][]int{{2, 7, 11, 15}, {9}}, []int{0, 1}},
67+
{"2 test 2", [][]int{{3, 2, 4}, {6}}, []int{1, 2}},
68+
{"3 test 3", [][]int{{2, 7, 11, 15}, {9}}, []int{0, 1}},
69+
{"4 test 4", [][]int{{7, 6, 5, 3, 2, 1, 4, 9, 10}, {17}}, []int{0, 8}},
70+
}
71+
72+
// 开始测试
73+
for _, c := range cases {
74+
t.Run(c.name, func(t *testing.T) {
75+
ret := TwoSum3(c.inputs[0], c.inputs[1][0])
76+
if !reflect.DeepEqual(ret, c.expect) {
77+
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
78+
c.expect, ret, c.inputs)
79+
}
80+
})
81+
}
82+
}
83+
84+
const N = 100
85+
86+
func BenchmarkTwoSum1(b *testing.B) {
87+
nums := []int{}
88+
for i := 0; i < N; i++ {
89+
nums = append(nums, rand.Int())
90+
}
91+
nums = append(nums, 7, 2)
92+
93+
b.ResetTimer()
94+
for i := 0; i < b.N; i++ {
95+
TwoSum1(nums, 9)
96+
}
97+
}
98+
99+
func BenchmarkTwoSum2(b *testing.B) {
100+
nums := []int{}
101+
for i := 0; i < N; i++ {
102+
nums = append(nums, rand.Int())
103+
}
104+
nums = append(nums, 7, 2)
105+
106+
b.ResetTimer()
107+
for i := 0; i < b.N; i++ {
108+
TwoSum2(nums, 9)
109+
}
110+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package Solution
2+
3+
type ListNode struct {
4+
Val int
5+
Next *ListNode
6+
}
7+
8+
// 比较结果
9+
func isEqual(l1 *ListNode, l2 *ListNode) bool {
10+
11+
for l1 != nil && l2 != nil {
12+
if l1.Val != l2.Val {
13+
return false
14+
}
15+
l1 = l1.Next
16+
l2 = l2.Next
17+
}
18+
if l1 == nil && l2 != nil {
19+
return false
20+
}
21+
if l1 != nil && l2 == nil {
22+
return false
23+
}
24+
return true
25+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
# [2. Add Two Numbers][title]
2+
3+
## Description
4+
5+
You are given two **non-empty** linked lists representing two non-negative integers. The digits are stored in **reverse order** and each of their nodes contain a single digit. Add the two numbers and return it as a linked list.
6+
7+
You may assume the two numbers do not contain any leading zero, except the number 0 itself.
8+
9+
**Example**
10+
11+
```
12+
Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
13+
Output: 7 -> 0 -> 8
14+
Explanation: 342 + 465 = 807.
15+
```
16+
17+
**Tags:** Linked List, Math
18+
19+
## 题意
20+
>以链表表示一个数,低位在前,高位在后,所以题中的例子就是 342 + 465 = 807.
21+
22+
## 题解
23+
24+
### 思路1
25+
> 直接模拟计算就好
26+
27+
```golang
28+
func addTwoNumbers(l1 *ListNode, l2 *ListNode) *ListNode {
29+
node := &ListNode{Val: 0, Next: nil}
30+
n1, n2, tmp := l1, l2, node
31+
sum := 0
32+
33+
for n1 != nil || n2 != nil {
34+
sum /= 10
35+
if n1 != nil {
36+
sum += n1.Val
37+
n1 = n1.Next
38+
39+
}
40+
if n2 != nil {
41+
sum += n2.Val
42+
n2 = n2.Next
43+
}
44+
tmp.Next = &ListNode{Val: sum % 10}
45+
tmp = tmp.Next
46+
}
47+
if sum/10 != 0 {
48+
tmp.Next = &ListNode{Val: 1}
49+
}
50+
return node.Next
51+
}
52+
53+
54+
```
55+
56+
### 思路2
57+
> 思路2
58+
```go
59+
60+
```
61+
62+
## 结语
63+
64+
如果你同我一样热爱数据结构、算法、LeetCode,可以关注我 GitHub 上的 LeetCode 题解:[awesome-golang-leetcode][me]
65+
66+
[title]: https://leetcode.com/problems/add-two-numbers/description/
67+
[me]: https://github.com/kylesliu/awesome-golang-algorithm
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package Solution
2+
3+
func addTwoNumbers(l1 *ListNode, l2 *ListNode) *ListNode {
4+
node := &ListNode{Val: 0, Next: nil}
5+
n1, n2, tmp := l1, l2, node
6+
sum := 0
7+
8+
for n1 != nil || n2 != nil {
9+
sum /= 10
10+
if n1 != nil {
11+
sum += n1.Val
12+
n1 = n1.Next
13+
14+
}
15+
if n2 != nil {
16+
sum += n2.Val
17+
n2 = n2.Next
18+
}
19+
tmp.Next = &ListNode{Val: sum % 10}
20+
tmp = tmp.Next
21+
}
22+
if sum/10 != 0 {
23+
tmp.Next = &ListNode{Val: 1}
24+
}
25+
return node.Next
26+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package Solution
2+
3+
import (
4+
"strconv"
5+
"testing"
6+
)
7+
8+
func TestSolution(t *testing.T) {
9+
// 测试用例
10+
cases := []struct {
11+
name string
12+
input1 *ListNode
13+
input2 *ListNode
14+
expect *ListNode
15+
}{
16+
{"Test Case ",
17+
&ListNode{Val: 2, Next: &ListNode{Val: 4, Next: &ListNode{Val: 3, Next: nil}}},
18+
&ListNode{Val: 5, Next: &ListNode{Val: 6, Next: &ListNode{Val: 4, Next: nil}}},
19+
&ListNode{Val: 7, Next: &ListNode{Val: 0, Next: &ListNode{Val: 8, Next: nil}}}},
20+
}
21+
22+
// 开始测试
23+
for i, c := range cases {
24+
t.Run(c.name+strconv.Itoa(i), func(t *testing.T) {
25+
got := addTwoNumbers(c.input1, c.input2)
26+
if !isEqual(got, c.expect) {
27+
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
28+
c.expect, got, c.input1)
29+
}
30+
})
31+
}
32+
}

0 commit comments

Comments
 (0)