add new solutions
This commit is contained in:
parent
0c73608ce5
commit
d798d5e8c9
19 changed files with 661 additions and 4 deletions
23
solutions/0/q74/solution.go
Normal file
23
solutions/0/q74/solution.go
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
package q74
|
||||
|
||||
func searchMatrix(matrix [][]int, target int) bool {
|
||||
w, h := len(matrix[0]), len(matrix)
|
||||
idx := func(i int) (int, int) { return i / w, i % w }
|
||||
|
||||
l, r := 0, w*h
|
||||
for l < r {
|
||||
m := (l + r) / 2
|
||||
row, col := idx(m)
|
||||
switch {
|
||||
case matrix[row][col] == target:
|
||||
return true
|
||||
case matrix[row][col] < target:
|
||||
l = m + 1
|
||||
default:
|
||||
r = m
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
var _ = searchMatrix
|
||||
Loading…
Add table
Add a link
Reference in a new issue