File tree Expand file tree Collapse file tree 1 file changed +42
-0
lines changed
Expand file tree Collapse file tree 1 file changed +42
-0
lines changed Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments