8000 search_a_2D_matrix_II · hitzzc/go-leetcode@cd90839 · GitHub
[go: up one dir, main page]

Skip to content

Commit cd90839

Browse files
committed
search_a_2D_matrix_II
1 parent 1cc60e3 commit cd90839

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,7 @@ Golang solution for leetcode. For each problem, there is a simple *_test.go to t
195195
#### [237. Delete Node in a Linked List](https://github.com/hitzzc/go-leetcode/tree/master/delete_node_in_a_linked_list)
196196
#### [238. product of array except self](https://github.com/hitzzc/go-leetcode/tree/master/product_of_array_except_self)
197197
#### [239. Sliding Window Maximum](https://github.com/hitzzc/go-leetcode/tree/master/sliding_window_maximum)
198+
#### [240. Search a 2D Matrix II](https://github.com/hitzzc/go-leetcode/tree/master/search_a_2D_matrix_II)
198199

199200

200201

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package search_a_2D_matrix_II
2+
3+
func searchMatrix(matrix [][]int, target int) bool {
4+
if len(matrix) == 0 || len(matrix[0]) == 0 {
5+
return false
6+
}
7+
row, col := 0, len(matrix[0])-1
8+
for row <= len(matrix)-1 && col >= 0 {
9+
if matrix[row][col] == target {
10+
return true
11+
}
12+
for row <= len(matrix)-1 && matrix[row][col] < target {
13+
row++
14+
}
15+
for col >= 0 && matrix[row][col] > target {
16+
col--
17+
}
18+
}
19+
return false
20+
}

0 commit comments

Comments
 (0)
0