Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit049670d

Browse files
committed
update 101 problem solution
1 parente997024 commit049670d

File tree

4 files changed

+351
-11
lines changed

4 files changed

+351
-11
lines changed

‎src/0101.Symmetric-Tree/List.go

Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
package Solution
2+
3+
import (
4+
"fmt"
5+
"sync"
6+
)
7+
8+
// 节点数据
9+
typeSingleObjectinterface{}
10+
11+
// 单链表节点
12+
typeSingleNodestruct {
13+
DataSingleObject
14+
Next*SingleNode
15+
}
16+
17+
// 单链表
18+
typeSingleListstruct {
19+
mutex*sync.RWMutex
20+
Head*SingleNode
21+
Tail*SingleNode
22+
Sizeuint
23+
}
24+
25+
// 初始化
26+
func (list*SingleList)Init() {
27+
list.Size=0
28+
list.Head=nil
29+
list.Tail=nil
30+
list.mutex=new(sync.RWMutex)
31+
}
32+
33+
// 添加节点到链表尾部
34+
func (list*SingleList)Append(node*SingleNode)bool {
35+
ifnode==nil {
36+
returnfalse
37+
}
38+
list.mutex.Lock()
39+
deferlist.mutex.Unlock()
40+
iflist.Size==0 {
41+
list.Head=node
42+
list.Tail=node
43+
list.Size=1
44+
returntrue
45+
}
46+
47+
tail:=list.Tail
48+
tail.Next=node
49+
list.Tail=node
50+
list.Size+=1
51+
returntrue
52+
}
53+
54+
// 插入节点到指定位置
55+
func (list*SingleList)Insert(indexuint,node*SingleNode)bool {
56+
ifnode==nil {
57+
returnfalse
58+
}
59+
60+
ifindex>list.Size {
61+
returnfalse
62+
}
63+
64+
list.mutex.Lock()
65+
deferlist.mutex.Unlock()
66+
67+
ifindex==0 {
68+
node.Next=list.Head
69+
list.Head=node
70+
list.Size+=1
71+
returntrue
72+
}
73+
variuint
74+
ptr:=list.Head
75+
fori=1;i<index;i++ {
76+
ptr=ptr.Next
77+
}
78+
next:=ptr.Next
79+
ptr.Next=node
80+
node.Next=next
81+
list.Size+=1
82+
returntrue
83+
}
84+
85+
// 删除指定位置的节点
86+
func (list*SingleList)Delete(indexuint)bool {
87+
iflist==nil||list.Size==0||index>list.Size-1 {
88+
returnfalse
89+
}
90+
91+
list.mutex.Lock()
92+
deferlist.mutex.Unlock()
93+
94+
ifindex==0 {
95+
head:=list.Head.Next
96+
list.Head=head
97+
iflist.Size==1 {
98+
list.Tail=nil
99+
}
100+
list.Size-=1
101+
returntrue
102+
}
103+
104+
ptr:=list.Head
105+
variuint
106+
fori=1;i<index;i++ {
107+
ptr=ptr.Next
108+
}
109+
next:=ptr.Next
110+
111+
ptr.Next=next.Next
112+
ifindex==list.Size-1 {
113+
list.Tail=ptr
114+
}
115+
list.Size-=1
116+
returntrue
117+
}
118+
119+
// 获取指定位置的节点,不存在则返回nil
120+
func (list*SingleList)Get(indexuint)*SingleNode {
121+
iflist==nil||list.Size==0||index>list.Size-1 {
122+
returnnil
123+
}
124+
125+
list.mutex.RLock()
126+
deferlist.mutex.RUnlock()
127+
128+
ifindex==0 {
129+
returnlist.Head
130+
}
131+
node:=list.Head
132+
variuint
133+
fori=0;i<index;i++ {
134+
node=node.Next
135+
}
136+
returnnode
137+
}
138+
139+
// 输出链表
140+
func (list*SingleList)Display() {
141+
iflist==nil||list.Size==0 {
142+
fmt.Println("this single list is nil")
143+
return
144+
}
145+
list.mutex.RLock()
146+
deferlist.mutex.RUnlock()
147+
fmt.Printf("this single list size is %d\n",list.Size)
148+
ptr:=list.Head
149+
variuint
150+
fori=0;i<list.Size;i++ {
151+
fmt.Printf("No%3d data is %v\n",i+1,ptr.Data)
152+
ptr=ptr.Next
153+
}
154+
}

