{"id":8537,"date":"2021-08-08T20:22:13","date_gmt":"2021-08-09T03:22:13","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=8537"},"modified":"2021-08-08T20:22:56","modified_gmt":"2021-08-09T03:22:56","slug":"leetcode-1886-determine-whether-matrix-can-be-obtained-by-rotation","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/simulation\/leetcode-1886-determine-whether-matrix-can-be-obtained-by-rotation\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1886. Determine Whether Matrix Can Be Obtained By Rotation"},"content":{"rendered":"\n<p>Given two&nbsp;<code>n x n<\/code>&nbsp;binary matrices&nbsp;<code>mat<\/code>&nbsp;and&nbsp;<code>target<\/code>, return&nbsp;<code>true<\/code><em>&nbsp;if it is possible to make&nbsp;<\/em><code>mat<\/code><em>&nbsp;equal to&nbsp;<\/em><code>target<\/code><em>&nbsp;by&nbsp;<strong>rotating<\/strong>&nbsp;<\/em><code>mat<\/code><em>&nbsp;in&nbsp;<strong>90-degree increments<\/strong>, or&nbsp;<\/em><code>false<\/code><em>&nbsp;otherwise.<\/em><\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2021\/05\/20\/grid3.png\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> mat = [[0,1],[1,0]], target = [[1,0],[0,1]]\n<strong>Output:<\/strong> true\n<strong>Explanation: <\/strong>We can rotate mat 90 degrees clockwise to make mat equal target.\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2021\/05\/20\/grid4.png\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> mat = [[0,1],[1,1]], target = [[1,0],[0,1]]\n<strong>Output:<\/strong> false\n<strong>Explanation:<\/strong> It is impossible to make mat equal to target by rotating mat.\n<\/pre>\n\n\n\n<p><strong>Example 3:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2021\/05\/26\/grid4.png\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> mat = [[0,0,0],[0,1,0],[1,1,1]], target = [[1,1,1],[0,1,0],[0,0,0]]\n<strong>Output:<\/strong> true\n<strong>Explanation: <\/strong>We can rotate mat 90 degrees clockwise two times to make mat equal target.\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>n == mat.length == target.length<\/code><\/li><li><code>n == mat[i].length == target[i].length<\/code><\/li><li><code>1 &lt;= n &lt;= 10<\/code><\/li><li><code>mat[i][j]<\/code>&nbsp;and&nbsp;<code>target[i][j]<\/code>&nbsp;are either&nbsp;<code>0<\/code>&nbsp;or&nbsp;<code>1<\/code>.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Simulation<\/strong><\/h2>\n\n\n\n<p>Time complexity: O(n<sup>2<\/sup>)<br>Space complexity: O(1)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"c++\">\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  bool findRotation(vector<vector<int>>& mat, vector<vector<int>>& target) {    \n    const int n = mat.size();\n    auto rot = [n](vector<vector<int>>& mat) {      \n      for (int i = 0; i < n; ++i)\n        for (int j = i; j < n; ++j)\n          swap(mat[i][j], mat[j][i]);\n      for (int j = 0; j < n; j++)\n        for (int i = 0; i < n \/ 2; ++i)\n          swap(mat[i][j], mat[n - i - 1][j]);\n      return mat;\n    };\n    for (int i = 0; i < 4; ++i)\n      if (rot(mat) == target) return true;    \n    return false;\n  }\n};\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Given two&nbsp;n x n&nbsp;binary matrices&nbsp;mat&nbsp;and&nbsp;target, return&nbsp;true&nbsp;if it is possible to make&nbsp;mat&nbsp;equal to&nbsp;target&nbsp;by&nbsp;rotating&nbsp;mat&nbsp;in&nbsp;90-degree increments, or&nbsp;false&nbsp;otherwise. Example 1: Input: mat = [[0,1],[1,0]], target = [[1,0],[0,1]] Output: true&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[48],"tags":[222,718,216,367,179],"class_list":["post-8537","post","type-post","status-publish","format-standard","hentry","category-simulation","tag-easy","tag-inplace","tag-matrix","tag-rotation","tag-simulation","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8537","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=8537"}],"version-history":[{"count":2,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8537\/revisions"}],"predecessor-version":[{"id":8539,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/8537\/revisions\/8539"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=8537"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=8537"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=8537"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}