Skip to content

Commit 78b9574

Browse files
authored
Merge pull request 6boris#86 from kylesliu/develop
add the 74 problem solution
2 parents 50f187c + 2295eaa commit 78b9574

File tree

3 files changed

+57
-19
lines changed

3 files changed

+57
-19
lines changed

src/0074.Search-a-2D-Matrix/README.md

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,21 +23,36 @@ Output: "10101"
2323
**Tags:** Math, String
2424

2525
## 题意
26-
>给你两个二进制串,求其和的二进制串。
26+
>写一个高效算法,在矩阵中查找一个数是否存在。矩阵有如下特点:
27+
- 矩阵中每行的数,从左到右单调递增;
28+
- 每行行首的数大于上一行行尾的数;
2729

2830
## 题解
2931

3032
### 思路1
31-
> 按照小学算数那么来做,用 `carry` 表示进位,从后往前算,依次往前,每算出一位就插入到最前面即可,直到把两个二进制串都遍历完即可。
33+
> 我们可以想象把整个矩阵,按行展开成一个一维数组,那么这个一维数组单调递增,然后直接二分即可。
34+
二分时可以通过整除和取模运算得到二维数组的坐标。
35+
时间复杂度分析:二分的时间复杂度是 O(logn2)=O(logn)O(logn2)=O(logn)。
3236

33-
```go
34-
35-
```
3637

37-
### 思路2
38-
> 思路2
3938
```go
40-
39+
func searchMatrix(matrix [][]int, target int) bool {
40+
if len(matrix) == 0 {
41+
return false
42+
}
43+
n, m := len(matrix), len(matrix[0])
44+
l, r := 0, n*m-1
45+
46+
for l < r {
47+
mid := (l + r) / 2
48+
if matrix[mid/m][mid%m] >= target {
49+
r = mid
50+
} else {
51+
l = mid + 1
52+
}
53+
}
54+
return matrix[r/m][r%m] == target
55+
}
4156
```
4257

4358
## 结语
Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,19 @@
11
package Solution
22

3-
func Solution(x bool) bool {
4-
return x
3+
func searchMatrix(matrix [][]int, target int) bool {
4+
if len(matrix) == 0 {
5+
return false
6+
}
7+
n, m := len(matrix), len(matrix[0])
8+
l, r := 0, n*m-1
9+
10+
for l < r {
11+
mid := (l + r) / 2
12+
if matrix[mid/m][mid%m] >= target {
13+
r = mid
14+
} else {
15+
l = mid + 1
16+
}
17+
}
18+
return matrix[r/m][r%m] == target
519
}

src/0074.Search-a-2D-Matrix/Solution_test.go

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,37 @@ package Solution
22

33
import (
44
"reflect"
5+
"strconv"
56
"testing"
67
)
78

89
func TestSolution(t *testing.T) {
910
// 测试用例
1011
cases := []struct {
1112
name string
12-
inputs bool
13+
matrix [][]int
14+
target int
1315
expect bool
1416
}{
15-
{"TestCacse 1", true, true},
16-
{"TestCacse 1", true, true},
17-
{"TestCacse 1", false, false},
17+
{"TestCase", [][]int{
18+
{1, 3, 5, 7},
19+
{10, 11, 16, 20},
20+
{23, 30, 34, 50},
21+
}, 3, true},
22+
{"TestCase", [][]int{
23+
{1, 3, 5, 7},
24+
{10, 13, 16, 20},
25+
{23, 30, 34, 50},
26+
}, 13, true},
1827
}
1928

2029
// 开始测试
21-
for _, c := range cases {
22-
t.Run(c.name, func(t *testing.T) {
23-
ret := Solution(c.inputs)
24-
if !reflect.DeepEqual(ret, c.expect) {
30+
for i, c := range cases {
31+
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
32+
got := searchMatrix(c.matrix, c.target)
33+
if !reflect.DeepEqual(got, c.expect) {
2534
t.Fatalf("expected: %v, but got: %v, with inputs: %v",
26-
c.expect, ret, c.inputs)
35+
c.expect, got, c.matrix)
2736
}
2837
})
2938
}

0 commit comments

Comments
 (0)