给你两个大小为 n x n 的二进制矩阵 mat 和 target 。现 以 90 度顺时针轮转 矩阵 mat 中的元素 若干次 ,如果能够使 mat 与 target 一致,返回 true ;否则,返回 false 。
示例 1:
输入:mat = [[0,1],[1,0]], target = [[1,0],[0,1]]
输出:true
解释:顺时针轮转 90 度一次可以使 mat 和 target 一致。
示例 2:
输入:mat = [[0,1],[1,1]], target = [[1,0],[0,1]]
输出:false
解释:无法通过轮转矩阵中的元素使 equal 与 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/problems/determine-whether-matrix-can-be-obtained-by-rotation
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
当一个数组旋转不通的度数,其对应坐标是多少我们知道的话,其实不用实际旋转坐标,就可以进行对比
// 转度数 原坐标 转换坐标
90° [x, y] -> [y, n-x]
180° [x, y] -> [n-x, n-y]
270° [x, y] -> [n-y, x]
fun findRotation(mat: Array<IntArray>, target: Array<IntArray>): Boolean {
val length = mat.size
var maxIndex = length - 1
var b360 = true
var b90 = true
var b180 = true
var b270 = true
for (i in 0 until length) {
for (j in 0 until length) {
//360 0
if (mat[i][j] != target[i][j]) {
b360 = false
}
//90
if (mat[i][j] != target[j][maxIndex - i]) {
b90 = false
}
//180
if (mat[i][j] != target[maxIndex - i][maxIndex - j]) {
b180 = false
}
//270
if (mat[i][j] != target[maxIndex - j][i]) {
b270 = false
}
}
}
return b360 || b180 || b270 || b90
}
1.旋转数组确实是一个很好的例子,对于掌握二维数组的坐标很有帮助
2.对于旋转多少度 写出对应坐标确实思路就很明确了
我们可以自己延伸题目 比如逆时针