-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathinterview_01.07_test.go
101 lines (95 loc) · 1.51 KB
/
interview_01.07_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
//给你一幅由 N × N 矩阵表示的图像,其中每个像素的大小为 4 字节。请你设计一种算法,将图像旋转 90 度。
//
// 不占用额外内存空间能否做到?
//
//
//
// 示例 1:
//
//
//给定 matrix =
//[
// [1,2,3],
// [4,5,6],
// [7,8,9]
//],
//
//原地旋转输入矩阵,使其变为:
//[
// [7,4,1],
// [8,5,2],
// [9,6,3]
//]
//
//
// 示例 2:
//
//
//给定 matrix =
//[
// [ 5, 1, 9,11],
// [ 2, 4, 8,10],
// [13, 3, 6, 7],
// [15,14,12,16]
//],
//
//原地旋转输入矩阵,使其变为:
//[
// [15,13, 2, 5],
// [14, 3, 4, 1],
// [12, 6, 8, 9],
// [16, 7,10,11]
//]
//
//
// 注意:本题与主站 48 题相同:https://leetcode-cn.com/problems/rotate-image/
// Related Topics 数组
// 👍 169 👎 0
package leetcode
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestRotate(t *testing.T) {
matrix := [][]int{
{1, 2, 3},
{4, 5, 6},
{7, 8, 9},
}
rotate(matrix)
assert.Equal(t, [][]int{
{7, 4, 1},
{8, 5, 2},
{9, 6, 3},
}, matrix)
matrix = [][]int{
{5, 1, 9, 11},
{2, 4, 8, 10},
{13, 3, 6, 7},
{15, 14, 12, 16},
}
rotate(matrix)
assert.Equal(t, [][]int{
{15, 13, 2, 5},
{14, 3, 4, 1},
{12, 6, 8, 9},
{16, 7, 10, 11},
}, matrix)
}
func rotate(matrix [][]int) {
if len(matrix) < 2 {
return
}
var (
n = len(matrix)
list = make([][]int, n)
)
for x := 0; x < n; x++ {
inner := make([]int, n)
for y := n - 1; y >= 0; y-- {
inner[n-1-y] = matrix[y][x]
}
list[x] = inner
}
copy(matrix, list)
}