Skip to content

2024-07-19 v. 6.2.2: added "59. Spiral Matrix II" #679

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -487,3 +487,4 @@ Profile on LeetCode: [fartem](https://leetcode.com/fartem/).
| 55. Jump Game | [Link](https://leetcode.com/problems/jump-game/) | [Link](./lib/medium/55_jump_game.rb) |
| 56. Merge Intervals | [Link](https://leetcode.com/problems/merge-intervals/) | [Link](./lib/medium/56_merge_intervals.rb) |
| 57. Insert Interval | [Link](https://leetcode.com/problems/insert-interval/) | [Link](./lib/medium/57_insert_interval.rb) |
| 59. Spiral Matrix II | [Link](https://leetcode.com/problems/spiral-matrix-ii/) | [Link](./lib/medium/59_spiral_matrix_ii.rb) |
2 changes: 1 addition & 1 deletion leetcode-ruby.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ require 'English'
::Gem::Specification.new do |s|
s.required_ruby_version = '>= 3.0'
s.name = 'leetcode-ruby'
s.version = '6.2.1'
s.version = '6.2.2'
s.license = 'MIT'
s.files = ::Dir['lib/**/*.rb'] + %w[bin/leetcode-ruby README.md LICENSE]
s.executable = 'leetcode-ruby'
Expand Down
71 changes: 71 additions & 0 deletions lib/medium/59_spiral_matrix_ii.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
# frozen_string_literal: true

# https://leetcode.com/problems/spiral-matrix-ii/
# @param {Integer} n
# @return {Integer[][]}
def generate_matrix(n)
i = 0
j = 0
up = 0
right = 1
left = 2
down = 4

up_side = 0
right_side = n
left_side = -1
down_side = n
direction = right
value = 0
result = ::Array.new(n) { ::Array.new(n) { nil } }

while value < n * n
if direction == up
while i > up_side
value += 1
result[i][j] = value
i -= 1
end

i += 1
j += 1
up_side += 1
direction = right
elsif direction == right
while j < right_side
value += 1
result[i][j] = value
j += 1
end

i += 1
j -= 1
right_side -= 1
direction = down
elsif direction == left
while j > left_side
value += 1
result[i][j] = value
j -= 1
end

i -= 1
j += 1
left_side += 1
direction = up
else
while i < down_side
value += 1
result[i][j] = value
i += 1
end

i -= 1
j -= 1
down_side -= 1
direction = left
end
end

result
end
12 changes: 12 additions & 0 deletions test/medium/test_59_spiral_matrix_ii.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# frozen_string_literal: true

require_relative '../test_helper'
require_relative '../../lib/medium/59_spiral_matrix_ii'
require 'minitest/autorun'

class SpiralMatrixIITest < ::Minitest::Test
def test_default
assert_equal([[1, 2, 3], [8, 9, 4], [7, 6, 5]], generate_matrix(3))
assert_equal([[1]], generate_matrix(1))
end
end
Loading