-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path054.rb
More file actions
40 lines (34 loc) · 873 Bytes
/
054.rb
File metadata and controls
40 lines (34 loc) · 873 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# @param {Integer[][]} matrix
# @return {Integer[]}
def spiral_order(matrix)
return [] if matrix.length == 0
return [] if matrix[0].length == 0
min_col = 0
max_col = matrix[0].length - 1
min_row = 0
max_row = matrix.length - 1
result = []
while true do
return result if min_col > max_col
min_col.upto(max_col).each do |col|
result.push(matrix[min_row][col])
end
min_row += 1
return result if min_row > max_row
min_row.upto(max_row).each do |row|
result.push(matrix[row][max_col])
end
max_col -= 1
return result if min_col > max_col
max_col.downto(min_col).each do |col|
result.push(matrix[max_row][col])
end
max_row -= 1
return result if min_row > max_row
max_row.downto(min_row).each do |row|
result.push(matrix[row][min_col])
end
min_col += 1
end
result
end