添加Go语言题解

This commit is contained in:
zouxinyao
2021-07-28 02:26:32 +08:00
parent 7eb8b4a9fd
commit 575b7612f0
52 changed files with 1679 additions and 104 deletions

View File

@@ -83,3 +83,30 @@ class Solution {
}
}
```
Go Code:
```go
func searchMatrix(matrix [][]int, target int) bool {
if len(matrix) == 0 {
return false
}
row, col := len(matrix), len(matrix[0])
// 将二维数组拉平为一维。
l, r := 0, row * col - 1
for l <= r {
m := l + (r - l) / 2
// 将一维的坐标转换为二维
x, y := m / col, m % col
if matrix[x][y] == target {
return true
} else if matrix[x][y] < target {
l = m + 1
} else if matrix[x][y] > target {
// 二分查找时把所有的情况都要写出来避免直接else
r = m - 1
}
}
return false
}
```