diff options
author | Jack Sangdahl <[email protected]> | 2025-09-12 22:36:22 -0600 |
---|---|---|
committer | Jack Sangdahl <[email protected]> | 2025-09-12 22:36:22 -0600 |
commit | 77d6e1dd41c644ed28fb61a3f19614965b43908c (patch) | |
tree | dd3bd4f8d3ad9df7a2f0a8728f40d80e09e98184 /check_every_row_col_contain_num.py | |
parent | e648ecc896f5ac7db93b046699adf1832e74405f (diff) | |
download | leetcode-77d6e1dd41c644ed28fb61a3f19614965b43908c.tar.gz leetcode-77d6e1dd41c644ed28fb61a3f19614965b43908c.zip |
2133. check if every row and column contains all numbers
Diffstat (limited to 'check_every_row_col_contain_num.py')
-rw-r--r-- | check_every_row_col_contain_num.py | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/check_every_row_col_contain_num.py b/check_every_row_col_contain_num.py new file mode 100644 index 0000000..32d19a0 --- /dev/null +++ b/check_every_row_col_contain_num.py @@ -0,0 +1,31 @@ +# 2133. Check if Every Row and Column Contains All Numbers + +""" +an 'n x n' matrix is valid if every row and every column contains all the +integers from 1 to 'n'. given an 'n x n' matrix, return true if the matrix is +valid, otherwise return false. +""" + + +class Solution(object): + def checkValid(self, matrix): + """ + :type matrix: List[List[int]] + :rtype: bool + """ + n = len(matrix) + for i in range(n): + row, col, mask = 1, 1, 1 + for j in range(n): + row ^= 1 << matrix[i][j] + col ^= 1 << matrix[j][i] + mask |= 1 << j + 1 + if row ^ mask or col ^ mask: + return False + return True + + +if __name__ == "__main__": + obj = Solution() + print(obj.checkValid(matrix=[[1, 2, 3], [3, 1, 2], [2, 3, 1]])) + print(obj.checkValid(matrix=[[1, 1, 1], [1, 2, 3], [1, 2, 3]])) |