Skip to content

Commit 33ef6f2

Browse files
committed
feat: update golang solution to lcof problems: No.67,68-II
1 parent fc58757 commit 33ef6f2

File tree

4 files changed

+126
-0
lines changed

4 files changed

+126
-0
lines changed

lcof/面试题67. 把字符串转换成整数/README.md

+49
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,55 @@ var strToInt = function (str) {
167167
};
168168
```
169169

170+
### **Go**
171+
172+
```go
173+
func strToInt(str string) int {
174+
n, sign, i, x := len(str), 1, 0, 0
175+
176+
// 去除开头的空格
177+
for i < n && str[i] == ' ' {
178+
i++
179+
}
180+
181+
// 如果只有空格
182+
if i == n {
183+
return 0
184+
}
185+
186+
// 负数
187+
if str[i] == '-' {
188+
sign = -1
189+
}
190+
191+
// 跳过符号
192+
if str[i] == '-' || str[i] == '+' {
193+
i++
194+
}
195+
196+
// 如果符号后紧跟的不是数字
197+
if i == n || str[i] < '0' || str[i] > '9' {
198+
return 0
199+
}
200+
201+
// golang 在 64 位平台下 int 就是 int64,所以不用对 x 进行特殊处理
202+
for ; i < n && str[i] >= '0' && str[i] <= '9'; i++ {
203+
x = x*10 + int(str[i]) - '0'
204+
if x > math.MaxInt32 {
205+
break
206+
}
207+
}
208+
209+
if x > math.MaxInt32 {
210+
if sign == 1 {
211+
return math.MaxInt32
212+
}
213+
return math.MinInt32
214+
}
215+
return sign * x
216+
}
217+
```
218+
170219
### **...**
171220

172221
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
func strToInt(str string) int {
2+
n, sign, i, x := len(str), 1, 0, 0
3+
4+
// 去除开头的空格
5+
for i < n && str[i] == ' ' {
6+
i++
7+
}
8+
9+
// 如果只有空格
10+
if i == n {
11+
return 0
12+
}
13+
14+
// 负数
15+
if str[i] == '-' {
16+
sign = -1
17+
}
18+
19+
// 跳过符号
20+
if str[i] == '-' || str[i] == '+' {
21+
i++
22+
}
23+
24+
// 如果符号后紧跟的不是数字
25+
if i == n || str[i] < '0' || str[i] > '9' {
26+
return 0
27+
}
28+
29+
// golang 在 64 位平台下 int 就是 int64,所以不用对 x 进行特殊处理
30+
for ; i < n && str[i] >= '0' && str[i] <= '9'; i++ {
31+
x = x*10 + int(str[i]) - '0'
32+
if x > math.MaxInt32 {
33+
break
34+
}
35+
}
36+
37+
if x > math.MaxInt32 {
38+
if sign == 1 {
39+
return math.MaxInt32
40+
}
41+
return math.MinInt32
42+
}
43+
return sign * x
44+
}

lcof/面试题68 - II. 二叉树的最近公共祖先/README.md

+19
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,25 @@ public:
167167

168168
```
169169

170+
### **Go**
171+
172+
```go
173+
func lowestCommonAncestor(root, p, q *TreeNode) *TreeNode {
174+
if root == nil || root == p || root == q {
175+
return root
176+
}
177+
left := lowestCommonAncestor(root.Left, p, q)
178+
right := lowestCommonAncestor(root.Right, p, q)
179+
if left == nil {
180+
return right
181+
}
182+
if right == nil {
183+
return left
184+
}
185+
return root
186+
}
187+
```
188+
170189
### **...**
171190

172191
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
func lowestCommonAncestor(root, p, q *TreeNode) *TreeNode {
2+
if root == nil || root == p || root == q {
3+
return root
4+
}
5+
left := lowestCommonAncestor(root.Left, p, q)
6+
right := lowestCommonAncestor(root.Right, p, q)
7+
if left == nil {
8+
return right
9+
}
10+
if right == nil {
11+
return left
12+
}
13+
return root
14+
}

0 commit comments

Comments
 (0)