[C++] 3 Solutions Compared and Explained, 100% Time, 100% Space
This problem is literally telling us what to do, so I guess there is no much to explain in terms of theory or mathematical tricks here - just fun to tackle it i
This problem is literally telling us what to do, so I guess there is no much to explain in terms of theory or mathematical tricks here - just fun to tackle it in multiple ways if you wish so.\n\nFirst of all, a simple, concise version taking each row as a reference, reversing it and then switching the bits on it:\n\n```cpp\nclass Solution {\npublic:\n    vector<vector<int>> flipAndInvertImage(vector<vector<int>>& arr) {\n        for (auto &row: arr) {\n            // reversing the row\n\t\t\treverse(begin(row), end(row));\n\t\t\t// flipping its bits\n            transform(begin(row), end(row), begin(row), [](auto b){return !b;});\n        }\n        return arr;\n    }\n}\n```\n\nA slightly more performance version came to me from [Abishek\'s brilliant solution](https://leetcode.com/problems/flipping-an-image/discuss/930432/), that I tweaked a bit to see if I can make it even a bit more optimal.\n\nFirst of all we declare 2 support variables:\n* `halfLen` is half the size of each row, to save recomputing it all the time;\n* `len` is the full length of it `- 1`, again because we want to avoid recomputing the same stuff over and over again.\n\nAbishek\'s idea was that things change only when the values of 2 elements with the same distance from the centre is different.\n\nFor example, given the row:\n\n```cpp\n0 0 1 1 1 0 1\n```\n\nThe first and the last element will stay the same, because you swap them and then flip them, you will still end up with `0 ... 1` at his extremes:\n\n```cpp\n0 0 1 1 1 0 1\n```\n\nProceeding with second and penultimate, you will see that both are equals, so this time we will have a change, which means both will flip:\n\n```cpp\n0 1 1 1 1 1 1\n```\n\nSimilarly with third and antepenultimate, both equal to `1`:\n\n```cpp\n0 1 0 1 0 1 1\n```\n\nFinally, with odd length rows, the central element is always going to be flipped:\n\n```cpp\n0 1 0 0 0 1 1\n```\n\nWith a nested loop, going by row first and by element later, we can easily work like that and save ourselves the pain (and computatinal cost) of a few unnecessary swaps and flips).\n\nOnce we are done, time to return `arr` :)\n\nThe code:\n\n```cpp\nclass Solution {\npublic:\n    vector<vector<int>> flipAndInvertImage(vector<vector<int>>& arr) {\n        // support variables\n        int halfLen = arr[0].size() / 2, len = arr[0].size() - 1;\n        for (auto &row: arr) {\n            for (int i = 0; i < halfLen; i++) {\n                // working only on equal pairss\n                if (row[i] == row[len - i]) {\n                    row[i] = !row[i];\n                    row[len - i] = row[i];\n                }\n            }\n            // swapping the central element for rows of odd length\n            if (len % 2 == 0) row[halfLen] = !row[halfLen];\n        }\n        return arr;\n    }\n};\n```\n\nFor comparison, the needlessly complicated solution I came up with months ago - thanks LeetCode for improving my skills to the point in which I no longer write this kind of stuff :D\n\nThe previous code:\n\n```cpp\nclass Solution {\npublic:\n    vector<vector<int>> flipAndInvertImage(vector<vector<int>>& A) {\n        int i, tmp, lastPos;\n        transform(begin(A), end(A), begin(A), [&](vector<int> e){\n            lastPos = e.size() -1;\n            for (i = 0; i < e.size() / 2; i++) {\n                tmp = e[i];\n                e[i] = !(e[lastPos - i]);\n                e[lastPos - i] = !tmp;\n            }\n            if (e.size() % 2) e[e.size() / 2] = !e[e.size() / 2];\n            return e;\n        });\n        return A;\n    }\n};\n```
['Array', 'C', 'Matrix', 'C++']