Skip to content

2024-07-15 v. 6.1.5: added "48. Rotate Image" #672

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 15, 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 @@ -480,3 +480,4 @@ Profile on LeetCode: [fartem](https://leetcode.com/fartem/).
| 39. Combination Sum | [Link](https://leetcode.com/problems/combination-sum/) | [Link](./lib/medium/39_combination_sum.rb) |
| 43. Multiply Strings | [Link](https://leetcode.com/problems/multiply-strings/) | [Link](./lib/medium/43_multiply_strings.rb) |
| 46. Permutations | [Link](https://leetcode.com/problems/permutations/) | [Link](./lib/medium/46_permutations.rb) |
| 48. Rotate Image | [Link](https://leetcode.com/problems/rotate-image/) | [Link](./lib/medium/48_rotate_image.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.1.4'
s.version = '6.1.5'
s.license = 'MIT'
s.files = ::Dir['lib/**/*.rb'] + %w[bin/leetcode-ruby README.md LICENSE]
s.executable = 'leetcode-ruby'
Expand Down
24 changes: 24 additions & 0 deletions lib/medium/48_rotate_image.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# frozen_string_literal: true

# https://leetcode.com/problems/rotate-image/
# @param {Integer[][]} matrix
# @return {Void} Do not return anything, modify matrix in-place instead.
def rotate(matrix)
n = matrix.length
up = (n + 1) / 2

(0...up).each do |i|
h = n / 2

(0...h).each do |j|
f_index = n - j - 1
temp = matrix[f_index][i]
s_index = n - i - 1

matrix[f_index][i] = matrix[s_index][f_index]
matrix[s_index][f_index] = matrix[j][s_index]
matrix[j][s_index] = matrix[i][j]
matrix[i][j] = temp
end
end
end
43 changes: 43 additions & 0 deletions test/medium/test_48_rotate_image.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# frozen_string_literal: true

require_relative '../test_helper'
require_relative '../../lib/medium/48_rotate_image'
require 'minitest/autorun'

class RotateImageTest < ::Minitest::Test
def test_default
matrix = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
]
rotate(matrix)

assert_equal(
[
[7, 4, 1],
[8, 5, 2],
[9, 6, 3]
],
matrix
)

matrix = [
[5, 1, 9, 11],
[2, 4, 8, 10],
[13, 3, 6, 7],
[15, 14, 12, 16]
]
rotate(matrix)

assert_equal(
[
[15, 13, 2, 5],
[14, 3, 4, 1],
[12, 6, 8, 9],
[16, 7, 10, 11]
],
matrix
)
end
end
Loading