Skip to content

Commit 819945f

Browse files
committed
feat(algorithms): add rotate_image
1 parent 659d367 commit 819945f

File tree

3 files changed

+89
-0
lines changed

3 files changed

+89
-0
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
| # | Problem | Solution | Difficulty | Single Repetition Duration | LeetCode Run Time |
2626
| ---: | :----- | :--------: | :----------: | ----------: | ----------: |
27+
|48|[Rotate Image][Algorithms-48]|[WindomZ][Algorithms-48-Go]|Medium|[12.1 ns/op/3 test cases][Algorithms-48-Test]|3 ms|
2728
|47|[Permutations II][Algorithms-47]|[WindomZ][Algorithms-47-Go]|Medium|[531 ns/op/3 test cases][Algorithms-47-Test]|19 ms|
2829
|46|[Permutations][Algorithms-46]|[WindomZ][Algorithms-46-Go]|Medium|[1713 ns/op/3 test cases][Algorithms-46-Test]|9 ms|
2930
|45|[Jump Game II][Algorithms-45]|[WindomZ][Algorithms-45-Go]|Hard|[19.6 ns/op/6 test cases][Algorithms-45-Test]|19 ms|
@@ -100,6 +101,9 @@ Welcome to report bugs, suggest ideas and discuss on [issues page](https://githu
100101
### Support
101102
If you like it then you can put a :star:Star on it.
102103

104+
[Algorithms-48-Test]:algorithms/rotate_image/rotate_test.go#L43
105+
[Algorithms-48-Go]:algorithms/rotate_image/rotate.go
106+
[Algorithms-48]:https://leetcode.com/rotate-image/
103107
[Algorithms-47-Test]:algorithms/permutations_ii/permuteUnique_test.go#L53
104108
[Algorithms-47-Go]:algorithms/permutations_ii/permuteUnique.go
105109
[Algorithms-47]:https://leetcode.com/permutations-ii/

algorithms/rotate_image/rotate.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package rotateimage
2+
3+
// rotate clockwise rotate
4+
// flip the matrix vertically, then flip it symmetrically
5+
// 1 2 3 7 8 9 7 4 1
6+
// 4 5 6 => 4 5 6 => 8 5 2
7+
// 7 8 9 1 2 3 9 6 3
8+
func rotate(matrix [][]int) {
9+
// flip the matrix vertically
10+
for i := 0; i < len(matrix)/2; i++ {
11+
for j := 0; j < len(matrix); j++ {
12+
matrix[i][j], matrix[len(matrix)-i-1][j] =
13+
matrix[len(matrix)-i-1][j], matrix[i][j] // swap
14+
}
15+
}
16+
// flip it symmetrically
17+
for i := 0; i < len(matrix); i++ {
18+
for j := i; j < len(matrix); j++ {
19+
matrix[i][j], matrix[j][i] = matrix[j][i], matrix[i][j] // swap
20+
}
21+
}
22+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package rotateimage
2+
3+
import (
4+
"testing"
5+
6+
"github.com/WindomZ/testify/assert"
7+
)
8+
9+
func Test_rotate(t *testing.T) {
10+
var matrix [][]int
11+
12+
matrix = [][]int{}
13+
rotate(matrix)
14+
assert.Equal(t, [][]int{}, matrix)
15+
16+
matrix = [][]int{
17+
{1, 2, 3},
18+
{4, 5, 6},
19+
{7, 8, 9},
20+
}
21+
rotate(matrix)
22+
assert.Equal(t, [][]int{
23+
{7, 4, 1},
24+
{8, 5, 2},
25+
{9, 6, 3},
26+
}, matrix)
27+
28+
matrix = [][]int{
29+
{5, 1, 9, 11},
30+
{2, 4, 8, 10},
31+
{13, 3, 6, 7},
32+
{15, 14, 12, 16},
33+
}
34+
rotate(matrix)
35+
assert.Equal(t, [][]int{
36+
{15, 13, 2, 5},
37+
{14, 3, 4, 1},
38+
{12, 6, 8, 9},
39+
{16, 7, 10, 11},
40+
}, matrix)
41+
}
42+
43+
func Benchmark_rotate(b *testing.B) {
44+
b.StopTimer()
45+
b.ReportAllocs()
46+
b.StartTimer()
47+
b.RunParallel(func(pb *testing.PB) {
48+
for pb.Next() {
49+
rotate([][]int{})
50+
rotate([][]int{
51+
{1, 2, 3},
52+
{4, 5, 6},
53+
{7, 8, 9},
54+
})
55+
rotate([][]int{
56+
{5, 1, 9, 11},
57+
{2, 4, 8, 10},
58+
{13, 3, 6, 7},
59+
{15, 14, 12, 16},
60+
})
61+
}
62+
})
63+
}

0 commit comments

Comments
 (0)