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

2022/8/29 6:52:57

本文主要是介绍[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;
    }
};


这篇关于[Google] LeetCode 2128 Remove All Ones With Row and Column Flips的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程