Skip to content

Commit d83b021

Browse files
authored
Merge pull request #675 from fartem/54_Spiral_Matrix
2024-07-17 v. 6.1.8: added "54. Spiral Matrix"
2 parents 0eb8f35 + 7355ce0 commit d83b021

File tree

4 files changed

+64
-1
lines changed

4 files changed

+64
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -483,3 +483,4 @@ Profile on LeetCode: [fartem](https://leetcode.com/fartem/).
483483
| 48. Rotate Image | [Link](https://leetcode.com/problems/rotate-image/) | [Link](./lib/medium/48_rotate_image.rb) |
484484
| 49. Group Anagrams | [Link](https://leetcode.com/problems/group-anagrams/) | [Link](./lib/medium/49_group_anagrams.rb) |
485485
| 53. Maximum Subarray | [Link](https://leetcode.com/problems/maximum-subarray/) | [Link](./lib/medium/53_maximum_subarray.rb) |
486+
| 54. Spiral Matrix | [Link](https://leetcode.com/problems/spiral-matrix/) | [Link](./lib/medium/54_spiral_matrix.rb) |

leetcode-ruby.gemspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ require 'English'
55
::Gem::Specification.new do |s|
66
s.required_ruby_version = '>= 3.0'
77
s.name = 'leetcode-ruby'
8-
s.version = '6.1.7'
8+
s.version = '6.1.8'
99
s.license = 'MIT'
1010
s.files = ::Dir['lib/**/*.rb'] + %w[bin/leetcode-ruby README.md LICENSE]
1111
s.executable = 'leetcode-ruby'

lib/medium/54_spiral_matrix.rb

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# frozen_string_literal: true
2+
3+
# https://leetcode.com/problems/spiral-matrix/
4+
# @param {Integer[][]} matrix
5+
# @return {Integer[]}
6+
def spiral_order(matrix)
7+
top = 0
8+
bottom = matrix.length - 1
9+
left = 0
10+
right = matrix[0].length - 1
11+
result = []
12+
13+
while top <= bottom && left <= right
14+
(left..right).each { |i| result << matrix[top][i] }
15+
top += 1
16+
17+
(top..bottom).each { |i| result << matrix[i][right] }
18+
right -= 1
19+
20+
if top <= bottom
21+
(left..right).reverse_each { |i| result << matrix[bottom][i] }
22+
bottom -= 1
23+
end
24+
25+
if left <= right
26+
(top..bottom).reverse_each { |i| result << matrix[i][left] }
27+
left += 1
28+
end
29+
end
30+
31+
result
32+
end

test/medium/test_54_spiral_matrix.rb

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# frozen_string_literal: true
2+
3+
require_relative '../test_helper'
4+
require_relative '../../lib/medium/54_spiral_matrix'
5+
require 'minitest/autorun'
6+
7+
class SpiralMatrixTest < ::Minitest::Test
8+
def test_default
9+
assert_equal(
10+
[1, 2, 3, 6, 9, 8, 7, 4, 5],
11+
spiral_order(
12+
[
13+
[1, 2, 3],
14+
[4, 5, 6],
15+
[7, 8, 9]
16+
]
17+
)
18+
)
19+
assert_equal(
20+
[1, 2, 3, 4, 8, 12, 11, 10, 9, 5, 6, 7],
21+
spiral_order(
22+
[
23+
[1, 2, 3, 4],
24+
[5, 6, 7, 8],
25+
[9, 10, 11, 12]
26+
]
27+
)
28+
)
29+
end
30+
end

0 commit comments

Comments
 (0)