Skip to content

Commit 853e330

Browse files
author
hero
committed
添加leet_code
1 parent b97ca77 commit 853e330

File tree

5 files changed

+77
-20
lines changed

5 files changed

+77
-20
lines changed

leet_code/add-two-numbers_test.go

+43-19
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,17 @@ import (
55
)
66

77
func TestAddTwoNumbers(t *testing.T) {
8-
t.Log(addTwoNumbers(&ListNode{
9-
Val: 2,
10-
Next: &ListNode{
11-
Val: 4,
12-
Next: &ListNode{
13-
Val: 3,
14-
Next: nil,
15-
},
16-
},
8+
info := addTwoNumbers(&ListNode{
9+
Val: 5,
10+
Next: nil,
1711
}, &ListNode{
18-
Val: 5,
19-
Next: &ListNode{
20-
Val: 6,
21-
Next: &ListNode{
22-
Val: 4,
23-
Next: nil,
24-
},
25-
},
26-
}))
12+
Val: 5,
13+
Next: nil,
14+
})
15+
for info != nil {
16+
t.Log(info.Val)
17+
info = info.Next
18+
}
2719
}
2820

2921
type ListNode struct {
@@ -34,5 +26,37 @@ type ListNode struct {
3426
//两数相加
3527
func addTwoNumbers(l1 *ListNode, l2 *ListNode) *ListNode {
3628
var data = new(ListNode)
37-
return data
29+
var curr = data
30+
var less int
31+
for l1 != nil || l2 != nil {
32+
var (
33+
x int = 0
34+
y int = 0
35+
sum = 0
36+
)
37+
if l1 != nil {
38+
x = l1.Val
39+
}
40+
if l2 != nil {
41+
y = l2.Val
42+
}
43+
sum = less + x + y
44+
less = sum / 10
45+
if l1 != nil {
46+
l1 = l1.Next
47+
}
48+
if l2 != nil {
49+
l2 = l2.Next
50+
}
51+
curr.Next = &ListNode{Val: sum % 10}
52+
curr = curr.Next
53+
54+
}
55+
if less > 0 {
56+
curr.Next = &ListNode{
57+
Val: less,
58+
Next: nil,
59+
}
60+
}
61+
return data.Next
3862
}
+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package leet_code
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func TestLengthOfLongestSubstring(t *testing.T) {
8+
t.Log(lengthOfLongestSubstring("abcabcbb"))
9+
}
10+
11+
type Data struct {
12+
Index int
13+
Val int
14+
have bool
15+
}
16+
17+
func lengthOfLongestSubstring(s string) int {
18+
if len(s) == 0 {
19+
return 0
20+
}
21+
return 0
22+
}

leet_code/two-sum_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import (
55
)
66

77
func TestTwoSum(t *testing.T) {
8-
t.Log(twoSum([]int{2, 7, 11, 15}, 6))
8+
t.Log(twoSum([]int{2, 7, 11, 15}, 9))
99
}
1010

1111
//两数相和

linked_list/double_list.go

+10
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,16 @@ func (l *DoubleList) GetAll() []interface{} {
117117
return data
118118
}
119119

120+
func (l *DoubleList) GetAllV2() []interface{} {
121+
var data []interface{}
122+
node := l.Head
123+
for node != nil {
124+
data = append(data, node.Data)
125+
node = node.Next
126+
}
127+
return data
128+
}
129+
120130
func (l *DoubleList) GetHead() *DoubleNode {
121131
return l.Head
122132
}

linked_list/double_list_test.go

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ func TestNewDoubleList(t *testing.T) {
1414
Data: 10,
1515
})
1616
t.Log(l.GetAll())
17+
t.Log(l.GetAllV2())
1718
t.Log(l.Get(3))
1819
t.Log(l.InsertNext(l.Get(2), &DoubleNode{
1920
Data: 11,

0 commit comments

Comments
 (0)