LC-54 Spiral Matrix
November 19, 2019•116 words
Question: Given a matrix and display its elements in spiral order
Solution: every time loop a circle. First we need to make sure the vertex position of circle corner. Then start looping.
class Solution {
public List spiralOrder(int[][] matrix) {
List res = new ArrayList<>();
if(matrix == null || matrix.length==0) return res;
int r1 = 0, r2 = matrix.length - 1;
int c1 = 0, c2 = matrix[0].length - 1;
while(r1<=r2 && c1<=c2){
...
Read post