my solution


  • 0
    J

    class Solution {
    public List<Integer> spiralOrder(int[][] matrix) {

        List<Integer> result = new ArrayList<>();
        if (matrix.length == 0) { return result; }
        int count = matrix.length * matrix[0].length,bound = 0,i=-1,j=0;
        while(count>0)
        {
            while(i+1<matrix[0].length-bound&&count>0) { i++;result.add(matrix[j][i]);count--; }
            while(j+1<matrix.length-bound&&count>0) { j++;result.add(matrix[j][i]);count--; }
            while(i-1>=bound&&count>0) { i--;result.add(matrix[j][i]);count--; }
            while(j-1>bound&&count>0) { j--;result.add(matrix[j][i]);count--; }
            bound++;
        }
        return result;
    }
    

    }


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.