Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LeetCode:240. 搜索二维矩阵 II,直接查找,详细注释 #476

Open
chencl1986 opened this issue Sep 12, 2024 · 0 comments
Open

Comments

@chencl1986
Copy link
Owner

原题链接:
https://leetcode.cn/problems/search-a-2d-matrix-ii/

解题思路:

  1. 不考虑矩阵的排序特性,直接搜索整个矩阵,查找是否存在等于target的元素即可
/**
 * @param {number[][]} matrix
 * @param {number} target
 * @return {boolean}
 */
var searchMatrix = function (matrix, target) {
  // 搜索二维矩阵中的所有元素,查询是否有雨target相等的元素
  for (let i = 0; i < matrix.length; i++) {
    for (let j = 0; j < matrix[i].length; j++) {
      if (matrix[i][j] === target) {
        return true
      }
    }
  }

  return false
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant