[LeetCode][Python3] 48. Rotate Image
2018. 11. 13. 01:40 |
프로그래밍/LeetCode
Problem :
https://leetcode.com/problems/rotate-image
My Solution :
class Solution:
def rotate(self, matrix):
"""
:type matrix: List[List[int]]
:rtype: void Do not return anything, modify matrix in-place instead.
"""
move = len(matrix)-1
for i in range(len(matrix)//2):
for j in range(i, move-i):
temp = matrix[i][j]
matrix[i][j] = matrix[move-j][i]
matrix[move-j][i] = matrix[move-i][move-j]
matrix[move-i][move-j] = matrix[j][move-i]
matrix[j][move-i] = temp
Comment :
나는 이렇게 수동으로 하나씩 회전을 시켰는데, reverse & swap 방식으로 쉽게 풀 수 있다고 한다. Python에서 행과 열을 쉽게 swap 해주는게 바로 zip이다.
Example :
class Solution:
def rotate(self, matrix):
"""
:type matrix: List[List[int]]
:rtype: void Do not return anything, modify matrix in-place instead.
"""
matrix[:] = map(list, zip(*matrix[::-1]))
'프로그래밍 > LeetCode' 카테고리의 다른 글
[LeetCode][Python3] 94. Binary Tree Inorder Traversal (1) | 2019.01.03 |
---|---|
[LeetCode][Python3] 62. Unique Paths (6) | 2018.11.15 |
[LeetCode][Python3] 55. Jump Game (0) | 2018.11.15 |
[LeetCode][Python3] 49. Group Anagrams (0) | 2018.11.14 |
[LeetCode][Python3] 46. Permutations (0) | 2018.11.12 |
[LeetCode][Python3] 36. Valid Sudoku (0) | 2018.11.11 |
[LeetCode][Python3] 34. Find First and Last Position of Element in Sorted Array (0) | 2018.11.11 |
[LeetCode][Python3] 22. Generate Parentheses (0) | 2018.11.10 |
최근에 달린 댓글 최근에 달린 댓글