package leetcode;

import java.util.ArrayList;
import java.util.List;

/**
 * https://leetcode.com/problems/spiral-matrix/
 */
public class Problem54 {
    public List<Integer> spiralOrder(int[][] matrix) {
        List<Integer> result = new ArrayList<>();
        if (matrix.length == 0) {
            return result;
        }
        Direction[] directions = new Direction[]{
            Direction.LEFT, Direction.DOWN, Direction.RIGHT, Direction.UP
        };
        int dirIdx = 0;
        int row = 0;
        int col = 0;
        int rowBegin = 0;
        int colBegin = 0;
        int rowEnd = matrix.length;
        int colEnd = matrix[row].length;
        do {
            if (dirIdx == directions.length) {
                dirIdx = 0;
            }
            switch (directions[dirIdx]) {
                case LEFT:
                    row = rowBegin;
                    col = colBegin;
                    while (col < colEnd) {
                        result.add(matrix[row][col]);
                        col++;
                    }
                    rowBegin++;
                    col--;
                    break;
                case DOWN:
                    row++;
                    while (row < rowEnd) {
                        result.add(matrix[row][col]);
                        row++;
                    }
                    colEnd--;
                    row--;
                    break;
                case RIGHT:
                    col--;
                    while (col >= colBegin) {
                        result.add(matrix[row][col]);
                        col--;
                    }
                    rowEnd--;
                    col++;
                    break;
                case UP:
                    row--;
                    while (row >= rowBegin) {
                        result.add(matrix[row][col]);
                        row--;
                    }
                    colBegin++;
                    break;
            }
            dirIdx++;
        } while (rowBegin != rowEnd && colBegin != colEnd);
        return result;
    }

    private static enum Direction {
        LEFT, RIGHT, UP, DOWN
    }
}

@@ -222,22 +222,22 @@
 nteger%3E 
-result
+answer
  = new A
@@ -263,69 +263,168 @@
    i
-f (matrix.length == 0) %7B%0A            return result;%0A        %7D
+nt left = 0;%0A        int right = matrix.length %3E 0 ? matrix%5B0%5D.length - 1 : 0;%0A        int up = 0;%0A        int down = matrix.length - 1;%0A        int dirIdx = 0;
 %0A   
@@ -492,19 +492,20 @@
 rection.
-LEF
+RIGH
 T, Direc
@@ -517,36 +517,35 @@
 DOWN, Direction.
-RIGH
+LEF
 T, Direction.UP%0A
@@ -567,297 +567,100 @@
     
-int dirIdx = 0;%0A        int row = 0;%0A        int col = 0;%0A        int rowBegin = 0;%0A        int colBegin = 0;%0A        int rowEnd = matrix.length;%0A        int colEnd = matrix%5Brow%5D.length;%0A        do %7B%0A            if (dirIdx == directions.length) %7B%0A                
+while (left %3C= right && up %3C= down) %7B%0A            Direction direction = directions%5B
 dirIdx
- = 0
+%5D
 ;%0A
-            %7D%0A
     
@@ -684,25 +684,16 @@
 irection
-s%5BdirIdx%5D
 ) %7B%0A    
@@ -709,19 +709,20 @@
    case 
-LEF
+RIGH
 T:%0A     
@@ -740,99 +740,48 @@
     
-row = rowBegin;%0A                    col = colBegin;%0A                    while (col %3C
+for (int col = left; col %3C= right;
  col
-End
+++
 ) %7B%0A
@@ -795,39 +795,38 @@
                 
- result
+answer
 .add(matrix%5Brow%5D
@@ -813,35 +813,34 @@
 swer.add(matrix%5B
-row
+up
 %5D%5Bcol%5D);%0A       
@@ -836,39 +836,8 @@
 %5D);%0A
-                        col++;%0A
     
@@ -878,47 +878,14 @@
     
-rowBegin
+up
 ++;%0A
-                    col--;%0A
     
@@ -958,54 +958,45 @@
     
-row++;%0A                    while (row %3C
+for (int row = up; row %3C= down;
  row
-End
+++
 ) %7B%0A
@@ -1011,38 +1011,38 @@
                 
-result
+answer
 .add(matrix%5Brow%5D
@@ -1034,35 +1034,37 @@
 add(matrix%5Brow%5D%5B
-col
+right
 %5D);%0A            
@@ -1075,94 +1075,35 @@
     
-    row++;%0A                    %7D%0A                    colEnd--;%0A                    row
+%7D%0A                    right
 --;%0A
@@ -1142,36 +1142,35 @@
            case 
-RIGH
+LEF
 T:%0A             
@@ -1180,57 +1180,48 @@
     
-col--;%0A                    while (col %3E= colBegin
+for (int col = right; col %3E= left; col--
 ) %7B%0A
@@ -1236,38 +1236,38 @@
                 
-result
+answer
 .add(matrix%5Brow%5D
@@ -1254,35 +1254,36 @@
 swer.add(matrix%5B
-r
+d
 ow
+n
 %5D%5Bcol%5D);%0A       
@@ -1279,39 +1279,8 @@
 %5D);%0A
-                        col--;%0A
     
@@ -1321,45 +1321,16 @@
     
-rowEnd
+down
 --;%0A
-                    col++;%0A
     
@@ -1401,57 +1401,45 @@
     
-row--;%0A                    while (row %3E= rowBegin
+for (int row = down; row %3E= up; row--
 ) %7B%0A
@@ -1462,22 +1462,22 @@
         
-result
+answer
 .add(mat
@@ -1489,46 +1489,16 @@
 ow%5D%5B
-col
+left
 %5D);%0A
-                        row--;%0A
     
@@ -1535,24 +1535,20 @@
         
-colBegin
+left
 ++;%0A    
@@ -1618,59 +1618,50 @@
     
-%7D while (rowBegin != rowEnd && colBegin != colEnd);
+    dirIdx %25= directions.length;%0A        %7D
 %0A   
@@ -1676,14 +1676,14 @@
 urn 
-result
+answer
 ;%0A  
@@ -1703,15 +1703,8 @@
 ate 
-static 
 enum
@@ -1727,22 +1727,16 @@
     
- LEFT,
  RIGHT, 
 UP, 
@@ -1735,16 +1735,22 @@
 HT, 
-UP, 
 DOWN
+, LEFT, UP
 %0A   

Update problem 54