Validate Rows and Columns
Problem statement
You are given a nonempty square integer matrix grid with n rows and n columns. Return true if every row and every column is valid; otherwise return false.
For this exercise, a valid row or column contains every integer from 1 through n exactly once. A value outside that range makes the grid invalid.
The grid is fully filled. Check rows and columns only; there is no subgrid or diagonal requirement.
Function
isValidGrid(grid: int[][]) → booleanExamples
Example 1
grid = [[1,2],[2,1]]return = trueEvery row and column contains 1 and 2 exactly once.
Example 2
grid = [[1,2],[1,2]]return = falseThe rows are valid, but each column repeats a value.
Example 3
grid = [[0]]return = falseA one-cell grid must contain 1; 0 is outside its allowed range.
Constraints
- For this exercise, assume
1 <= n <= 50. grid.length == nandgrid[i].length == n.- For this exercise, assume
-10^9 <= grid[i][j] <= 10^9.