54: Spiral Matrix Matrix spiral
Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order.
Given a m X n- matrix elements ( m row, n- column), follow a helical clockwise order, returns all the elements in the matrix.
Example 1:
Input:
[
[ 1, 2, 3 ],
[ 4, 5, 6 ],
[ 7, 8, 9 ]
]
Output: [1,2,3,6,9,8,7,4,5]
Example 2:
Input:
[
[1, 2, 3, 4],
[5, 6, 7, 8],
[9,10,11,12]
]
Output: [1,2,3,4,8,12,11,10,9,5,6,7]
Problem-solving ideas:
Reference Example II was observed to change the way the index: (0,0) ---> (0,3), (0,3) ---> ((2,3) ---> (2,0) - -> (1,0) ---> (1,2)
From (0,3) See, are: 1 down abscissa incremented to 2; left: Save ordinate from 1 to 0; abscissa upwardly from the minus 1 to 1; the vertical coordinate from the right by 1 to 2
If m * n matrix, from (0, m-1) starts moving down to the n-1 times the lowest, then left m-1 times, n-2 times upward, rightward m-2 times, followed by: downward n-3, left m-3, up n-4, rightward m-4. M or n are each turned from minus 1.
This is my way of thinking, the online index coordinate many of which are directly manipulated, I think it is not well understood, because more than one spiral matrix, each time to change the reference coordinates, but the two methods is not very different in nature
java:
class Solution {
public List<Integer> spiralOrder(int[][] matrix) {
List nums=new ArrayList();
if (matrix.length==0||matrix[0].length==0)return nums ;
int row=matrix.length-1,col=matrix[0].length-1,m=0,n=0,i=-1,tmp=0;
while (row>=0&&col>=0){
switch (i++%4){
case 0:
for (tmp=0;tmp<row;tmp++)
nums.add(matrix[++m][n]);row-=1;
break;
case 1:
for (tmp=0;tmp<col;tmp++)
nums.add(matrix[m][--n]);col-=1;
break;
case 2:
for (tmp=0;tmp<row;tmp++)
nums.add(matrix[--m][n]);row-=1;
break;
case 3:
for (tmp=0;tmp<col;tmp++)
nums.add(matrix[m][++n]);col-=1;
break;
default:
for (tmp=0;tmp<=col;tmp++)
nums.add(matrix[m][n++]);tmp=0;n-=1;
break;
}
}
return nums;
}
}
important point:
First determine whether an empty array, sequence determination condition can not be reversed. Because if the matrix.length==0
determination is true, then the latter matrix[0].length==0
will not be determined, i.e., returns an empty array; however, matrix[0].length==0
when the front, if the input array is empty, matrix[0]
will be given because no matrix index 0.
python3:
class Solution:
def spiralOrder(self, matrix: List[List[int]]) -> List[int]:
if len(matrix)==0 or len(matrix[0])==0:return []
nums=[];m=0;n=0;row=len(matrix)-1;col=len(matrix[0])-1;flag=0;
for n in range(col+1):nums.append(matrix[m][n])
while row>=0 and col>=0:
if flag % 4 == 0:
for i in range(row):
m+=1
nums.append(matrix[m][n])
row -= 1
elif flag % 4==1:
for i in range(col):
n-=1
nums.append(matrix[m][n])
col -= 1
elif flag % 4 == 2:
for i in range(row):
m-=1
nums.append(matrix[m][n])
row -= 1
elif flag % 4 == 3:
for i in range(col):
n+=1
nums.append(matrix[m][n])
col -= 1
flag+=1
return nums
important point:
python does not switch ... case ... statement. cycle may for high operability, can operate directly traversal index coordinate change, it will not be repeated.