💙 【2133】Check if Every Row and Column Contains All Numbers easy
An n x n
matrix is valid if every row and every column contains all the integers from 1
to n
(inclusive).
Given an n x n
integer matrix matrix
, return true
if the matrix is valid. Otherwise, return false
.
Example 1:
Input: matrix = [[1,2,3],[3,1,2],[2,3,1]]
Output: true
Explanation: In this case, n = 3, and every row and column contains the numbers 1, 2, and 3.
Hence, we return true.
Example 2:
Input: matrix = [[1,1,1],[1,2,3],[1,2,3]]
Output: false
Explanation: In this case, n = 3, but the first row and the first column do not contain the numbers 2 or 3.
Hence, we return false.
Constraints:
n == matrix.length == matrix[i].length
1 <= n <= 100
1 <= matrix[i][j] <= n
📝个人分析
如果每一行和每一列包含从1到n(包括1到n)的所有整数,则nxn矩阵有效。 给定一个nxn整数矩阵,如果该矩阵有效,则返回true。否则,返回false。
矩阵行列相等,按照常规循环。这里使用HashSet来记录是否存在重复值。Set不会存入相同的内容,每次循环检查set长度是否与矩阵行或者列相等。
💡Solution
1 public boolean checkValid(int[][] matrix) {
2 int n = matrix.length;
3 for(int i = 0;i<n;i++){
4 Set<Integer> row = new HashSet<>();
5 Set<Integer> col = new HashSet<>();
6 for(int j = 0;j<n;j++){
7 row.add(matrix[i][j]);
8 col.add(matrix[j][i]);
9 }
10 if(row.size() != n || col.size() != n)return false;
11 }
12 return true;
13 }
🔗 Refer links
https://leetcode.com/problems/check-if-every-row-and-column-contains-all-numbers/