日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python顺时针螺旋顺序

發布時間:2025/3/21 python 54 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python顺时针螺旋顺序 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

給你一個 m 行 n 列的矩陣 matrix ,請按照 順時針螺旋順序 ,返回矩陣中的所有元素。

輸入:matrix = [[1,2,3],[4,5,6],[7,8,9]]
輸出:[1,2,3,6,9,8,7,4,5]

輸入:matrix = [[1,2,3,4],[5,6,7,8],[9,10,11,12]]
輸出:[1,2,3,4,8,12,11,10,9,5,6,7]

class Solution:def spiralOrder(self, matrix: List[List[int]]) -> List[int]:columns, rows = len(matrix), len(matrix[0]) # 計算矩陣的長度很寬度# top, bottom, left, right 分別表示矩陣的列的最小索引值和列的最大索引值,行的最小索引值和行的最大索引值的top, bottom, left, right = 0, columns-1, 0, rows - 1result_list = list()while right >= left and bottom >= top:for i in range(top, right+1):result_list.append(matrix[top][i]) # 按順時針把頂層元素添加到list中for j in range(top+1, bottom+1):result_list.append(matrix[j][right]) # 按順時針把最右邊的元素添加到list中if right > left and bottom > left:for i in range(right-1, left-1, -1):result_list.append(matrix[bottom][i]) # 按順時針把底層元素添加到list中for j in range(bottom-1, top, -1):result_list.append(matrix[j][left]) # 按順時針把最左側的元素添加到list中# 當外層遍歷結束 分別對四邊的做相應的調整top, bottom, left, right = top + 1, bottom - 1, left + 1, right - 1return result_list

總結

以上是生活随笔為你收集整理的python顺时针螺旋顺序的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。