File tree 4 files changed +69
-1
lines changed
4 files changed +69
-1
lines changed Original file line number Diff line number Diff line change @@ -480,3 +480,4 @@ Profile on LeetCode: [fartem](https://leetcode.com/fartem/).
480
480
| 39. Combination Sum | [ Link] ( https://leetcode.com/problems/combination-sum/ ) | [ Link] ( ./lib/medium/39_combination_sum.rb ) |
481
481
| 43. Multiply Strings | [ Link] ( https://leetcode.com/problems/multiply-strings/ ) | [ Link] ( ./lib/medium/43_multiply_strings.rb ) |
482
482
| 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 ) |
Original file line number Diff line number Diff line change @@ -5,7 +5,7 @@ require 'English'
5
5
::Gem ::Specification . new do |s |
6
6
s . required_ruby_version = '>= 3.0'
7
7
s . name = 'leetcode-ruby'
8
- s . version = '6.1.4 '
8
+ s . version = '6.1.5 '
9
9
s . license = 'MIT'
10
10
s . files = ::Dir [ 'lib/**/*.rb' ] + %w[ bin/leetcode-ruby README.md LICENSE ]
11
11
s . executable = 'leetcode-ruby'
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
You can’t perform that action at this time.
0 commit comments