返回介绍

solution / 2300-2399 / 2319.Check if Matrix Is X-Matrix / README_EN

发布于 2024-06-17 01:03:07 字数 5317 浏览 0 评论 0 收藏 0

2319. Check if Matrix Is X-Matrix

中文文档

Description

A square matrix is said to be an X-Matrix if both of the following conditions hold:

  1. All the elements in the diagonals of the matrix are non-zero.
  2. All other elements are 0.

Given a 2D integer array grid of size n x n representing a square matrix, return true_ if _grid_ is an X-Matrix_. Otherwise, return false.

 

Example 1:

Input: grid = [[2,0,0,1],[0,3,1,0],[0,5,2,0],[4,0,0,2]]
Output: true
Explanation: Refer to the diagram above. 
An X-Matrix should have the green elements (diagonals) be non-zero and the red elements be 0.
Thus, grid is an X-Matrix.

Example 2:

Input: grid = [[5,7,0],[0,3,1],[0,5,0]]
Output: false
Explanation: Refer to the diagram above.
An X-Matrix should have the green elements (diagonals) be non-zero and the red elements be 0.
Thus, grid is not an X-Matrix.

 

Constraints:

  • n == grid.length == grid[i].length
  • 3 <= n <= 100
  • 0 <= grid[i][j] <= 105

Solutions

Solution 1

class Solution:
  def checkXMatrix(self, grid: List[List[int]]) -> bool:
    for i, row in enumerate(grid):
      for j, v in enumerate(row):
        if i == j or i + j == len(grid) - 1:
          if v == 0:
            return False
        elif v:
          return False
    return True
class Solution {
  public boolean checkXMatrix(int[][] grid) {
    int n = grid.length;
    for (int i = 0; i < n; ++i) {
      for (int j = 0; j < n; ++j) {
        if (i == j || i + j == n - 1) {
          if (grid[i][j] == 0) {
            return false;
          }
        } else if (grid[i][j] != 0) {
          return false;
        }
      }
    }
    return true;
  }
}
class Solution {
public:
  bool checkXMatrix(vector<vector<int>>& grid) {
    int n = grid.size();
    for (int i = 0; i < n; ++i) {
      for (int j = 0; j < n; ++j) {
        if (i == j || i + j == n - 1) {
          if (!grid[i][j]) {
            return false;
          }
        } else if (grid[i][j]) {
          return false;
        }
      }
    }
    return true;
  }
};
func checkXMatrix(grid [][]int) bool {
  for i, row := range grid {
    for j, v := range row {
      if i == j || i+j == len(row)-1 {
        if v == 0 {
          return false
        }
      } else if v != 0 {
        return false
      }
    }
  }
  return true
}
function checkXMatrix(grid: number[][]): boolean {
  const n = grid.length;
  for (let i = 0; i < n; ++i) {
    for (let j = 0; j < n; ++j) {
      if (i == j || i + j == n - 1) {
        if (!grid[i][j]) {
          return false;
        }
      } else if (grid[i][j]) {
        return false;
      }
    }
  }
  return true;
}
impl Solution {
  pub fn check_x_matrix(grid: Vec<Vec<i32>>) -> bool {
    let n = grid.len();
    for i in 0..n {
      for j in 0..n {
        if i == j || i + j == n - 1 {
          if grid[i][j] == 0 {
            return false;
          }
        } else if grid[i][j] != 0 {
          return false;
        }
      }
    }
    true
  }
}
public class Solution {
  public bool CheckXMatrix(int[][] grid) {
    int n = grid.Length;
    for (int i = 0; i < n; ++i) {
      for (int j = 0; j < n; ++j) {
        if (i == j || i + j == n - 1) {
          if (grid[i][j] == 0) {
            return false;
          }
        } else if (grid[i][j] != 0) {
          return false;
        }
      }
    }
    return true;
  }
}
bool checkXMatrix(int** grid, int gridSize, int* gridColSize) {
  for (int i = 0; i < gridSize; i++) {
    for (int j = 0; j < gridSize; j++) {
      if (i == j || i + j == gridSize - 1) {
        if (grid[i][j] == 0) {
          return false;
        }
      } else if (grid[i][j] != 0) {
        return false;
      }
    }
  }
  return true;
}

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

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

发布评论

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