{"id":2829,"date":"2018-05-13T10:20:24","date_gmt":"2018-05-13T17:20:24","guid":{"rendered":"http:\/\/zxi.mytechroad.com\/blog\/?p=2829"},"modified":"2018-05-13T10:20:38","modified_gmt":"2018-05-13T17:20:38","slug":"leetcode-832-flipping-an-image","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/algorithms\/array\/leetcode-832-flipping-an-image\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 832. Flipping an Image"},"content":{"rendered":"<h1><strong>Problem<\/strong><\/h1>\n<div class=\"question-description__2cX5\">\n<div>\n<p>Given a binary matrix\u00a0<code>A<\/code>, we want to flip the image horizontally, then invert it, and return the resulting image.<\/p>\n<p>To flip an image horizontally means that each row of the image is reversed.\u00a0 For example, flipping\u00a0<code>[1, 1, 0]<\/code>\u00a0horizontally results in\u00a0<code>[0, 1, 1]<\/code>.<\/p>\n<p>To invert an image means\u00a0that each\u00a0<code>0<\/code>\u00a0is replaced by\u00a0<code>1<\/code>, and each\u00a0<code>1<\/code>\u00a0is replaced by\u00a0<code>0<\/code>.\u00a0For example, inverting\u00a0<code>[0, 1, 1]<\/code>\u00a0results in\u00a0<code>[1, 0, 0]<\/code>.<\/p>\n<p><strong>Example 1:<\/strong><\/p>\n<pre class=\"crayon:false\"><strong>Input: <\/strong>[[1,1,0],[1,0,1],[0,0,0]]\r\n<strong>Output: <\/strong>[[1,0,0],[0,1,0],[1,1,1]]\r\n<strong>Explanation:<\/strong> First reverse each row: [[0,1,1],[1,0,1],[0,0,0]].\r\nThen, invert the image: [[1,0,0],[0,1,0],[1,1,1]]\r\n<\/pre>\n<p><strong>Example 2:<\/strong><\/p>\n<pre class=\"crayon:false \"><strong>Input: <\/strong>[[1,1,0,0],[1,0,0,1],[0,1,1,1],[1,0,1,0]]\r\n<strong>Output: <\/strong>[[1,1,0,0],[0,1,1,0],[0,0,0,1],[1,0,1,0]]\r\n<strong>Explanation:<\/strong> First reverse each row: [[0,0,1,1],[1,0,0,1],[1,1,1,0],[0,1,0,1]].\r\nThen invert the image: [[1,1,0,0],[0,1,1,0],[0,0,0,1],[1,0,1,0]]\r\n<\/pre>\n<p><strong>Notes:<\/strong><\/p>\n<ul>\n<li><code>1 &lt;= A.length = A[0].length &lt;= 20<\/code><\/li>\n<li><code>0 &lt;= A[i][j]<span style=\"font-family: sans-serif, Arial, Verdana, 'Trebuchet MS';\">\u00a0&lt;=\u00a0<\/span>1<\/code><\/li>\n<\/ul>\n<\/div>\n<\/div>\n<h1>Solution 1: Brute Force<\/h1>\n<p>Time complexity: O(m*n)<\/p>\n<p>Space complexity: O(m*n)<\/p>\n<p>C++<\/p>\n<pre class=\"lang:c++ decode:true\">\/\/ Author: Huahua\r\n\/\/ Running time: 15 ms\r\nclass Solution {\r\npublic:\r\n  vector&lt;vector&lt;int&gt;&gt; flipAndInvertImage(vector&lt;vector&lt;int&gt;&gt;&amp; A) {\r\n    auto B = A;\r\n    int m = A.size();\r\n    int n = A[0].size();\r\n    for (int i = 0; i &lt; m; ++i)\r\n      for (int j = 0; j &lt; n; ++j)\r\n        B[i][j] = 1 - A[i][n - j - 1];\r\n    return B;\r\n  }\r\n};<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Problem Given a binary matrix\u00a0A, we want to flip the image horizontally, then invert it, and return the resulting image. To flip an image horizontally&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[184],"tags":[20,222,302,301],"class_list":["post-2829","post","type-post","status-publish","format-standard","hentry","category-array","tag-array","tag-easy","tag-flip","tag-image","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/2829","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=2829"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/2829\/revisions"}],"predecessor-version":[{"id":2831,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/2829\/revisions\/2831"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=2829"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=2829"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=2829"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}