https://leetcode.com/problems/rotate-image/submissions/
Rotate Image - LeetCode
Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.
leetcode.com
각 row를 거꾸로 정렬한 뒤 matrix를 왼쪽 위에서 오른쪽 아래 대각선으로 잘랐다고 생각하고 그 대각선을 기준으로 각 칸을 swap 해준다.
<Python>
class Solution:
def rotate(self, matrix: List[List[int]]) -> None:
matrix.reverse()
for i in range(len(matrix)):
for j in range(i):
matrix[i][j], matrix[j][i] = matrix[j][i], matrix[i][j]
<JavaScript>
var rotate = function(matrix) {
matrix.reverse();
for(let i=0; i<matrix.length; i++){
for(let j=0; j<=i; j++){
let temp = matrix[i][j];
matrix[i][j] = matrix[j][i];
matrix[j][i] = temp;
}
}
};
'알고리즘' 카테고리의 다른 글
[LeetCode] 114. Flatten Binary Tree to Linked List 2가지 방법으로 풀기(Recursion, Morris Traversal) (0) | 2022.04.06 |
---|---|
[LeetCode] quick sort 알고리즘: 215. Kth Largest Element in an Array (0) | 2022.04.05 |
[LeetCode] BFS: 127. Word Ladder (Python, JavaScript) (0) | 2022.04.04 |
[LeetCode] 581. Shortest Unsorted Continuous Subarray (0) | 2022.04.03 |
[리트코드] 238. Product of Array Except Self (0) | 2022.04.03 |