48. Rotate Image
第一種辦法:每次旋轉4個值
class Solution(object):
def rotate(self, matrix):
"""
:type matrix: List[List[int]]
:rtype: void Do not return anything, modify matrix in-place instead.
"""
n = len(matrix[0])
i = 0
j = 0
while i < n / 2:
j = i
while j < n - 1 - i:
temp = matrix[i][j]
matrix[i][j] = matrix[n-1-j][i]
matrix[n-1-j][i] = matrix[n-1-i][n-1-j]
matrix[n-1-i][n-1-j] = matrix[j][n-1-i]
matrix[j][n-1-i]= temp
j += 1
i += 1
第二種做法:
先沿著n-1對稱軸進行對稱變換,然後上下兩半進行對稱反轉
class Solution(object):
def rotate(self, matrix):
"""
:type matrix: List[List[int]]
:rtype: void Do not return anything, modify matrix in-place instead.
"""
n = len(matrix)
for i in range(n-1):
for j in range(n-1-i):
matrix[i][j],matrix[n-1-j][n-1-i] = matrix[n-1-j][n-1-i],matrix[i][j]
for i in range(n/2):
for j in range(n):
matrix[i][j], matrix[n-1-i][j] = matrix[n-1-i][j],matrix[i][j]
第三種做法
先轉置矩陣,然後左右對稱一下
class Solution(object):
def rotate(self, matrix):
"""
:type matrix: List[List[int]]
:rtype: void Do not return anything, modify matrix in-place instead.
"""
print matrix
n = len(matrix[0])
matrix = map(list, zip(*matrix))
print matrix
for j in range(n/2):
for i in range(n):
matrix[i][j],matrix[i][n-1-j] = matrix[i][n-1-j],matrix[i][j]
print matrix
s = Solution()
s.rotate([[1,2,3],[4,5,6],[7,8,9]])
這種辦法我在本地上可以做,但是在leetcode上就不行了,不知道為啥
相關文章
- canvas rotate()Canvas
- LeetCode 48. 旋轉影像LeetCode
- [LeetCode] Rotate StringLeetCode
- Rotate Array@LeetCodeLeetCode
- CSS3 rotate()CSSS3
- Leetcode 61. Rotate ListLeetCode
- [LeetCode] 61. Rotate ListLeetCode
- WPF Image Image clip EllipseGeometry
- Image
- WPF Image add watermark and save marked image as jpg
- codeforces 1209E1 Rotate Columns (easy version)
- CSS rotate3d(x,y,z,angle)CSS3D
- Image Manipulation
- docker imageDocker
- PIL Image
- 劍指 Offer 48. 最長不含重複字元的子字串字元字串
- 帶你理解transform:rotate(含三維檢視)ORM
- canvas translate()、scale()和rotate()方法程式碼例項Canvas
- WPF Image automatically display image via System.Timer.Timer
- Medical Image Reader
- Image onload 事件事件
- Flutter元件ImageFlutter元件
- SVG <image>元素SVG
- image003
- image001
- image005
- image002
- image004
- image010
- image006
- leetcode 劍指 Offer 48. 最長不含重複字元的子字串LeetCode字元字串
- docker建立volume 指定volume 匯出image 匯入imageDocker
- 題解:CF1209E1 Rotate Columns (easy version)
- 圖解:什麼是旋轉陣列(Rotate Array)?圖解陣列
- 常用的畫素操作演算法:Resize、Flip、Rotate演算法
- CSS3 rotate3d(x,y,z,angle)CSSS33D
- Image server for Incus and LXCServer
- WPF CheckBox ToolTip Image