@@ -0,0 +1,1090 @@
+// Solution runs in O(n * m) time and O(n) space for output result (O(min(n, m)) recursion)%0A// Main gotcha is to handle matrices/states that are row or column vectors - we just add those%0A// separately.%0Aclass Solution %7B%0A  public List%3CInteger%3E spiralOrder(int%5B%5D%5B%5D matrix) %7B%0A    List%3CInteger%3E res = new ArrayList%3CInteger%3E();%0A    if (matrix.length == 0 %7C%7C matrix%5B0%5D.length == 0) return res;%0A    spiral(matrix, 0, matrix.length - 1, 0, matrix%5B0%5D.length - 1, res);%0A    return res;%0A  %7D%0A%0A  private void spiral(int%5B%5D%5B%5D matrix, int rs, int re, int cs, int ce, List%3CInteger%3E res) %7B%0A    if (rs %3E re %7C%7C cs %3E ce) return;%0A    if (cs == ce) %7B%0A      for (int i = rs; i %3C= re; i++) res.add(matrix%5Bi%5D%5Bcs%5D);%0A    %7D else if (rs == re) %7B%0A      for (int j = cs; j %3C= ce; j++) res.add(matrix%5Brs%5D%5Bj%5D);%0A    %7D else %7B%0A      for (int j = cs; j %3C ce; j++) res.add(matrix%5Brs%5D%5Bj%5D);%0A      for (int i = rs; i %3C re; i++) res.add(matrix%5Bi%5D%5Bce%5D);%0A      for (int j = ce; j %3E cs; j--) res.add(matrix%5Bre%5D%5Bj%5D);%0A      for (int i = re; i %3E rs; i--) res.add(matrix%5Bi%5D%5Bcs%5D);%0A    %7D%0A    spiral(matrix, rs+1, re-1, cs+1, ce-1, res);%0A  %7D%0A%7D%0A

add solution