--- tags: leetcode --- # [2128. Remove All Ones With Row and Column Flips](https://leetcode.com/problems/remove-all-ones-with-row-and-column-flips/) --- # My Solution ## The Key Idea for Solving This Coding Question ## C++ Code ```cpp= class Solution { public: bool removeOnes(vector<vector<int>> &grid) { int rows = grid.size(), cols = grid[0].size(); // Scan each column. // If the first element in a column is 1, // flip all the elements in that column. // Otherwise, do nothing. for (int c = 0; c < cols; ++c) { if (grid[0][c] == 1) { for (int r = 0; r < rows; ++r) { grid[r][c] ^= 1; } } } // Scan each row and count the numbers of 1. // If the number of ones is not zero and not equal to cols, // return false; for (int r = 0; r < rows; ++r) { int ones = 0; for (int c = 0; c < cols; ++c) { if (grid[r][c] == 1) { ++ones; } } if (ones != 0 && ones != cols) { return false; } } return true; } }; ``` ## Time Complexity ## Space Complexity # Miscellaneous <!-- # Test Cases ``` ``` -->