‎src/0101.Symmetric-Tree/Queue.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package Solution
2+
3+
// Queue 队列信息
4+
typeQueuestruct {
5+
list*SingleList
6+
}
7+
8+
// Init 队列初始化
9+
func (q*Queue)Init() {
10+
q.list=new(SingleList)
11+
q.list.Init()
12+
}
13+
14+
// Size 获取队列长度
15+
func (q*Queue)Size()uint {
16+
returnq.list.Size
17+
}
18+
19+
// Enqueue 进入队列
20+
func (q*Queue)Enqueue(datainterface{})bool {
21+
returnq.list.Append(&SingleNode{Data:data})
22+
}
23+
24+
// Dequeue 出列
25+
func (q*Queue)Dequeue()interface{} {
26+
node:=q.list.Get(0)
27+
ifnode==nil {
28+
returnnil
29+
}
30+
q.list.Delete(0)
31+
returnnode.Data
32+
}
33+
34+
// Peek 查看队头信息
35+
func (q*Queue)Peek()interface{} {
36+
node:=q.list.Get(0)
37+
ifnode==nil {
38+
returnnil
39+
}
40+
returnnode.Data
41+
}

‎src/0101.Symmetric-Tree/Solution.go

Lines changed: 49 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,16 @@ type TreeNode struct {
66
Right*TreeNode
77
}
88

9-
funcSolution(xbool)bool {
10-
returnx
11-
}
12-
9+
//递归
1310
funcisSymmetric(root*TreeNode)bool {
1411
returnroot==nil||isSymmetricHelper(root.Left,root.Right)
1512
}
1613

