Skip to content

Commit b90e9fb

Browse files
committed
feat: finish 48
1 parent e49b8d4 commit b90e9fb

File tree

3 files changed

+116
-0
lines changed

3 files changed

+116
-0
lines changed

algorithm/48.RotateImage/README.md

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# [48. Rotate Image](https://leetcode.com/problems/rotate-image/)
2+
3+
## 2019/09/15
4+
5+
### 题目 💗[medium]
6+
7+
You are given an n x n 2D matrix representing an image.
8+
9+
Rotate the image by 90 degrees (clockwise).
10+
11+
Note:
12+
13+
You have to rotate the image in-place, which means you have to modify the input 2D matrix directly. DO NOT allocate another 2D matrix and do the rotation.
14+
15+
Example 1:
16+
17+
```bash
18+
Given input matrix =
19+
[
20+
[1,2,3],
21+
[4,5,6],
22+
[7,8,9]
23+
],
24+
25+
rotate the input matrix in-place such that it becomes:
26+
[
27+
[7,4,1],
28+
[8,5,2],
29+
[9,6,3]
30+
]
31+
```
32+
33+
Example 2:
34+
35+
```bash
36+
Given input matrix =
37+
[
38+
[ 5, 1, 9,11],
39+
[ 2, 4, 8,10],
40+
[13, 3, 6, 7],
41+
[15,14,12,16]
42+
],
43+
44+
rotate the input matrix in-place such that it becomes:
45+
[
46+
[15,13, 2, 5],
47+
[14, 3, 4, 1],
48+
[12, 6, 8, 9],
49+
[16, 7,10,11]
50+
]
51+
```

algorithm/48.RotateImage/main.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package deleteDuplicates
2+
3+
import (
4+
"github.com/pengliheng/leetcode/Helper"
5+
)
6+
7+
type ListNode = Helper.ListNode
8+
9+
func deleteDuplicates(head *ListNode) *ListNode {
10+
arr := []int{}
11+
for head != nil {
12+
// remove duplicate
13+
if lastIndexOf(arr, head.Val) == -1 {
14+
arr = append(arr, head.Val)
15+
}
16+
head = head.Next
17+
}
18+
node := &ListNode{Val: 0}
19+
newHead := node
20+
for len(arr) > 0 {
21+
node.Next = &ListNode{Val: arr[0]}
22+
node = node.Next
23+
arr = arr[1:]
24+
}
25+
return newHead.Next
26+
}
27+
28+
func lastIndexOf(nums []int, val int) int {
29+
for i := len(nums) - 1; i >= 0; i-- {
30+
if nums[i] == val {
31+
return i
32+
}
33+
}
34+
return -1
35+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package deleteDuplicates
2+
3+
import (
4+
"testing"
5+
6+
"github.com/pengliheng/leetcode/Helper"
7+
8+
"github.com/stretchr/testify/assert"
9+
)
10+
11+
var tcs = []struct {
12+
N1 *ListNode
13+
ans *ListNode
14+
}{
15+
{
16+
Helper.Ints2LinkList([]int{1, 1, 2}),
17+
Helper.Ints2LinkList([]int{1, 2}),
18+
},
19+
{
20+
Helper.Ints2LinkList([]int{1, 1, 2, 3, 3}),
21+
Helper.Ints2LinkList([]int{1, 2, 3}),
22+
},
23+
}
24+
25+
func Test_bitwiseComplement(t *testing.T) {
26+
ast := assert.New(t)
27+
for _, tc := range tcs {
28+
ast.Equal(tc.ans, deleteDuplicates(tc.N1), "输入:%v", tc)
29+
}
30+
}

0 commit comments

Comments
 (0)