rocksc30 / LeetCode

用于力扣刷题打卡
2 stars 0 forks source link

2319. 判断矩阵是否是一个 X 矩阵 #58

Open rocksc30 opened 1 year ago

rocksc30 commented 1 year ago
class Solution {
    public boolean checkXMatrix(int[][] grid) {
        int len = grid.length;
        for(int i = 0; i < len; i++){
            for(int j = 0; j < len; j++){
                if(i == j || i + j == len - 1){
                    if(grid[i][j] == 0){
                        return false;
                    }
                }else{
                    if(grid[i][j] != 0){
                        return false;
                    }
                }
            }
        }
        return true;
    }
}
Ni-Guvara commented 1 year ago
class Solution {
public:
    bool checkXMatrix(vector<vector<int>>& grid) {
        int len = grid.size();

        for(int i = 0 ; i < len ; i++)
        {
            for(int j = 0 ;j < len ; j++)
            {
                if( i == j || j == len - i - 1)
                {
                    if( grid[i][j] == 0 )
                        return false;
                }else
                {
                    if(grid[i][j] != 0)
                        return false;
                }
            }
        }
        return true;         
    }
};