# Elegant and fast Java solution (240ms)

• ``````    public List<Integer> spiralOrder(int[][] matrix) {
List<Integer> spiralList = new ArrayList<>();
if(matrix == null || matrix.length == 0) return spiralList;

// declare indices
int top = 0;
int bottom = matrix.length - 1;
int left = 0;
int right = matrix[0].length - 1;

while(true){
// 1. print top row
for(int j=left; j <=right;j++){
}
top++;
if(boundriesCrossed(left,right,bottom,top))
break;

// 2. print rightmost column
for(int i=top; i <= bottom; i++){
}
right--;
if(boundriesCrossed(left,right,bottom,top))
break;

// 3. print bottom row
for(int j=right; j >=left; j--){
}
bottom--;
if(boundriesCrossed(left,right,bottom,top))
break;

// 4. print leftmost column
for(int i=bottom; i >= top; i--){
}
left++;
if(boundriesCrossed(left,right,bottom,top))
break;
}// end while true

return spiralList;
}

private boolean boundriesCrossed(int left,int right,int bottom,int top){
if(left>right || bottom<top)
return true;
else
return false;
}``````

• Thanks for sharing, I think you could just write like following without adding the helper function.

``if(left>right || bottom<top) break;``

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.