Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class SnailSolution
- {
- public static int[] Snail(int[][] array)
- {
- int left = 0;
- int top = 0;
- int right = array[0].Length;
- int bottom = array.Length;
- int row = 0;
- int col = 0;
- var list = new System.Collections.Generic.List<int>();
- int size = bottom * right;
- if (size == 0)
- return System.Array.Empty<int>();
- while (list.Count < size - 1)
- {
- // go right
- while (col < right - 1)
- {
- list.Add(array[row][col++]);
- }
- ++top;
- // go down
- while (row < bottom - 1)
- {
- list.Add(array[row++][col]);
- }
- --right;
- // go left
- while (col > left)
- {
- list.Add(array[row][col--]);
- }
- --bottom;
- // go up
- while (row > top)
- {
- list.Add(array[row--][col]);
- }
- ++left;
- }
- list.Add(array[row][col]);
- return list.ToArray();
- }
- }
Add Comment
Please, Sign In to add comment