Skip to content

Commit 05f2a67

Browse files
author
Openset
committed
Add: rotate_image
1 parent 00412c8 commit 05f2a67

File tree

2 files changed

+68
-0
lines changed

2 files changed

+68
-0
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,15 @@
11
package rotate_image
2+
3+
func rotate(matrix [][]int) {
4+
l := len(matrix)
5+
t := make([][]int, l)
6+
for i := 0; i < l; i++ {
7+
t[i] = make([]int, l)
8+
copy(t[i], matrix[i])
9+
}
10+
for i := 0; i < l; i++ {
11+
for j := 0; j < l; j++ {
12+
matrix[j][l-1-i] = t[i][j]
13+
}
14+
}
15+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,55 @@
11
package rotate_image
2+
3+
import (
4+
"reflect"
5+
"testing"
6+
)
7+
8+
type caseType struct {
9+
input [][]int
10+
expected [][]int
11+
}
12+
13+
func TestRotate(t *testing.T) {
14+
tests := [...]caseType{
15+
{
16+
input: [][]int{
17+
{1, 2, 3},
18+
{4, 5, 6},
19+
{7, 8, 9},
20+
},
21+
expected: [][]int{
22+
{7, 4, 1},
23+
{8, 5, 2},
24+
{9, 6, 3},
25+
},
26+
},
27+
{
28+
input: [][]int{
29+
{5, 1, 9, 11},
30+
{2, 4, 8, 10},
31+
{13, 3, 6, 7},
32+
{15, 14, 12, 16},
33+
},
34+
expected: [][]int{
35+
{15, 13, 2, 5},
36+
{14, 3, 4, 1},
37+
{12, 6, 8, 9},
38+
{16, 7, 10, 11},
39+
},
40+
},
41+
}
42+
43+
for _, tc := range tests {
44+
l := len(tc.input)
45+
output := make([][]int, l)
46+
for i := 0; i < l; i++ {
47+
output[i] = make([]int, l)
48+
copy(output[i], tc.input[i])
49+
}
50+
rotate(output)
51+
if !reflect.DeepEqual(output, tc.expected) {
52+
t.Fatalf("input: %v, output: %v, expected: %v", tc.input, output, tc.expected)
53+
}
54+
}
55+
}

0 commit comments

Comments
 (0)