Titulo:
Dada una matriz de elementos mxn (m filas, n columnas), devuelve todos los elementos en la matriz en un orden espiral en sentido horario.
Ideas:
Use dos punteros y controle el límite.
Procedimiento:
Solución de clase:
def spiralOrder (self, matrix: List [List [int]]) -> List [int]:
fila = len (matriz)
si fila <= 0:
regreso []
columna = len (matriz [0])
resultado = []
row_begin = 0
row_end = row - 1
column_begin = 0
column_end = column - 1
mientras row_begin <= row_end y column_begin <= column_end:
para index1 en rango (column_begin, column_end + 1):
result.append (matrix [row_begin] [index1])
row_begin + = 1
para index2 en rango (row_begin, row_end + 1):
resultado.append (matriz [index2] [column_end])
column_end - = 1
para index1 en rango (column_end, column_begin - 1, -1):
si row_end> = row_begin:
resultado.append (matriz [fila_end] [índice1])
row_end - = 1
para index2 en rango (row_end, row_begin - 1, -1):
si column_end> = column_begin:
resultado.append (matriz [index2] [column_begin])
column_begin + = 1
resultado devuelto