package com.fishercoder.solutions;

/**
 * 59. Spiral Matrix II
 *
 * Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order.

 For example,
 Given n = 3,

 You should return the following matrix:
 [
 [ 1, 2, 3 ],
 [ 8, 9, 4 ],
 [ 7, 6, 5 ]
 ]
 */
public class _59 {

  public static class Solution1 {
    /**credit: https://leetcode.com/problems/spiral-matrix-ii/discuss/22289/My-Super-Simple-Solution.-Can-be-used-for-both-Spiral-Matrix-I-and-II/21907*/
    public int[][] generateMatrix(int n) {
      int[][] matrix = new int[n][n];
      if (n == 0) {
        return matrix;
      }
      int value = 1;
      int top = 0;
      int bottom = n - 1;
      int left = 0;
      int right = n - 1;
      while (left <= right && top <= bottom) {
        for (int j = left; j <= right; j++) {
          matrix[top][j] = value++;
        }
        top++;
        for (int i = top; i <= bottom; i++) {
          matrix[i][right] = value++;
        }
        right--;
        for (int j = right; j >= left; j--) {
          matrix[bottom][j] = value++;
        }
        bottom--;
        for (int i = bottom; i >= top; i--) {
          matrix[i][left] = value++;
        }
        left++;
      }
      return matrix;
    }
  }
}

@@ -33,260 +33,8 @@
 s;%0A%0A
-/**%0A * 59. Spiral Matrix II%0A *%0A * Given an integer n, generate a square matrix filled with elements from 1 to n2 in spiral order.%0A%0A For example,%0A Given n = 3,%0A%0A You should return the following matrix:%0A %5B%0A %5B 1, 2, 3 %5D,%0A %5B 8, 9, 4 %5D,%0A %5B 7, 6, 5 %5D%0A %5D%0A */%0A
 publ
@@ -49,16 +49,18 @@
  _59 %7B%0A%0A
+  
   public
@@ -93,11 +93,27 @@
     
-/**
+    /**%0A         * 
 cred
@@ -256,15 +256,29 @@
 1907
+%0A         
 */%0A    
+  
+  
 publ
@@ -308,24 +308,30 @@
 ix(int n) %7B%0A
+      
       int%5B%5D%5B
@@ -356,16 +356,22 @@
 %5Bn%5D%5Bn%5D;%0A
+      
       if
@@ -386,24 +386,32 @@
 ) %7B%0A        
+  
+      
 return matri
@@ -419,24 +419,36 @@
 ;%0A      
-%7D%0A
+      %7D%0A   
       
+   
 int valu
@@ -456,24 +456,30 @@
  = 1;%0A      
+     
+ 
 int top = 0;
@@ -475,24 +475,30 @@
 nt top = 0;%0A
+      
       int bo
@@ -513,24 +513,30 @@
  - 1;%0A      
+ 
+     
 int left = 0
@@ -539,24 +539,30 @@
  = 0;%0A      
+   
+   
 int right = 
@@ -574,16 +574,22 @@
 ;%0A      
+    
+  
 while (l
@@ -621,32 +621,40 @@
 ttom) %7B%0A        
+      
+  
 for (int j = lef
@@ -681,24 +681,34 @@
 %7B%0A          
+      
+    
 matrix%5Btop%5D%5B
@@ -729,26 +729,37 @@
         
-%7D%0A
+        %7D%0A   
         
 top++;%0A 
@@ -754,15 +754,28 @@
     
+     
 top++;%0A
+        
     
@@ -818,32 +818,42 @@
 ++) %7B%0A          
+   
+       
 matrix%5Bi%5D%5Bright%5D
@@ -856,32 +856,40 @@
 ght%5D = value++;%0A
+        
         %7D%0A      
@@ -890,16 +890,24 @@
         
+ 
+       
 right--;
@@ -903,24 +903,32 @@
    right--;%0A
+        
         for 
@@ -967,24 +967,34 @@
 %7B%0A          
+ 
+         
 matrix%5Bbotto
@@ -1010,34 +1010,50 @@
 alue++;%0A        
+      
-%7D%0A
+  %7D%0A        
         bottom--
@@ -1054,32 +1054,40 @@
 ttom--;%0A        
+      
+  
 for (int i = bot
@@ -1104,24 +1104,34 @@
 top; i--) %7B%0A
+          
           ma
@@ -1163,26 +1163,37 @@
         
+   
-%7D%0A
+     %7D%0A   
         
 left++;%0A
@@ -1184,16 +1184,21 @@
         
+     
 left++;%0A
@@ -1199,26 +1199,38 @@
 ft++;%0A      
+   
-%7D%0A
+   %7D%0A      
       return
@@ -1242,16 +1242,22 @@
 ix;%0A    
-%7D%0A
+    %7D%0A  
   %7D%0A%7D%0A

refactor 59