Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commita6fdcdc

Browse files
authored
Merge pull requestneetcode-gh#1991 from saip7795/sp/spiral-matrix
Create: 0054-Spiral-Matrix.rb
2 parentsd346853 +a11b52c commita6fdcdc

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

‎ruby/0054-spiral-matrix.rb

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
defspiral_order(matrix)
2+
result=[]
3+
left=0
4+
right=matrix[0].length
5+
top=0
6+
bottom=matrix.length
7+
8+
whileleft <right &&top <bottom
9+
10+
(left..right-1).eachdo |i|
11+
result.append(matrix[top][i])
12+
end
13+
14+
top +=1
15+
16+
(top..bottom-1).eachdo |i|
17+
result.append(matrix[i][right-1])
18+
end
19+
20+
right -=1
21+
22+
breakunless(left <right &&top <bottom)
23+
24+
(right-1).downto(left).eachdo |i|
25+
result.append(matrix[bottom-1][i])
26+
end
27+
28+
bottom -=1
29+
30+
(bottom-1).downto(top).eachdo |i|
31+
result.append(matrix[i][left])
32+
end
33+
34+
left +=1
35+
36+
end
37+
38+
returnresult
39+
end

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp