Skip to content

Latest commit

 

History

History
58 lines (50 loc) · 1.9 KB

File metadata and controls

58 lines (50 loc) · 1.9 KB

1886. Determine Whether Matrix Can Be Obtained By Rotation

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.

Solutions (Rust)

1. Solution

impl Solution {
    pub fn find_rotation(mat: Vec<Vec<i32>>, target: Vec<Vec<i32>>) -> bool {
        let mut r0 = true;
        let mut r1 = true;
        let mut r2 = true;
        let mut r3 = true;
        let n = mat.len();

        for r in 0..n {
            for c in 0..n {
                r0 &= mat[r][c] == target[r][c];
                r1 &= mat[r][c] == target[c][n - 1 - r];
                r2 &= mat[r][c] == target[n - 1 - r][n - 1 - c];
                r3 &= mat[r][c] == target[n - 1 - c][r];
            }
        }

        r0 || r1 || r2 || r3
    }
}