This commit is contained in:
2026-06-13 03:04:54 +10:00
parent 5022093fad
commit ec07a9322b
23 changed files with 208 additions and 0 deletions
+44
View File
@@ -0,0 +1,44 @@
package top100liked
import (
"fmt"
"testing"
)
func searchMatrix(matrix [][]int, target int) bool {
for i := range len(matrix) {
for j := range len(matrix[0]) {
if matrix[i][j] == target {
return true
}
if j == 0 && matrix[i][j] > target {
return false
}
if matrix[i][j] > target {
break
}
}
}
return false
}
// func searchMatrix(matrix [][]int, target int) bool {
// m, n := len(matrix), len(matrix[0])
// x, y := 0, n-1
// for x < m && y >= 0 {
// if matrix[x][y] == target {
// return true
// }
// if matrix[x][y] > target {
// y--
// } else {
// x++
// }
// }
// return false
// }
func TestS21(t *testing.T) {
fmt.Println(searchMatrix([][]int{{1, 4, 7, 11, 15}, {2, 5, 8, 12, 19}, {3, 6, 9, 16, 22}, {10, 13, 14, 17, 24}, {18, 21, 23, 26, 30}}, 5))
fmt.Println(searchMatrix([][]int{{1, 4, 7, 11, 15}, {2, 5, 8, 12, 19}, {3, 6, 9, 16, 22}, {10, 13, 14, 17, 24}, {18, 21, 23, 26, 30}}, 20))
}