You are given an m x n
binary matrix grid.
In one operation, you can choose any row or column and flip each value in that row or column (i.e., changing all 0
‘s to 1
‘s, and all 1
‘s to 0
‘s).
Return true if it is possible to remove all 1
‘s from grid using any number of operations or false otherwise.
Solution
每次将一整行或者一整列进行翻转。一个很明显的特点是,翻转多次的效果是一样的。如果最后能统一翻转成全0,那就必须某一列或者某一行全部是相同的数,否则怎么翻转也不行
点击查看代码
class Solution {
public:
bool removeOnes(vector<vector<int>>& grid) {
int r = grid.size(), c = grid[0].size();
for(int i=1;i<r;i++){
if(grid[i-1]==grid[i])continue;
else{
for(int j=0;j<c;j++){
if(grid[i][j]==grid[i-1][j])return false;
}
}
}
return true;
}
};
原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/282806.html