1714
funcisSymmetricHelper(left*TreeNode,right*TreeNode)bool {
18-
15+
ifleft==nil&&right==nil {
16+
returntrue
17+
}
18+
//左右节点一个不存在
1919
ifleft==nil||right==nil {
2020
returnleft==right
2121
}
@@ -27,3 +27,47 @@ func isSymmetricHelper(left *TreeNode, right *TreeNode) bool {
2727
returnisSymmetricHelper(left.Left,right.Right)&&
2828
isSymmetricHelper(left.Right,right.Left)
2929
}
30+
31+
//循环
32+
funcisSymmetric_BFS(root*TreeNode)bool {
33+
queue,left,right:= []*TreeNode{root.Left,root.Right},&TreeNode{},&TreeNode{}
34+
35+
forlen(queue)>0 {
36+
left,right,queue=queue[0],queue[1], []*TreeNode{}
37+
38+
ifleft==nil&&right==nil {
39+
continue
40+
}
41+
ifleft==nil||right==nil {
42+
returnfalse
43+
}
44+
ifleft.Val!=right.Val {
45+
returnfalse
46+
}
47+
48+
queue=append(queue,left.Left,right.Right,left.Right,right.Left)
49+
}
50+
returntrue
51+
}
52+
funcisSymmetric_DFS(root*TreeNode)bool {
53+
54+
stack,left,right:= []*TreeNode{root.Left,root.Right},&TreeNode{},&TreeNode{}
55+
forlen(stack)>0 {
56+
left,right=stack[len(stack)-1],stack[len(stack)-2]
57+
stack=stack[:len(stack)-2]
58+
ifleft==nil&&right==nil {
59+
continue
60+
}
61+
62+
ifleft==nil||right==nil {
63+
returnfalse
64+
}
65+
66+
ifleft.Val!=right.Val {
67+
returnfalse
68+
}
69+
70+
stack=append(stack,left.Left,right.Right,left.Right,right.Left)
71+
}
72+
returntrue
73+
}

‎src/0101.Symmetric-Tree/Solution_test.go

Lines changed: 107 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,123 @@ import (
55
"testing"
66
)
77

8-
funcTestSolution(t*testing.T) {
8+
funcTestSolution_Recursive(t*testing.T) {
99
//测试用例
1010
cases:= []struct {
1111
namestring
12-
inputsbool
12+
inputs*TreeNode
1313
expectbool
1414
}{
15-
{"1 test 1",true,true},
16-
{"2 test 2",true,true},
17-
{"3 test 3",false,false},
15+
{
16+
"TestCase 1",
17+
&TreeNode{Val:1,
18+
Left:&TreeNode{Val:2,Right:&TreeNode{Val:3}},
19+
Right:&TreeNode{Val:2,Right:&TreeNode{Val:3}},
20+
},
21+
false,
22+
},
23+
{
24+
"TestCase 2",
25+
&TreeNode{Val:1,
26+
Left:&TreeNode{Val:2,
27+
Left:&TreeNode{Val:3},
28+
Right:&TreeNode{Val:4}},
29+
Right:&TreeNode{Val:2,
30+
Left:&TreeNode{Val:4},
31+
Right:&TreeNode{Val:3},
32+
},
33+
},
34+
true,
35+
},
1836
}
1937

2038
//开始测试
2139
for_,c:=rangecases {
2240
t.Run(c.name,func(t*testing.T) {
23-
ret:=Solution(c.inputs)
41+
ret:=isSymmetric(c.inputs)
42+
if!reflect.DeepEqual(ret,c.expect) {
43+
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
44+
c.expect,ret,c.inputs)
45+
}
46+
})
47+
}
48+
}
49+
50+
funcTestSolution_BFS(t*testing.T) {
51+
//测试用例
52+
cases:= []struct {
53+
namestring
54+
inputs*TreeNode
55+
expectbool
56+
}{
57+
{
58+
"TestCase 1",
59+
&TreeNode{Val:1,
60+
Left:&TreeNode{Val:2,Right:&TreeNode{Val:3}},
61+
Right:&TreeNode{Val:2,Right:&TreeNode{Val:3}},
62+
},
63+
false,
64+
},
65+
{
66+
"TestCase 2",
67+
&TreeNode{Val:1,
68+
Left:&TreeNode{Val:2,
69+
Left:&TreeNode{Val:3},
70+
Right:&TreeNode{Val:4}},
71+
Right:&TreeNode{Val:2,
72+
Left:&TreeNode{Val:4},
73+
Right:&TreeNode{Val:3},
74+
},
75+
},
76+
true,
77+
},
78+
}
79+
80+
//开始测试
81+
for_,c:=rangecases {
82+
t.Run(c.name,func(t*testing.T) {
83+
ret:=isSymmetric_BFS(c.inputs)
84+
if!reflect.DeepEqual(ret,c.expect) {
85+
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
86+
c.expect,ret,c.inputs)
87+
}
88+
})
89+
}
90+
}
91+
funcTestSolution_DFS(t*testing.T) {
92+
//测试用例
93+
cases:= []struct {
94+
namestring
95+
inputs*TreeNode
96+
expectbool
97+
}{
98+
{
99+
"TestCase 1",
100+
&TreeNode{Val:1,
101+
Left:&TreeNode{Val:2,Right:&TreeNode{Val:3}},
102+
Right:&TreeNode{Val:2,Right:&TreeNode{Val:3}},
103+
},
104+
false,
105+
},
106+
{
107+
"TestCase 2",
108+
&TreeNode{Val:1,
109+
Left:&TreeNode{Val:2,
110+
Left:&TreeNode{Val:3},
111+
Right:&TreeNode{Val:4}},
112+
Right:&TreeNode{Val:2,
113+
Left:&TreeNode{Val:4},
114+
Right:&TreeNode{Val:3},
115+
},
116+
},
117+
true,
118+
},
119+
}
120+
121+
//开始测试
122+
for_,c:=rangecases {
123+
t.Run(c.name,func(t*testing.T) {
124+
ret:=isSymmetric_DFS(c.inputs)
24125
if!reflect.DeepEqual(ret,c.expect) {
25126
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
26127
c.expect,ret,c.inputs)

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp