Skip to content

Commit 0d73ea7

Browse files
committed
[이진희]3643. Flip Square Submatrix Vertically
1 parent 4b654c9 commit 0d73ea7

1 file changed

Lines changed: 33 additions & 0 deletions

File tree

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
3+
1. 아이디어 : (x,y)를 왼쪽 위 좌표로 하는 k*k영역의 정사각형의 원소를 뒤집는다.
4+
swap메소드를 구현하면 된다.
5+
6+
2. 시간복잡도 : O(k*k)
7+
8+
3. 자료구조/알고리즘 : 구현
9+
10+
*/
11+
12+
class Solution {
13+
public int[][] reverseSubmatrix(int[][] grid, int x, int y, int k) {
14+
// 뒤집기
15+
16+
for(int i=0; i<k/2; i++) {
17+
int top = x+i;
18+
int bottom = x+k-i-1;
19+
20+
for(int j=y; j<y+k; j++) {
21+
swap(grid, j, top, bottom);
22+
}
23+
}
24+
25+
return grid;
26+
}
27+
28+
private void swap(int[][] grid, int j, int top, int bottom) {
29+
int tmp = grid[top][j];
30+
grid[top][j] = grid[bottom][j];
31+
grid[bottom][j] = tmp;
32+
}
33+
}

0 commit comments

Comments
 (0)