返回介绍

solution / 2900-2999 / 2946.Matrix Similarity After Cyclic Shifts / README_EN

发布于 2024-06-17 01:02:58 字数 4728 浏览 0 评论 0 收藏 0

2946. Matrix Similarity After Cyclic Shifts

中文文档

Description

You are given a 0-indexed m x n integer matrix mat and an integer k. You have to cyclically right shift odd indexed rows k times and cyclically left shift even indexed rows k times.

Return true _if the initial and final matrix are exactly the same and _false _otherwise._

 

Example 1:

Input: mat = [[1,2,1,2],[5,5,5,5],[6,3,6,3]], k = 2
Output: true
Explanation:


Initially, the matrix looks like the first figure. 
Second figure represents the state of the matrix after one right and left cyclic shifts to even and odd indexed rows.
Third figure is the final state of the matrix after two cyclic shifts which is similar to the initial matrix.
Therefore, return true.

Example 2:

Input: mat = [[2,2],[2,2]], k = 3
Output: true
Explanation: As all the values are equal in the matrix, even after performing cyclic shifts the matrix will remain the same. Therefeore, we return true.

Example 3:

Input: mat = [[1,2]], k = 1
Output: false
Explanation: After one cyclic shift, mat = [[2,1]] which is not equal to the initial matrix. Therefore we return false.

 

Constraints:

  • 1 <= mat.length <= 25
  • 1 <= mat[i].length <= 25
  • 1 <= mat[i][j] <= 25
  • 1 <= k <= 50

Solutions

Solution 1

class Solution:
  def areSimilar(self, mat: List[List[int]], k: int) -> bool:
    n = len(mat[0])
    for i, row in enumerate(mat):
      for j, x in enumerate(row):
        if i % 2 == 1 and x != mat[i][(j + k) % n]:
          return False
        if i % 2 == 0 and x != mat[i][(j - k + n) % n]:
          return False
    return True
class Solution {
  public boolean areSimilar(int[][] mat, int k) {
    int m = mat.length, n = mat[0].length;
    k %= n;
    for (int i = 0; i < m; ++i) {
      for (int j = 0; j < n; ++j) {
        if (i % 2 == 1 && mat[i][j] != mat[i][(j + k) % n]) {
          return false;
        }
        if (i % 2 == 0 && mat[i][j] != mat[i][(j - k + n) % n]) {
          return false;
        }
      }
    }
    return true;
  }
}
class Solution {
public:
  bool areSimilar(vector<vector<int>>& mat, int k) {
    int m = mat.size(), n = mat[0].size();
    k %= n;
    for (int i = 0; i < m; ++i) {
      for (int j = 0; j < n; ++j) {
        if (i % 2 == 1 && mat[i][j] != mat[i][(j + k) % n]) {
          return false;
        }
        if (i % 2 == 0 && mat[i][j] != mat[i][(j - k + n) % n]) {
          return false;
        }
      }
    }
    return true;
  }
};
func areSimilar(mat [][]int, k int) bool {
  n := len(mat[0])
  k %= n
  for i, row := range mat {
    for j, x := range row {
      if i%2 == 1 && x != mat[i][(j+k)%n] {
        return false
      }
      if i%2 == 0 && x != mat[i][(j-k+n)%n] {
        return false
      }
    }
  }
  return true
}
function areSimilar(mat: number[][], k: number): boolean {
  const m = mat.length;
  const n = mat[0].length;
  k %= n;
  for (let i = 0; i < m; ++i) {
    for (let j = 0; j < n; ++j) {
      if (i % 2 === 1 && mat[i][j] !== mat[i][(j + k) % n]) {
        return false;
      }
      if (i % 2 === 0 && mat[i][j] !== mat[i][(j - k + n) % n]) {
        return false;
      }
    }
  }
  return true;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文