java solution


  • 0
    P
    public class Solution {
        List<Integer> list = new ArrayList<Integer>();
        public List<Integer> spiralOrder(int[][] matrix) {
            if(matrix.length == 0)
            {
                return list;
            }
            int left = matrix[0].length-1;
            int right = 0;
            int bottom = matrix.length-1;
            int top = 0;
            while(true)
            {
                for(int j = right;j<=left;j++)
                {
                    list.add(matrix[top][j]);
                }
                top++;
                //Can be deleted
    /*          if(bottom<top||left<right)
                {
                    break;
                }
    */
                for(int i = top;i<=bottom;i++)
                {
                    list.add(matrix[i][left]);
                }
                left--;
                if(bottom<top||left<right)
                {
                    break;
                }
                for(int j = left;j>=right;j--)
                {
                    list.add(matrix[bottom][j]);
                }
                bottom--;
                //Can be deleted
    /*          if(bottom<top||left<right)
                {
                    break;
                }
    */
                for(int i = bottom;i>=top;i--)
                {
                    list.add(matrix[i][right]);
                }
                right++;
                if(bottom<top||left<right)
                {
                    break;
                }
            }
            return list;
        }
    }
    

Log in to reply
 

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