LeetCode 剑指 Offer 04. 二维数组中的查找

题目描述

剑指 Offer 04. 二维数组中的查找

image-20250510211717514

image-20250510211736882

image-20241107172311693

思路分析

方案一:从右上角开始查找

image-20250510211624325

参考代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// searchMatrix
func findTargetIn2DPlants(matrix [][]int, target int) bool {
	if len(matrix) == 0 || len(matrix[0]) == 0 {
		return false
	}

	row, col := 0, len(matrix[0])-1 // 从右上角开始
	for row < len(matrix) && col >= 0 {
		if matrix[row][col] == target {
			return true
		} else if matrix[row][col] > target {
			col-- // 向左移动
		} else {
			row++ // 向下移动
		}
	}
	return false
}

➡️ 点击查看 Java 题解

1
write your code here

相似题目

image-20250510211842604

本文作者:
本文链接: https://hgnulb.github.io/blog/2023/26553981
版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议,转载请注明出处!