Skip to content

Commit fae6c70

Browse files
author
baxiang
committed
旋转矩阵
1 parent 2d1a810 commit fae6c70

File tree

6 files changed

+175
-0
lines changed

6 files changed

+175
-0
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package main
2+
3+
func main() {
4+
$END$
5+
}

257binary-tree-paths/main.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package main
2+
3+
func main() {
4+
$END$
5+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package main
2+
3+
func main() {
4+
$END$
5+
}

54spiral-matrix/main.go

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
package main
2+
3+
import "fmt"
4+
5+
func spiralOrder1(matrix [][]int) []int {
6+
var res []int
7+
if len(matrix)==0{
8+
return res
9+
}
10+
row :=len(matrix)
11+
col := len(matrix[0])
12+
up :=0//上界
13+
down :=row-1//下界
14+
left :=0//左界
15+
right :=col-1// 右界
16+
total := row*col//总数
17+
for {
18+
// 从左到右
19+
for i:=left;i<=right;i++{
20+
res = append(res,matrix[up][i])
21+
}
22+
if len(res)==total{
23+
break
24+
}
25+
up++// 上边界+1 向下移动一位
26+
for i:=up;i<=down;i++{
27+
res = append(res,matrix[i][right])
28+
}
29+
if len(res)==total{
30+
break
31+
}
32+
right--
33+
for i:=right;i>=left;i--{
34+
res = append(res,matrix[down][i])
35+
}
36+
if len(res)==total{
37+
break
38+
}
39+
down--
40+
for i:=down;i>=up;i--{
41+
res = append(res,matrix[i][left])
42+
}
43+
if len(res)==total{
44+
break
45+
}
46+
left++
47+
}
48+
return res
49+
}
50+
51+
52+
func spiralOrder(matrix [][]int) []int {
53+
var res []int
54+
if len(matrix)==0{
55+
return res
56+
}
57+
row :=len(matrix)
58+
col := len(matrix[0])
59+
up :=0//上界
60+
down :=row-1//下界
61+
left :=0//左界
62+
right :=col-1// 右界
63+
for {
64+
// 从左到右
65+
for i:=left;i<=right;i++{
66+
res = append(res,matrix[up][i])
67+
}
68+
up++// 上边界+1 向下移动一位
69+
if up>down{
70+
break
71+
}
72+
// 从上到下
73+
for i:=up;i<=down;i++{
74+
res = append(res,matrix[i][right])
75+
}
76+
// 右边界移动
77+
right--
78+
if right<left{
79+
break
80+
}
81+
//从右到左
82+
for i:=right;i>=left;i--{
83+
res = append(res,matrix[down][i])
84+
}
85+
//下边界向上移动一位
86+
down--
87+
if down<up{
88+
break
89+
}
90+
// 从下到上
91+
for i:=down;i>=up;i--{
92+
res = append(res,matrix[i][left])
93+
}
94+
// 左边界向右移动一位
95+
left++
96+
if left>right{
97+
break
98+
}
99+
}
100+
return res
101+
}
102+
103+
104+
func main() {
105+
fmt.Println(spiralOrder1([][]int{
106+
{1, 2, 3 },
107+
{ 4, 5, 6 },
108+
{ 7, 8, 9 },
109+
}))
110+
111+
fmt.Println(spiralOrder1([][]int{
112+
{1, 2, 3, 4},
113+
{5, 6, 7, 8},
114+
{9, 10, 11, 12},
115+
}))
116+
}

5ti-huan-kong-ge-lcof/main.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package main
2+
3+
func main() {
4+
$END$
5+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package main
2+
3+
import "fmt"
4+
5+
type TreeNode struct {
6+
Val int
7+
Left *TreeNode
8+
Right *TreeNode
9+
}
10+
11+
func buildTree(preorder []int, inorder []int) *TreeNode {
12+
if len(preorder) == 0 {
13+
return nil
14+
}
15+
rootIdx := 0
16+
for i := range inorder {
17+
if inorder[i] == preorder[0] {
18+
rootIdx = i
19+
break
20+
}
21+
}
22+
root := &TreeNode{Val: preorder[0]}
23+
root.Left = buildTree(preorder[1:rootIdx+1], inorder[:rootIdx])
24+
root.Right = buildTree(preorder[rootIdx+1:], inorder[rootIdx+1:])
25+
return root
26+
}
27+
func printTree(root *TreeNode) {
28+
if root == nil {
29+
return
30+
}
31+
fmt.Println(root.Val)
32+
printTree(root.Left)
33+
printTree(root.Right)
34+
}
35+
36+
func main() {
37+
r := buildTree([]int{3, 9, 20, 15, 7}, []int{9, 3, 15, 20, 7})
38+
printTree(r)
39+
}

0 commit comments

Comments
 (0)