Skip to content

Commit c1f58db

Browse files
authored
Merge pull request #672 from fartem/48_Rotate_Image
2024-07-15 v. 6.1.5: added "48. Rotate Image"
2 parents 47b9493 + 642517d commit c1f58db

File tree

4 files changed

+69
-1
lines changed

4 files changed

+69
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -480,3 +480,4 @@ Profile on LeetCode: [fartem](https://leetcode.com/fartem/).
480480
| 39. Combination Sum | [Link](https://leetcode.com/problems/combination-sum/) | [Link](./lib/medium/39_combination_sum.rb) |
481481
| 43. Multiply Strings | [Link](https://leetcode.com/problems/multiply-strings/) | [Link](./lib/medium/43_multiply_strings.rb) |
482482
| 46. Permutations | [Link](https://leetcode.com/problems/permutations/) | [Link](./lib/medium/46_permutations.rb) |
483+
| 48. Rotate Image | [Link](https://leetcode.com/problems/rotate-image/) | [Link](./lib/medium/48_rotate_image.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.4'
8+
s.version = '6.1.5'
99
s.license = 'MIT'
1010
s.files = ::Dir['lib/**/*.rb'] + %w[bin/leetcode-ruby README.md LICENSE]
1111
s.executable = 'leetcode-ruby'

lib/medium/48_rotate_image.rb

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# frozen_string_literal: true
2+
3+
# https://leetcode.com/problems/rotate-image/
4+
# @param {Integer[][]} matrix
5+
# @return {Void} Do not return anything, modify matrix in-place instead.
6+
def rotate(matrix)
7+
n = matrix.length
8+
up = (n + 1) / 2
9+
10+
(0...up).each do |i|
11+
h = n / 2
12+
13+
(0...h).each do |j|
14+
f_index = n - j - 1
15+
temp = matrix[f_index][i]
16+
s_index = n - i - 1
17+
18+
matrix[f_index][i] = matrix[s_index][f_index]
19+
matrix[s_index][f_index] = matrix[j][s_index]
20+
matrix[j][s_index] = matrix[i][j]
21+
matrix[i][j] = temp
22+
end
23+
end
24+
end

test/medium/test_48_rotate_image.rb

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# frozen_string_literal: true
2+
3+
require_relative '../test_helper'
4+
require_relative '../../lib/medium/48_rotate_image'
5+
require 'minitest/autorun'
6+
7+
class RotateImageTest < ::Minitest::Test
8+
def test_default
9+
matrix = [
10+
[1, 2, 3],
11+
[4, 5, 6],
12+
[7, 8, 9]
13+
]
14+
rotate(matrix)
15+
16+
assert_equal(
17+
[
18+
[7, 4, 1],
19+
[8, 5, 2],
20+
[9, 6, 3]
21+
],
22+
matrix
23+
)
24+
25+
matrix = [
26+
[5, 1, 9, 11],
27+
[2, 4, 8, 10],
28+
[13, 3, 6, 7],
29+
[15, 14, 12, 16]
30+
]
31+
rotate(matrix)
32+
33+
assert_equal(
34+
[
35+
[15, 13, 2, 5],
36+
[14, 3, 4, 1],
37+
[12, 6, 8, 9],
38+
[16, 7, 10, 11]
39+
],
40+
matrix
41+
)
42+
end
43+
end

0 commit comments

Comments
 (0)