
Input: grid =[[0,1,0],[1,0,1],[0,1,0]]Output: trueExplanation: One possible way to remove all 1's from grid is to:- Flip the middle row
- Flip the middle column
Example 2:
1
2
3
4

Input: grid =[[1,1,0],[0,0,0],[0,0,0]]Output: falseExplanation: It is impossible to remove all 1's from grid.
Example 3:
1
2
3
4

Input: grid =[[0]]Output: trueExplanation: There are no 1's in grid.
If we can flip any row or column, the only thing that matters is the pattern of 0s and 1s in each row. If two rows are different, we can flip columns to make them the same, but only if their difference is consistent (i.e., every bit is flipped or not). So, for all rows, either they are the same as the first row, or the bitwise inverse of the first row.
For each row, check if it is either identical to the first row or its bitwise inverse.
If all rows satisfy this, it is possible to remove all ones; otherwise, it is not.
This works because flipping a row or column is equivalent to toggling bits, and the only way to reach all zeros is if all rows can be made the same by column flips.
classSolution {
public:bool removeOnes(vector<vector<int>>& grid) {
int m = grid.size(), n = grid[0].size();
for (int i =1; i < m; ++i) {
bool same = true, inv = true;
for (int j =0; j < n; ++j) {
if (grid[i][j] != grid[0][j]) same = false;
if (grid[i][j] == grid[0][j]) inv = false;
}
if (!same &&!inv) return false;
}
return true;
}
};
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
funcremoveOnes(grid [][]int) bool {
m, n:= len(grid), len(grid[0])
fori:=1; i < m; i++ {
same, inv:=true, trueforj:=0; j < n; j++ {
ifgrid[i][j] !=grid[0][j] {
same = false }
ifgrid[i][j] ==grid[0][j] {
inv = false }
}
if !same&& !inv {
returnfalse }
}
returntrue}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
classSolution {
publicbooleanremoveOnes(int[][] grid) {
int m = grid.length, n = grid[0].length;
for (int i = 1; i < m; ++i) {
boolean same =true, inv =true;
for (int j = 0; j < n; ++j) {
if (grid[i][j]!= grid[0][j]) same =false;
if (grid[i][j]== grid[0][j]) inv =false;
}
if (!same &&!inv) returnfalse;
}
returntrue;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
classSolution {
funremoveOnes(grid: Array<IntArray>): Boolean {
val m = grid.size
val n = grid[0].size
for (i in1 until m) {
var same = truevar inv = truefor (j in0 until n) {
if (grid[i][j] != grid[0][j]) same = falseif (grid[i][j] == grid[0][j]) inv = false }
if (!same &&!inv) returnfalse }
returntrue }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
classSolution:
defremoveOnes(self, grid: list[list[int]]) -> bool:
m, n = len(grid), len(grid[0])
for i in range(1, m):
same =True inv =Truefor j in range(n):
if grid[i][j] != grid[0][j]:
same =Falseif grid[i][j] == grid[0][j]:
inv =Falseifnot same andnot inv:
returnFalsereturnTrue
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
impl Solution {
pubfnremove_ones(grid: Vec<Vec<i32>>) -> bool {
let m = grid.len();
let n = grid[0].len();
for i in1..m {
letmut same =true;
letmut inv =true;
for j in0..n {
if grid[i][j] != grid[0][j] { same =false; }
if grid[i][j] == grid[0][j] { inv =false; }
}
if!same &&!inv { returnfalse; }
}
true }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
classSolution {
removeOnes(grid: number[][]):boolean {
constm=grid.length, n=grid[0].length;
for (leti=1; i<m; ++i) {
letsame=true, inv=true;
for (letj=0; j<n; ++j) {
if (grid[i][j] !==grid[0][j]) same=false;
if (grid[i][j] ===grid[0][j]) inv=false;
}
if (!same&&!inv) returnfalse;
}
returntrue;
}
}