Skip to content

Commit 942503a

Browse files
committedJan 6, 2022
Add solution #1886
1 parent f9d8ecf commit 942503a

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed
 

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,7 @@
217217
1672|[Richest Customer Wealth](./1672-richest-customer-wealth.js)|Easy|
218218
1780|[Check if Number is a Sum of Powers of Three](./1780-check-if-number-is-a-sum-of-powers-of-three.js)|Medium|
219219
1880|[Check if Word Equals Summation of Two Words](./1880-check-if-word-equals-summation-of-two-words.js)|Easy|
220+
1886|[Determine Whether Matrix Can Be Obtained By Rotation](./1886-determine-whether-matrix-can-be-obtained-by-rotation.js)|Easy|
220221
1920|[Build Array from Permutation](./1920-build-array-from-permutation.js)|Easy|
221222
1929|[Concatenation of Array](./1929-concatenation-of-array.js)|Easy|
222223
1935|[Maximum Number of Words You Can Type](./1935-maximum-number-of-words-you-can-type.js)|Easy|
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**
2+
* 1886. Determine Whether Matrix Can Be Obtained By Rotation
3+
* https://leetcode.com/problems/determine-whether-matrix-can-be-obtained-by-rotation/
4+
* Difficulty: Easy
5+
*
6+
* Given two n x n binary matrices mat and target, return true if it is possible to make mat
7+
* equal to target by rotating mat in 90-degree increments, or false otherwise.
8+
*/
9+
10+
/**
11+
* @param {number[][]} mat
12+
* @param {number[][]} target
13+
* @return {boolean}
14+
*/
15+
var findRotation = function(mat, target) {
16+
const goal = JSON.stringify(target);
17+
for (let i = 0; i < 4; i++) {
18+
if (JSON.stringify(rotate(mat)) === goal) {
19+
return true;
20+
}
21+
}
22+
return false;
23+
};
24+
25+
function rotate(matrix) {
26+
matrix = matrix.reverse();
27+
28+
for (let i = 0; i < matrix.length; i++) {
29+
for (let j = 0; j < i; j++) {
30+
[matrix[i][j], matrix[j][i]] = [matrix[j][i], matrix[i][j]];
31+
}
32+
}
33+
34+
return matrix;
35+
}

0 commit comments

Comments
 (0)
Please sign in to comment.