74.搜索二维矩阵
74.搜索二维矩阵
题解
其实就是二维矩阵转一维,然后二分就好了,水题
代码
package main func searchMatrix(matrix [][]int, target int) bool { row := len(matrix) col := len(matrix[0]) left, right := 0, row*col-1 for left+1 < right { mid := left + (right-left)/2 midNum := matrix[mid/col][mid%col] if midNum > target { right = mid } else if midNum < target { left = mid } else { right = mid } } if matrix[left/col][left%col] == target || matrix[right/col][right%col] == target { return true } return false }