python 🐍 | 6 Methods with explanation 🎯
Platform: leetcode.com\n832. Flipping an Image\nLink: https://leetcode.com/problems/flipping-an-image/\nDifficulty: Easy\nAuthor : hritik5102\nDate: 31/1/2021\n
**Platform**: leetcode.com\n**832.** Flipping an Image\n**Link**: https://leetcode.com/problems/flipping-an-image/\n**Difficulty**: Easy\n**Author** : hritik5102\n**Date**: 31/1/2021\n**Post Discussion** : https://leetcode.com/problems/flipping-an-image/discuss/1044064/python-or-6-Methods-with-explanation\n**Submission** : https://leetcode.com/submissions/detail/450220569/\n**(Time, Space) Complexity** : O(n), O(n)\n\n\n# Method 1 : Easy and consice\n\n## Reverse the row and the invert the number i.e. if 1 then convert it into 0 or vice versa\n```python\n>> arr = [1,2,3]  \n>> arr[::-1]   \n>> [3,2,1]\n```\n\n```\nclass Solution:\n    def flipAndInvertImage(self, A):\n        res = []\n        for arr in A:\n            res.append(list(map(lambda x:0 if x else 1, arr[::-1])))\n        return res            \n```\n\n# Method 02 : Create a dictionary and the same is logic\n\n```\nclass Solution:\n    def flipAndInvertImage(self, A):\n        res = []\n        d = {\n            0:1,\n            1:0\n        }\n        for arr in A:\n            res.append(list(map(lambda x: d[x], arr[::-1])))\n        return res            \n```\n\n# Method 03 : Instead of using map and lambda function, use list comprehesion which is faster\n\n```\nclass Solution:\n    def flipAndInvertImage(self, A):\n        res = []\n        d = {\n            0:1,\n            1:0\n        }\n        for arr in A:\n            res.append([d[i] for i in arr[::-1]])\n        return res            \n```\n\n# Method 04 : List comprehension\n```\nclass Solution:\n    def flipAndInvertImage(self, A):\n        d = {\n            0:1,\n            1:0\n        }\n        return [[d[i] for i in arr[::-1]] for arr in A]\n```\n        \n# Method 05 : One liner but faster\n```\nclass Solution:\n    def flipAndInvertImage(self, A):\n        return [[1^i for i in arr[::-1]] for arr in A]\n```\n\n# Method 06 : Fastest method  (Reference from : [@xieal](https://leetcode.com/xieal) )\n```\nclass Solution:\n    def flipAndInvertImage(self, A):\n        return [[l[i] ^ 1 for i in range(len(l) - 1, -1, -1)] for l in A]\n\nobj = Solution()\nprint(obj.flipAndInvertImage(A = [[1,1,0],[1,0,1],[0,0,0]]))        \n```
['Python']