From 642517d712e364c204c46b87a3ce65c789397660 Mon Sep 17 00:00:00 2001 From: fartem Date: Mon, 15 Jul 2024 06:27:01 +0300 Subject: [PATCH] 2024-07-15 v. 6.1.5: added "48. Rotate Image" --- README.md | 1 + leetcode-ruby.gemspec | 2 +- lib/medium/48_rotate_image.rb | 24 ++++++++++++++++ test/medium/test_48_rotate_image.rb | 43 +++++++++++++++++++++++++++++ 4 files changed, 69 insertions(+), 1 deletion(-) create mode 100644 lib/medium/48_rotate_image.rb create mode 100644 test/medium/test_48_rotate_image.rb diff --git a/README.md b/README.md index 14678b95..b6bef36b 100644 --- a/README.md +++ b/README.md @@ -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) | diff --git a/leetcode-ruby.gemspec b/leetcode-ruby.gemspec index 6493d512..6626c228 100644 --- a/leetcode-ruby.gemspec +++ b/leetcode-ruby.gemspec @@ -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' diff --git a/lib/medium/48_rotate_image.rb b/lib/medium/48_rotate_image.rb new file mode 100644 index 00000000..0cdc2833 --- /dev/null +++ b/lib/medium/48_rotate_image.rb @@ -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 diff --git a/test/medium/test_48_rotate_image.rb b/test/medium/test_48_rotate_image.rb new file mode 100644 index 00000000..c60ac4f7 --- /dev/null +++ b/test/medium/test_48_rotate_image.rb @@ -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