Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 7788c4d

Browse files
authored
48 solved. (#35)
1 parent 3573e2e commit 7788c4d

File tree

4 files changed

+80
-0
lines changed

4 files changed

+80
-0
lines changed

‎README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ continually updating 😃.
1515
* [11. Container With Most Water](./src/0011_container_with_most_water/container_with_most_water.go)   *`double index;`*  *`array`*
1616
* [26. Remove Duplicates from Sorted Array](./src/0026_remove_duplicates_from_sorted_array/rdfsa.go)   *`double index;`*  *`array`*
1717
* [27. Remove Element](src/0027_remove_element/remove_element.go)   *`double index;`*  *`array`*
18+
* [48. Rotate Image](src/0048_rotate_image/rotate_image.go)
1819
* [75. Sort Colors](./src/0075_sort_colors/sort_colors.go)   *`sort;`*  *`array`*
1920
* [80. Remove Duplicates from Sorted Array II](./src/0080_remove_duplicates_from_sorted_array2/rdfsa2.go)   *`double index;`*  *`array`*
2021
* [88. Merge Sorted Array](./src/0088_merge_sorted_array/msa.go)   *`sort;`*  *`array`*
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
48. Rotate Image
3+
https://leetcode.com/problems/rotate-image/
4+
5+
You are given an n x n 2D matrix representing an image.
6+
Rotate the image by 90 degrees (clockwise).
7+
8+
Note:
9+
You have to rotate the image in-place,
10+
which means you have to modify the input 2D matrix directly.
11+
DO NOT allocate another 2D matrix and do the rotation.
12+
*/
13+
// time: 2019年01月02日
14+
15+
package ri
16+
17+
// time complexity: O(n^2)
18+
// space complexity: O(1)
19+
func rotate(matrix [][]int) {
20+
/*
21+
[
22+
[1,2,3],
23+
[4,5,6],
24+
[7,8,9]
25+
]
26+
*/
27+
n := len(matrix)
28+
for i := 0; i < n; i++ {
29+
for j := i + 1; j < n; j++ {
30+
matrix[i][j], matrix[j][i] = matrix[j][i], matrix[i][j]
31+
}
32+
}
33+
/*
34+
[
35+
[1,4,7],
36+
[2,5,8],
37+
[3,6,9]
38+
]
39+
*/
40+
for i := 0; i < n; i++ {
41+
for j := 0; j < n/2; j++ {
42+
matrix[i][j], matrix[i][n-1-j] = matrix[i][n-1-j], matrix[i][j]
43+
}
44+
}
45+
/*
46+
[
47+
[7,4,1],
48+
[8,5,2],
49+
[9,6,3]
50+
]
51+
*/
52+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package ri
2+
3+
import (
4+
"reflect"
5+
"testing"
6+
)
7+
8+
func TestRotate(t *testing.T) {
9+
matrix := [][]int{
10+
{5, 1, 9, 11},
11+
{2, 4, 8, 10},
12+
{13, 3, 6, 7},
13+
{15, 14, 12, 16},
14+
}
15+
16+
expected := [][]int{
17+
{15, 13, 2, 5},
18+
{14, 3, 4, 1},
19+
{12, 6, 8, 9},
20+
{16, 7, 10, 11},
21+
}
22+
23+
if rotate(matrix); !reflect.DeepEqual(matrix, expected) {
24+
t.Errorf("expected %v, got %v", expected, matrix)
25+
}
26+
}

‎src/README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
|0033|[Search in Rotated Sorted Array](0033_search_in_rotated_sorted_array/search_in_rotated_sorted_array.go)|Medium|*`binary search`*|
2525
|0034|[Find First and Last Position of Element in Sorted Array](0034_find_first_and_last_position_of_element_in_sorted_array/find_first_and_last_position_of_element_in_sorted_array.go)|Medium|*`binary search`*|
2626
|0035|[35. Search Insert Position](0035_search_insert_position/search_insert_position.go)|Easy|*`binary search`*|
27+
|0048|[48. Rotate Image](0048_rotate_image/rotate_image.go)|Medium|*`array`*|
2728
|0061|[Rotate List](./0061_rotate_list/rotate_list.go)|Medium|*`linked list`*|
2829
|0062|[Unique Paths](./0062_unique_paths/unique_paths.go)|Medium|*`recursion;`* *`memory search;`* *`dynamic programming`*|
2930
|0063|[Unique Paths 2](./0063_unique_paths_2/unique_paths2.go)|Medium|*`recursion;`* *`memory search;`* *`dynamic programming`*|

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /