Skip to content

1886. 判断矩阵经轮转后是否一致 #47

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

Open
buuing opened this issue Jun 6, 2021 · 0 comments
Open

1886. 判断矩阵经轮转后是否一致 #47

buuing opened this issue Jun 6, 2021 · 0 comments

Comments

@buuing
Copy link
Owner

buuing commented Jun 6, 2021

给你两个大小为 n x n 的二进制矩阵 mat 和 target 。现 以 90 度顺时针轮转 矩阵 mat 中的元素 若干次 ,如果能够使 mat 与 target 一致,返回 true ;否则,返回 false 。

示例 1:

image

输入:mat = [[0,1],[1,0]], target = [[1,0],[0,1]]
输出:true
解释:顺时针轮转 90 度一次可以使 mat 和 target 一致。

示例 2:

image

输入:mat = [[0,1],[1,1]], target = [[1,0],[0,1]]
输出:false
解释:无法通过轮转矩阵中的元素使 equal 与 target 一致。

示例 3:

image

输入:mat = [[0,0,0],[0,1,0],[1,1,1]], target = [[1,1,1],[0,1,0],[0,0,0]]
输出:true
解释:顺时针轮转 90 度两次可以使 mat 和 target 一致。

提示:

  • n == mat.length == target.length
  • n == mat[i].length == target[i].length
  • 1 <= n <= 10
  • mat[i][j] 和 target[i][j] 不是 0 就是 1

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/determine-whether-matrix-can-be-obtained-by-rotation
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。




const findRotation = (mat, target) => {
  let n = mat.length
  const rotate = (grid) => {
    for (let i = 0; i < n; i++) {
      for (let j = 0; j < i; j++) {
        [grid[i][j], grid[j][i]] = [grid[j][i], grid[i][j]]
      }
    }
    for (let i = 0; i < n; i++) {
      for (let j = 0; j < n / 2; j++) {
        [grid[i][j], grid[i][n - j - 1]] = [grid[i][n - j - 1], grid[i][j]]
      }
    }
  }
  return [0,0,0,0].some(() => {
    rotate(mat)
    for (let i = 0; i < n; i++) {
      for (let j = 0; j < n; j++) {
        if (mat[i][j] !== target[i][j]) return false
      }
    }
    return true
  })
}
@buuing buuing changed the title 1885. 判断矩阵经轮转后是否一致 1886. 判断矩阵经轮转后是否一致 Jun 7, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant