Skip to content

Commit e891be5

Browse files
committedMar 24, 2024·
feat: 0074 Search a 2D Matrix
1 parent 337adb0 commit e891be5

File tree

4 files changed

+172
-0
lines changed

4 files changed

+172
-0
lines changed
 
+93
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
---
2+
title: 74. Search a 2D Matrix
3+
subtitle: "https://leetcode.com/problems/search-a-2d-matrix/description/"
4+
date: 2024-03-24T22:10:00+08:00
5+
lastmod: 2024-03-24T22:10:00+08:00
6+
draft: false
7+
author: "Kimi.Tsai"
8+
authorLink: "https://kimi0230.github.io/"
9+
description: "0074.Search-a-2D-Matrix"
10+
license: ""
11+
images: []
12+
13+
tags: [LeetCode, Go, Medium, Search a 2D Matrix, Array, Binary Search, Matrix, Facebook, Amazon, Microsoft, Bloomberg]
14+
categories: [LeetCode]
15+
16+
featuredImage: ""
17+
featuredImagePreview: ""
18+
19+
hiddenFromHomePage: false
20+
hiddenFromSearch: false
21+
twemoji: false
22+
lightgallery: true
23+
ruby: true
24+
fraction: true
25+
fontawesome: true
26+
linkToMarkdown: false
27+
rssFullText: false
28+
29+
toc:
30+
enable: true
31+
auto: true
32+
code:
33+
copy: true
34+
maxShownLines: 200
35+
math:
36+
enable: false
37+
# ...
38+
mapbox:
39+
# ...
40+
share:
41+
enable: true
42+
# ...
43+
comment:
44+
enable: true
45+
# ...
46+
library:
47+
css:
48+
# someCSS = "some.css"
49+
# located in "assets/"
50+
# Or
51+
# someCSS = "https://cdn.example.com/some.css"
52+
js:
53+
# someJS = "some.js"
54+
# located in "assets/"
55+
# Or
56+
# someJS = "https://cdn.example.com/some.js"
57+
seo:
58+
images: []
59+
# ...
60+
---
61+
# [74. Search a 2D Matrix](https://leetcode.com/problems/search-a-2d-matrix/description/)
62+
63+
## 題目
64+
65+
## 題目大意
66+
編寫一個高效的演算法來判斷 m x n 矩陣中,是否存在一個目標值。 該矩陣具有如下特性:
67+
* 每行中的整數從左到右按升序排列。
68+
* 每行的第一个整数大于前一行的最后一个整数。
69+
70+
## 解題思路
71+
可以將二維轉成一維矩陣
72+
73+
## Big O
74+
75+
* 時間複雜 : ``
76+
* 空間複雜 : ``
77+
78+
## 來源
79+
* https://leetcode.com/problems/search-a-2d-matrix/description/
80+
* https://leetcode.cn/problems/
81+
82+
## 解答
83+
https://github.com/kimi0230/LeetcodeGolang/blob/master/Leetcode/0074.Search-a-2D-Matrix/main.go
84+
85+
```go
86+
87+
```
88+
89+
## Benchmark
90+
91+
```sh
92+
93+
```
+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package searcha2dmatrix
2+
3+
// 時間複雜 O(), 空間複雜 O()
4+
func searchMatrix(matrix [][]int, target int) bool {
5+
m := len(matrix)
6+
if m <= 0 {
7+
return false
8+
}
9+
n := len(matrix[0])
10+
11+
left, right := 0, m*n-1
12+
for left <= right {
13+
mid := int(uint(right+left) >> 1) //left + (right-left)/2
14+
val := getMatrix(matrix, mid)
15+
if val == target {
16+
return true
17+
} else if val < target {
18+
left = mid + 1
19+
} else {
20+
right = mid - 1
21+
}
22+
}
23+
return false
24+
}
25+
26+
// 將 mxn的二維陣列 映射成一維陣列
27+
func getMatrix(matrix [][]int, index int) int {
28+
n := len(matrix[0])
29+
i, j := index/n, index%n
30+
return matrix[i][j]
31+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package searcha2dmatrix
2+
3+
import "testing"
4+
5+
var tests = []struct {
6+
matrix [][]int
7+
target int
8+
want bool
9+
}{
10+
{
11+
[][]int{
12+
{1, 3, 5, 7},
13+
{10, 11, 16, 20},
14+
{23, 30, 34, 60},
15+
},
16+
3,
17+
true,
18+
},
19+
{
20+
[][]int{
21+
{1, 3, 5, 7},
22+
{10, 11, 16, 20},
23+
{23, 30, 34, 60},
24+
},
25+
13,
26+
false,
27+
},
28+
{
29+
[][]int{},
30+
5,
31+
false,
32+
},
33+
}
34+
35+
func TestSearchMatrix(t *testing.T) {
36+
for _, tt := range tests {
37+
if got := searchMatrix(tt.matrix, tt.target); got != tt.want {
38+
t.Errorf("searchMatrix(%v, %v) = %v, want %v", tt.matrix, tt.target, got, tt.want)
39+
}
40+
}
41+
}
42+
43+
func BenchmarkSearchMatrix(b *testing.B) {
44+
for i := 0; i < b.N; i++ {
45+
searchMatrix(tests[0].matrix, tests[0].target)
46+
}
47+
}

‎README.md

+1
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,7 @@ https://labuladong.gitee.io/algo/2/21/57/
308308
| [0142](https://kimi0230.github.io/LeetcodeGolang/Leetcode/0142.Linked-List-CycleII/) | [Linked List Cycle II](https://leetcode.com/problems/linked-list-cycle-ii/) | [Go](https://github.com/kimi0230/LeetcodeGolang/tree/master/Leetcode/0142.Linked-List-CycleII) | Medium | O(n) | O(1) | Linked List, Two Pointers |
309309
| [0344](https://kimi0230.github.io/LeetcodeGolang/Leetcode/0344.Reverse-String/) | [Reverse String](https://leetcode.com/problems/reverse-string/) | [Go](https://github.com/kimi0230/LeetcodeGolang/tree/master/Leetcode/0344.Reverse-String) | Easy | O(n) | O(1) | Two Pointers |
310310
| [0876](https://kimi0230.github.io/LeetcodeGolang/Leetcode/0876.Middle-of-the-Linked-List/) | [Middle of the Linked List](https://leetcode.com/problems/middle-of-the-linked-list/) | [Go](https://github.com/kimi0230/LeetcodeGolang/tree/master/Leetcode/0876.Middle-of-the-Linked-List) | Easy | | | Linked List, Two Pointers |
311+
| [0074](https://kimi0230.github.io/LeetcodeGolang/Leetcode/0074.Search-a-2D-Matrix/) | [Search a 2D Matrix](https://leetcode.com/problems/search-a-2d-matrix/) | [Go](https://github.com/kimi0230/LeetcodeGolang/tree/master/Leetcode/0074.Search-a-2D-Matrix) | Medium | | | Binary Search, Two Pointers |
311312

312313
---
313314

0 commit comments

Comments
 (0)
Please sign in to comment.