-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add post '(Leetcode) 48 - Rotate Image 풀이'
- Loading branch information
1 parent
1873814
commit 1a3b442
Showing
3 changed files
with
46 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
--- | ||
layout: post | ||
title: (Leetcode) 48 - Rotate Image 풀이 | ||
categories: [스터디-알고리즘] | ||
tags: [자바, java, 리트코드, Leetcode, 알고리즘, array, matrix] | ||
date: 2024-07-22 16:30:00 +0900 | ||
toc: true | ||
--- | ||
|
||
기회가 되어 [달레님의 스터디](https://github.com/DaleStudy/leetcode-study)에 참여하여 시간이 될 때마다 한문제씩 풀어보고 있다. | ||
|
||
[https://neetcode.io/practice](https://neetcode.io/practice) | ||
|
||
--- | ||
|
||
[https://leetcode.com/problems/rotate-image/description/](https://leetcode.com/problems/rotate-image/description/) | ||
|
||
## 내가 작성한 풀이 | ||
|
||
rotate 가 진행될 때 어떤 아이템이 어디로 이동되는지 확인하고 그것을 일반화해서 해결하였다. | ||
실제 배열과 수학 행렬을 매칭시켜주는게 다소 헷갈리기 때문에 변수와 위치 정의를 잘 해두면 좋을 것 같다. | ||
|
||
```java | ||
class Solution { | ||
public void rotate(int[][] matrix) { | ||
int l = matrix.length; | ||
|
||
int limit = (int) Math.ceil((double) l / 2); | ||
for (int i = 0; i < limit; i++) { | ||
for (int j = i; j < l - 1 - i; j++) { | ||
int temp = matrix[i][j]; | ||
matrix[i][j] = matrix[l - j - 1][i]; | ||
matrix[l - j - 1][i] = matrix[l - i - 1][l - j - 1]; | ||
matrix[l - i - 1][l - j - 1] = matrix[j][l - i - 1]; | ||
matrix[j][l - i - 1] = temp; | ||
} | ||
} | ||
} | ||
} | ||
``` | ||
|
||
### TC, SC | ||
|
||
시간 복잡도는 `O(n^2)` 공간 복잡도는 `O(1)` 이다. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters