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

48. Rotate Image #35

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
zwfang merged 1 commit into master from rotate-image
Jan 2, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ continually updating 😃.
* [11. Container With Most Water](./src/0011_container_with_most_water/container_with_most_water.go)   *`double index;`*  *`array`*
* [26. Remove Duplicates from Sorted Array](./src/0026_remove_duplicates_from_sorted_array/rdfsa.go)   *`double index;`*  *`array`*
* [27. Remove Element](src/0027_remove_element/remove_element.go)   *`double index;`*  *`array`*
* [48. Rotate Image](src/0048_rotate_image/rotate_image.go)
* [75. Sort Colors](./src/0075_sort_colors/sort_colors.go)   *`sort;`*  *`array`*
* [80. Remove Duplicates from Sorted Array II](./src/0080_remove_duplicates_from_sorted_array2/rdfsa2.go)   *`double index;`*  *`array`*
* [88. Merge Sorted Array](./src/0088_merge_sorted_array/msa.go)   *`sort;`*  *`array`*
Expand Down
52 changes: 52 additions & 0 deletions src/0048_rotate_image/rotate_image.go
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
48. Rotate Image
https://leetcode.com/problems/rotate-image/
You are given an n x n 2D matrix representing an image.
Rotate the image by 90 degrees (clockwise).
Note:
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.
*/
// time: 2019年01月02日

package ri

// time complexity: O(n^2)
// space complexity: O(1)
func rotate(matrix [][]int) {
/*
[
[1,2,3],
[4,5,6],
[7,8,9]
]
*/
n := len(matrix)
for i := 0; i < n; i++ {
for j := i + 1; j < n; j++ {
matrix[i][j], matrix[j][i] = matrix[j][i], matrix[i][j]
}
}
/*
[
[1,4,7],
[2,5,8],
[3,6,9]
]
*/
for i := 0; i < n; i++ {
for j := 0; j < n/2; j++ {
matrix[i][j], matrix[i][n-1-j] = matrix[i][n-1-j], matrix[i][j]
}
}
/*
[
[7,4,1],
[8,5,2],
[9,6,3]
]
*/
}
26 changes: 26 additions & 0 deletions src/0048_rotate_image/rotate_image_test.go
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package ri

import (
"reflect"
"testing"
)

func TestRotate(t *testing.T) {
matrix := [][]int{
{5, 1, 9, 11},
{2, 4, 8, 10},
{13, 3, 6, 7},
{15, 14, 12, 16},
}

expected := [][]int{
{15, 13, 2, 5},
{14, 3, 4, 1},
{12, 6, 8, 9},
{16, 7, 10, 11},
}

if rotate(matrix); !reflect.DeepEqual(matrix, expected) {
t.Errorf("expected %v, got %v", expected, matrix)
}
}
1 change: 1 addition & 0 deletions src/README.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
|0033|[Search in Rotated Sorted Array](0033_search_in_rotated_sorted_array/search_in_rotated_sorted_array.go)|Medium|*`binary search`*|
|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`*|
|0035|[35. Search Insert Position](0035_search_insert_position/search_insert_position.go)|Easy|*`binary search`*|
|0048|[48. Rotate Image](0048_rotate_image/rotate_image.go)|Medium|*`array`*|
|0061|[Rotate List](./0061_rotate_list/rotate_list.go)|Medium|*`linked list`*|
|0062|[Unique Paths](./0062_unique_paths/unique_paths.go)|Medium|*`recursion;`* *`memory search;`* *`dynamic programming`*|
|0063|[Unique Paths 2](./0063_unique_paths_2/unique_paths2.go)|Medium|*`recursion;`* *`memory search;`* *`dynamic programming`*|
Expand Down

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