Skip to content

Commit 37313a1

Browse files
committed
feat(leetcode): add No.2946
1 parent 2281a67 commit 37313a1

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// https://leetcode.com/problems/matrix-similarity-after-cyclic-shifts/description/
2+
// algorithms
3+
// Easy (56.67%)
4+
// Total Accepted: 17K
5+
// Total Submissions: 30K
6+
7+
8+
class Solution {
9+
10+
public boolean areSimilar(int[][] mat, int k) {
11+
int m = mat.length;
12+
13+
if (m == 0) {
14+
return false;
15+
}
16+
17+
int n = mat[0].length;
18+
k %= n;
19+
20+
for (int i = 0; i < m; i++) {
21+
// 偶数往左,奇数往右
22+
int direction = i & 1;
23+
for (int j = 0; j < n; j++) {
24+
int targetIdx;
25+
if (direction == 0) {
26+
targetIdx = j - k;
27+
if (targetIdx < 0) {
28+
targetIdx += n;
29+
}
30+
} else {
31+
targetIdx = (j + k) % n;
32+
}
33+
if (mat[i][j] != mat[i][targetIdx]) {
34+
return false;
35+
}
36+
}
37+
}
38+
39+
return true;
40+
}
41+
42+
}

0 commit comments

Comments
 (0)