Advertisement
Guest User

Untitled

a guest
Jan 9th, 2021
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.49 KB | None | 0 0
  1. class Solution {
  2.     private List<Integer> answer = new LinkedList();
  3.     public List<Integer> spiralOrder(int[][] matrix) {
  4.         
  5.         int up = 0;
  6.         int down = matrix.length - 1;
  7.         int right = matrix[0].length -1;
  8.         int left = 0;
  9.         
  10.         int entry = 1;
  11.         
  12.         while(entry <= matrix[0].length * matrix.length)
  13.         {
  14.             //GO RIGHT
  15.             for(int i = up; i <= right; i++)
  16.             {
  17.                 if(entry > matrix[0].length * matrix.length)
  18.                 {
  19.                     break;
  20.                 }
  21.           
  22.                 
  23.                 
  24.                 answer.add(matrix[up][i]);
  25.                 entry++;
  26.             }
  27.  
  28.             up++;
  29.             //GO DOWN
  30.             for(int i = up; i <= down; i++)
  31.             {
  32.                 if(entry > matrix[0].length * matrix.length)
  33.                 {
  34.                     break;
  35.                 }
  36.                 
  37.                 answer.add(matrix[i][right]);
  38.                 entry++;
  39.             }
  40.       
  41.             right--;
  42.             
  43.             //GO LEFT
  44.             for(int i = right; i >= left; i--)
  45.             {
  46.                 if(entry > matrix[0].length * matrix.length)
  47.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement