タイトル:
mxn要素(m行、n列)の行列が与えられた場合、行列内のすべての要素を時計回りのらせん順に返します。
アイデア:
2つのポインターを使用して境界を制御します。
手順:
クラスSolution:
def spiralOrder(self、matrix:List [List [int]])-> List [int]:
行= len(行列)
行<= 0の場合:
戻る[]
列= len(matrix [0])
結果= []
row_begin = 0
row_end = row-1
column_begin = 0
column_end = column-1
一方、row_begin <= row_endおよびcolumn_begin <= column_end:
範囲内のindex1の場合(column_begin、column_end + 1):
result.append(matrix [row_begin] [index1])
row_begin + = 1
範囲内のindex2の場合(row_begin、row_end + 1):
result.append(matrix [index2] [column_end])
column_end-= 1
範囲内のindex1の場合(column_end、column_begin-1、-1):
row_end> = row_beginの場合:
result.append(matrix [row_end] [index1])
row_end-= 1
範囲内のindex2の場合(row_end、row_begin-1、-1):
column_end> = column_beginの場合:
result.append(matrix [index2] [column_begin])
column_begin + = 1
結果を返す