kiraventom

Untitled

Feb 4th, 2022 (edited)
229
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.85 KB | None | 0 0
  1. public class SnailSolution
  2. {
  3.     public static int[] Snail(int[][] array)
  4.     {
  5.         int left = 0;
  6.         int top = 0;
  7.         int right = array[0].Length;
  8.         int bottom = array.Length;
  9.         int row = 0;
  10.         int col = 0;
  11.         var list = new System.Collections.Generic.List<int>();
  12.  
  13.         int size = bottom * right;
  14.         if (size == 0)
  15.             return System.Array.Empty<int>();
  16.  
  17.         while (list.Count < size - 1)
  18.         {
  19.  
  20.             // go right
  21.             while (col < right - 1)
  22.             {
  23.                 list.Add(array[row][col++]);
  24.             }
  25.  
  26.             ++top;
  27.  
  28.             // go down
  29.             while (row < bottom - 1)
  30.             {
  31.                 list.Add(array[row++][col]);
  32.             }
  33.  
  34.             --right;
  35.  
  36.             // go left
  37.             while (col > left)
  38.             {
  39.                 list.Add(array[row][col--]);
  40.             }
  41.  
  42.             --bottom;
  43.  
  44.             // go up
  45.             while (row > top)
  46.             {
  47.                 list.Add(array[row--][col]);
  48.             }
  49.  
  50.             ++left;
  51.         }
  52.  
  53.         list.Add(array[row][col]);
  54.  
  55.         return list.ToArray();
  56.     }
  57. }
Add Comment
Please, Sign In to add comment