-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path059.rb
More file actions
42 lines (37 loc) · 859 Bytes
/
059.rb
File metadata and controls
42 lines (37 loc) · 859 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
41
42
# @param {Integer} n
# @return {Integer[][]}
def generate_matrix(n)
matrix = []
n.times { matrix.push(Array.new(n, 0)) }
min_col = 0
max_col = n - 1
min_row = 0
max_row = n - 1
value = 1
while true do
return matrix if min_col > max_col
min_col.upto(max_col).each do |col|
matrix[min_row][col] = value
value += 1
end
min_row += 1
return matrix if min_row > max_row
min_row.upto(max_row).each do |row|
matrix[row][max_col] = value
value += 1
end
max_col -= 1
return matrix if min_col > max_col
max_col.downto(min_col).each do |col|
matrix[max_row][col] = value
value += 1
end
max_row -= 1
return matrix if min_row > max_row
max_row.downto(min_row).each do |row|
matrix[row][min_col] = value
value += 1
end
min_col += 1
end
end