From 44004ea38f75eede7b3ef8c6965476f4b794a52e Mon Sep 17 00:00:00 2001 From: lincongcong Date: Thu, 22 Jul 2021 23:50:46 +0800 Subject: [PATCH] =?UTF-8?q?add=20code=20of=20=E4=BA=8C=E7=BB=B4=E6=95=B0?= =?UTF-8?q?=E7=BB=84=E7=9A=84=E4=BA=8C=E5=88=86=E6=9F=A5=E6=89=BE=20in=20g?= =?UTF-8?q?o?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../二分查找及其变种/二维数组的二分查找.md | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/animation-simulation/二分查找及其变种/二维数组的二分查找.md b/animation-simulation/二分查找及其变种/二维数组的二分查找.md index b5a27dd..bc24d5f 100644 --- a/animation-simulation/二分查找及其变种/二维数组的二分查找.md +++ b/animation-simulation/二分查找及其变种/二维数组的二分查找.md @@ -52,6 +52,8 @@ #### 题目代码 +Java Code: + ```java class Solution { public boolean searchMatrix(int[][] matrix, int target) { @@ -84,3 +86,32 @@ class Solution { } ``` +Go Code: +```go +func searchMatrix(matrix [][]int, target int) bool { + if len(matrix) == 0 { + return false + } + // 行数 + row := len(matrix) + //列数 + col := len(matrix[0]) + // 行数 * 列数 - 1 为右指针 + left, right := 0, row * col - 1 + for (left <= right) { + mid := left + ((right - left) >> 1) + //将一维坐标变为二维坐标 + rownum := mid / col + colnum := mid % col + if matrix[rownum][colnum] == target { + return true + } else if matrix[rownum][colnum] > target { + right = mid - 1 + } else if matrix[rownum][colnum] < target { + left = mid + 1 + } + } + return false +} +``` +