We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
2 parents d346853 + a11b52c commit a6fdcdcCopy full SHA for a6fdcdc
ruby/0054-spiral-matrix.rb
@@ -0,0 +1,39 @@
1
+def spiral_order(matrix)
2
+ result = []
3
+ left = 0
4
+ right = matrix[0].length
5
+ top = 0
6
+ bottom = matrix.length
7
+
8
+ while left < right && top < bottom
9
10
+ (left..right-1).each do |i|
11
+ result.append(matrix[top][i])
12
+ end
13
14
+ top +=1
15
16
+ (top..bottom-1).each do |i|
17
+ result.append(matrix[i][right-1])
18
19
20
+ right -=1
21
22
+ break unless (left < right && top < bottom)
23
24
+ (right-1).downto(left).each do |i|
25
+ result.append(matrix[bottom-1][i])
26
27
28
+ bottom -=1
29
30
+ (bottom-1).downto(top).each do |i|
31
+ result.append(matrix[i][left])
32
33
34
+ left +=1
35
36
37
38
+ return result
39
+end
0 commit comments