Skip to content

Commit 06a9140

Browse files
committed
Problem 006
1 parent 4d7e0a4 commit 06a9140

File tree

8 files changed

+166
-75
lines changed

8 files changed

+166
-75
lines changed

003-二维数组中的查找/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#题意
1+
# 题意
22

33
## 题目描述
44

004-替换空格/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#题意
1+
# 题意
22

33
请实现一个函数,将一个字符串中的空格替换成“%20”。 例如,当字符串为We Are Happy.则经过替换之后的字符串为We%20Are%20Happy。
44

005-从尾到头打印链表(ing)/README.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#题意
1+
# 题意
22

33
输入一个链表,从尾到头打印链表每个节点的值。
44

@@ -10,18 +10,20 @@
1010

1111
输出为需要打印的“新链表”的表头
1212

13-
##反转链表
13+
## 反转链表
1414

1515
首先我们想到的就是反转链表了,如果把链表反转了,然后再返回头,这样再次遍历的时候就相当于从尾到头打印了。
1616

1717
但是修改输入数据真的可行么?
1818

1919
剑指Offer中为我们在面试中提出了如下小提示
2020

21-
在面试时候,如果我们打算修改输入的数据,最好先问问面试官是不是允许修改
21+
> 在面试时候,如果我们打算修改输入的数据,最好先问问面试官是不是允许修改
2222
2323
通常打印只是一个只读操作,我们肯定不希望输入时候修改链表的内容
2424

25+
**如果要求反转的话,直接头插法就可以了**
26+
2527
## 解题思路
2628

2729
见程序注释
Lines changed: 30 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,35 @@
1-
package problem005
1+
package main
22

3-
type struct NodeList{
4-
int val
3+
import (
4+
"fmt"
5+
)
6+
7+
type NodeList struct{
8+
Val int
9+
Next *NodeList
510
}
611

7-
func replaceSpace(str []byte, length int) {
8-
count := 0
9-
// 遍历一遍字符串, 统计字符出现的数目, 计算替换后的字符串长度
10-
for i:=0; i<length; i++ {
11-
if str[i] == ' '{
12-
count++
13-
}
14-
}
15-
newlength := length + count*2
16-
17-
// 两个index,一个指向length-1, 另一个指向newlength-1,遍历一遍字符串,完成替换
18-
for l,nl := length-1,newlength-1; l>=0 && nl>=0; {
19-
if str[l]==' '{
20-
str[nl] = '0'
21-
nl--
22-
str[nl] = '2'
23-
nl--
24-
str[nl] = '%'
25-
nl--
26-
l--
27-
} else {
28-
str[nl] = str[l]
29-
nl--
30-
l--
31-
}
12+
// just print it form tail to head and do not modify the original NodeList
13+
14+
func printListFromTailToHead(head *NodeList) {
15+
if head != nil {
16+
printListFromTailToHead(head.Next)
17+
fmt.Printf("%d -> ", head.Val)
3218
}
3319
}
20+
21+
func main() {
22+
// example
23+
n3 := &NodeList{3, nil}
24+
n2 := &NodeList{2, n3}
25+
n1 := &NodeList{1, n2}
26+
27+
fmt.Printf("\n NodeList 1 -> 2 -> 3 \n")
28+
29+
// test
30+
fmt.Printf("\n Output: ")
31+
printListFromTailToHead(n1)
32+
fmt.Printf("\n \n")
33+
34+
35+
}

005-从尾到头打印链表(ing)/problem005_test.go

Lines changed: 0 additions & 42 deletions
This file was deleted.

006-重建二叉树/README.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# 题意
2+
题目描述
3+
4+
输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树。
5+
6+
假设输入的前序遍历和中序遍历的结果中都不含重复的数字。
7+
8+
输入
9+
10+
> 前序遍历序列{1,2,4,7,3,5,6,8}
11+
> 中序遍历序列{4,7,2,1,5,3,8,6}
12+
13+
则重建二叉树并返回。
14+
15+
# 分析
16+
17+
这道题还是比较简单的,我们知道
18+
19+
前序遍历的顺序为:根左右
20+
中序遍历的顺序为:左根右
21+
22+
递归思想:
23+
24+
1. 我们先根据前序遍历序列的第一个确定根,然后在中序遍历的序列中找到根的位置,根左边的就是其左子树,右边就是其右子树
25+
2. 构建根和左右子树
26+
3. 递归的进行1和2
27+
28+
29+
## 解题思路
30+
31+
见程序注释
32+

006-重建二叉树/problem006.go

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
)
6+
7+
type TreeNode struct{
8+
Val int
9+
Left *TreeNode
10+
Right *TreeNode
11+
}
12+
13+
func printPreOrder(root *TreeNode){
14+
if root != nil {
15+
fmt.Printf("%d ", root.Val)
16+
printPreOrder(root.Left)
17+
printPreOrder(root.Right)
18+
}
19+
}
20+
21+
func printInOrder(root *TreeNode){
22+
if root != nil {
23+
printInOrder(root.Left)
24+
fmt.Printf("%d ", root.Val)
25+
printInOrder(root.Right)
26+
}
27+
}
28+
29+
func reConstructBinaryTree(pre []int, in []int) *TreeNode {
30+
if len(pre) != len(in) || len(pre) == 0 {
31+
return nil
32+
}
33+
// find root and root Index in inOrder
34+
rootVal := pre[0]
35+
rootIndex := 0
36+
for i := 0; i < len(in); i++ {
37+
if in[i] == rootVal {
38+
rootIndex = i
39+
}
40+
}
41+
inL, inR := in[:rootIndex], in[rootIndex+1:]
42+
preL, preR := pre[1:rootIndex+1], pre[rootIndex+1:]
43+
left := reConstructBinaryTree(preL, inL)
44+
right := reConstructBinaryTree(preR, inR)
45+
return &TreeNode{Val: rootVal, Left: left, Right: right}
46+
}
47+
48+
49+
50+
func main() {
51+
// example
52+
pre := []int{1,2,4,7,3,5,6,8}
53+
in := []int{4,7,2,1,5,3,6,8}
54+
55+
fmt.Println("preOder: ", pre)
56+
fmt.Println("inOrder: ", in)
57+
58+
// Reconstruct
59+
fmt.Println("\nReconstruct Binary Tree... \n ",)
60+
root := reConstructBinaryTree(pre, in)
61+
62+
// test
63+
fmt.Printf("preOder from Tree reconstructed: ")
64+
printPreOrder(root)
65+
fmt.Printf("\n")
66+
67+
fmt.Printf("inOder from Tree reconstructed: ")
68+
printInOrder(root)
69+
fmt.Printf("\n")
70+
71+
72+
}
73+
74+
75+
76+
77+
78+
79+
80+
81+
82+
83+
84+
85+
86+
87+
88+
89+
90+
91+
92+
93+
94+
95+
96+

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
# 剑指Offer - Golang实现
22

3+
如有错误或者更好的版本,欢迎提交意见

0 commit comments

Comments
 (0)