Skip to content

Commit 112523e

Browse files
committed
feat:add golang solutions for lcof problems 03-05
1 parent 7aaa1b5 commit 112523e

File tree

6 files changed

+78
-0
lines changed

6 files changed

+78
-0
lines changed

lcof/面试题03. 数组中重复的数字/README.md

+16
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,22 @@ var findRepeatNumber = function(nums) {
7474
};
7575
```
7676

77+
### Go
78+
79+
```go
80+
func findRepeatNumber(nums []int) int {
81+
for i := 0; i < len(nums); i++ {
82+
for nums[i] != i {
83+
if nums[i] == nums[nums[i]] {
84+
return nums[i]
85+
}
86+
nums[i], nums[nums[i]] = nums[nums[i]], nums[i]
87+
}
88+
}
89+
return -1
90+
}
91+
```
92+
7793
### ...
7894
```
7995
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
func findRepeatNumber(nums []int) int {
2+
for i := 0; i < len(nums); i++ {
3+
for nums[i] != i {
4+
if nums[i] == nums[nums[i]] {
5+
return nums[i]
6+
}
7+
nums[i], nums[nums[i]] = nums[nums[i]], nums[i]
8+
}
9+
}
10+
return -1
11+
}

lcof/面试题04. 二维数组中的查找/README.md

+23
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,29 @@ var findNumberIn2DArray = function(matrix, target) {
9898
};
9999
```
100100

101+
### Go
102+
```go
103+
func findNumberIn2DArray(matrix [][]int, target int) bool {
104+
if len(matrix) == 0 {
105+
return false
106+
}
107+
rows, cols := len(matrix), len(matrix[0])
108+
i, j := rows - 1, 0
109+
for i >= 0 && j < cols {
110+
if matrix[i][j] == target {
111+
return true
112+
}
113+
if matrix[i][j] > target {
114+
i--
115+
} else {
116+
j++
117+
}
118+
}
119+
return false
120+
}
121+
```
122+
123+
101124
### ...
102125
```
103126
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
func findNumberIn2DArray(matrix [][]int, target int) bool {
2+
if len(matrix) == 0 {
3+
return false
4+
}
5+
rows, cols := len(matrix), len(matrix[0])
6+
i, j := rows - 1, 0
7+
for i >= 0 && j < cols {
8+
if matrix[i][j] == target {
9+
return true
10+
}
11+
if matrix[i][j] > target {
12+
i--
13+
} else {
14+
j++
15+
}
16+
}
17+
return false
18+
}

lcof/面试题05. 替换空格/README.md

+7
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,13 @@ var replaceSpace = function(s) {
4444
};
4545
```
4646

47+
### Go
48+
```go
49+
func replaceSpace(s string) string {
50+
return strings.Replace(s, " ", "%20", -1 )
51+
}
52+
```
53+
4754
### ...
4855
```
4956
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
func replaceSpace(s string) string {
2+
return strings.Replace(s, " ", "%20", -1 )
3+
}

0 commit comments

Comments
 (0)