亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Python3實(shí)現(xiàn)的旋轉(zhuǎn)矩陣圖像算法示例

 更新時(shí)間:2019年04月03日 08:47:49   作者:zhenghaitian  
這篇文章主要介紹了Python3實(shí)現(xiàn)的旋轉(zhuǎn)矩陣圖像算法,涉及Python3列表遍歷、切片轉(zhuǎn)換、矩陣運(yùn)算等相關(guān)操作技巧,需要的朋友可以參考下

本文實(shí)例講述了Python3實(shí)現(xiàn)的旋轉(zhuǎn)矩陣圖像算法。分享給大家供大家參考,具體如下:

問(wèn)題:

給定一個(gè) n × n 的二維矩陣表示一個(gè)圖像。

將圖像順時(shí)針旋轉(zhuǎn) 90 度。

方案一:先按X軸對(duì)稱旋轉(zhuǎn), 再用zip()解壓,最后用list重組。

# -*- coding:utf-8 -*-
#! python3
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]))
    return matrix
if __name__ == '__main__':
  # 測(cè)試代碼
  matrix = [
    [1,2,3,4],
    [5,6,7,8],
    [9,10,11,12],
    [13,14,15,16]
  ]
  solution = Solution()
  result = solution.rotate(matrix)
  print(result)

運(yùn)行結(jié)果:

[[13, 9, 5, 1], [14, 10, 6, 2], [15, 11, 7, 3], [16, 12, 8, 4]]

方案二:找到規(guī)律,用原矩陣數(shù)據(jù) 賦值

# -*- coding:utf-8 -*-
#! python3
class Solution:
  def rotate(self, matrix):
    """
    :type matrix: List[List[int]]
    :rtype: void Do not return anything, modify matrix in-place instead.
    """
    m = matrix.copy()
    n = len(matrix)
    for i in range(n):
      matrix[i] = [m[j][i] for j in range(n - 1, -1, -1)]
    return
if __name__ == '__main__':
  # 測(cè)試代碼
  matrix = [
    [1,2,3,4],
    [5,6,7,8],
    [9,10,11,12],
    [13,14,15,16]
  ]
  solution = Solution()
  result = solution.rotate(matrix)
  print(result)

運(yùn)行結(jié)果:

[[13, 9, 5, 1], [14, 10, 6, 2], [15, 11, 7, 3], [16, 12, 8, 4]]

更多關(guān)于Python相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Python數(shù)學(xué)運(yùn)算技巧總結(jié)》、《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》及《Python入門與進(jìn)階經(jīng)典教程

希望本文所述對(duì)大家Python程序設(shè)計(jì)有所幫助。

相關(guān)文章

最新評(píng)論