其他分享
首页 > 其他分享> > [Google] LeetCode 2128 Remove All Ones With Row and Column Flips

[Google] LeetCode 2128 Remove All Ones With Row and Column Flips

作者:互联网

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;
    }
};

标签:size,Google,Column,2128,int,grid,row,any,翻转
来源: https://www.cnblogs.com/xinyu04/p/16634645.html