-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy path0059-spiral-matrix-ii.rb
72 lines (56 loc) · 1.31 KB
/
0059-spiral-matrix-ii.rb
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# frozen_string_literal: true
# 59. Spiral Matrix II
# https://leetcode.com/problems/spiral-matrix-ii
# Medium
=begin
Given a positive integer n, generate an n x n matrix filled with elements from 1 to n2 in spiral order.
Example 1:
Input: n = 3
Output: [[1,2,3],[8,9,4],[7,6,5]]
Example 2:
Input: n = 1
Output: [[1]]
Constraints:
1 <= n <= 20
=end
# @param {Integer} n
# @return {Integer[][]}
def generate_matrix(n)
matrix = n.times.map { [nil] * n }
top_i, bottom_i = 0, n - 1
left_j, right_j = 0, n - 1
count = 1
while top_i <= bottom_i && left_j <= right_j
(left_j...right_j + 1).each do |j|
matrix[top_i][j] = count
count += 1
end
(top_i + 1...bottom_i + 1).each do |i|
matrix[i][right_j] = count
count += 1
end
(right_j - 1).downto(left_j) do |j|
matrix[bottom_i][j] = count
count += 1
end
(bottom_i - 1).downto(top_i + 1) do |i|
matrix[i][left_j] = count
count += 1
end
top_i += 1
bottom_i -= 1
left_j += 1
right_j -= 1
end
matrix
end
# **************** #
# TEST #
# **************** #
require "test/unit"
class Test_generate_matrix < Test::Unit::TestCase
def test_
assert_equal [[1, 2, 3], [8, 9, 4], [7, 6, 5]], generate_matrix(3)
assert_equal [[1]], generate_matrix(1)
end
end