Skip to content

Latest commit

 

History

History
77 lines (65 loc) · 2.98 KB

README.md

File metadata and controls

77 lines (65 loc) · 2.98 KB

Given two n x n binary matrices mat and target, return true if it is possible to make mat equal to target by rotating mat in 90-degree increments, or false otherwise.

 

Example 1:

Input: mat = [[0,1],[1,0]], target = [[1,0],[0,1]]
Output: true
Explanation: We can rotate mat 90 degrees clockwise to make mat equal target.

Example 2:

Input: mat = [[0,1],[1,1]], target = [[1,0],[0,1]]
Output: false
Explanation: It is impossible to make mat equal to target by rotating mat.

Example 3:

Input: mat = [[0,0,0],[0,1,0],[1,1,1]], target = [[1,1,1],[0,1,0],[0,0,0]]
Output: true
Explanation: We can rotate mat 90 degrees clockwise two times to make mat equal target.

 

Constraints:

  • n == mat.length == target.length
  • n == mat[i].length == target[i].length
  • 1 <= n <= 10
  • mat[i][j] and target[i][j] are either 0 or 1.

Companies:
Amazon

Related Topics:
Array

Similar Questions:

Solution 1.

// OJ: https://leetcode.com/problems/determine-whether-matrix-can-be-obtained-by-rotation/
// Author: github.com/lzl124631x
// Time: O(N^2)
// Space: O(1)
class Solution {
    void rotate(vector<vector<int>> &A) {
        int N = A.size();
        for (int i = 0; i < N / 2; ++i) {
            for (int j = i; j < N - i - 1; ++j) {
                int tmp = A[i][j];
                A[i][j] = A[j][N - i - 1];
                A[j][N - i - 1] = A[N - i - 1][N - j - 1];
                A[N - i - 1][N - j - 1] = A[N - j - 1][i];
                A[N - j - 1][i] = tmp;
            }
        }
    }
public:
    bool findRotation(vector<vector<int>>& A, vector<vector<int>>& B) {
        if (A == B) return true;
        for (int i = 0; i < 3; ++i) {
            rotate(A);
            if (A == B) return true;
        }
        return false;
    }
};