Skip to content

Commit 66387f7

Browse files
committed
48. Rotate Image
1 parent 9b0e4ad commit 66387f7

File tree

3 files changed

+169
-74
lines changed

3 files changed

+169
-74
lines changed

src/main/java/joshua/leetcode/array/RotateArray189.java

Lines changed: 70 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -2,85 +2,81 @@
22

33
public abstract class RotateArray189 {
44

5-
/**
6-
* Rotate an array of n elements to the right by k steps.
5+
/**
6+
* Rotate an array of n elements to the right by k steps.
7+
* <p/>
8+
* For example, with n = 7 and k = 3, the array [1,2,3,4,5,6,7] is rotated to [5,6,7,1,2,3,4].
9+
*/
710

8-
For example, with n = 7 and k = 3, the array [1,2,3,4,5,6,7] is rotated to [5,6,7,1,2,3,4].
9-
*/
10-
11-
public abstract void rotate(int[] nums, int k);
11+
public abstract void rotate(int[] nums, int k);
1212

13-
static class Solution1 extends RotateArray189{
13+
static class Solution1 extends RotateArray189 {
1414

15-
public void swap(int[] a, int i, int j)
16-
{
17-
int temp = a[i];
18-
a[i] = a[j];
19-
a[j] = temp;
20-
}
15+
public void swap(int[] a, int i, int j) {
16+
int temp = a[i];
17+
a[i] = a[j];
18+
a[j] = temp;
19+
}
2120

22-
/**
23-
* for example, array:[1,2,3,4,5,6], step=2
24-
* the steps will be:
25-
* [1,6,3,4,5,2], idx=1, temp=5
26-
* [1,6,3,2.5.4], idx=3, temp=5
27-
* [1,6,3,2,5,4], idx=(3+2)%6=5==temp, so temp=temp-1=4, idx=4, no element moved in this iteration
28-
* [5,6,3,2,1,4], idx=0, temp=4
29-
* [5,6,1,2,3,4], idx=2, temp=4, iteration exit.
30-
*/
31-
public void rotate(int[] a, int k)
32-
{
33-
int N = a.length;
34-
k = k%N;
35-
int idx = N-1;
36-
int temp = N-1;
37-
/**
38-
* every step will swap two element, within which one element gets the final position.
39-
* So in total it only needs N-1 step.
40-
*/
41-
for (int s = 0; s < N-1; s++)
42-
{
43-
idx = (idx+k)%N;
44-
if(temp == idx)
45-
{
46-
temp = temp-1;
47-
idx = temp;
48-
continue;
49-
}
50-
/*idx is now the final position of element at position temp*/
51-
swap(a,temp,idx);
52-
}
53-
}
54-
}
55-
56-
static class Solution2 extends RotateArray189{
21+
/**
22+
* for example, array:[1,2,3,4,5,6], step=2
23+
* the steps will be:
24+
* [1,6,3,4,5,2], idx=1, temp=5
25+
* [1,6,3,2.5.4], idx=3, temp=5
26+
* [1,6,3,2,5,4], idx=(3+2)%6=5==temp, so temp=temp-1=4, idx=4, no element moved in this iteration
27+
* [5,6,3,2,1,4], idx=0, temp=4
28+
* [5,6,1,2,3,4], idx=2, temp=4, iteration exit.
29+
*/
30+
public void rotate(int[] a, int k) {
31+
int N = a.length;
32+
k = k % N;
33+
int idx = N - 1;
34+
int temp = N - 1;
35+
/**
36+
* every step will swap two element, within which one element gets the final position.
37+
* So in total it only needs N-1 step.
38+
*/
39+
for (int s = 0; s < N - 1; s++) {
40+
idx = (idx + k) % N;
41+
if (temp == idx) {
42+
temp = temp - 1;
43+
idx = temp;
44+
continue;
45+
}
46+
/*idx is now the final position of element at position temp*/
47+
swap(a, temp, idx);
48+
}
49+
}
50+
}
5751

58-
/**
59-
* example [1,2,3,4,5,6,7], step=3
60-
*
61-
* rotate the [1,2,3,4] firstly as [4,3,2,1,5,6,7]
62-
* rotate the [5,6,7] secondly as [4,3,2,1,7,6,5]
63-
* rotate the whole array as [5,6,7,1,2,3,4]
64-
*/
65-
@Override
66-
public void rotate(int[] nums, int k) {
67-
k=k%nums.length;
68-
rotate(nums,0,nums.length-k-1);
69-
rotate(nums,nums.length-k,nums.length-1);
70-
rotate(nums,0,nums.length-1);
71-
}
72-
73-
private void rotate(int[] nums, int start, int end){
74-
while(start<end){
75-
nums[start] ^= nums[end];
76-
nums[end] ^= nums[start];
77-
nums[start] ^= nums[end];
78-
start++;
79-
end--;
80-
}
81-
}
82-
83-
}
52+
static class Solution2 extends RotateArray189 {
53+
54+
/**
55+
* example [1,2,3,4,5,6,7], step=3
56+
* <p/>
57+
* rotate the [1,2,3,4] firstly as [4,3,2,1,5,6,7]
58+
* rotate the [5,6,7] secondly as [4,3,2,1,7,6,5]
59+
* rotate the whole array as [5,6,7,1,2,3,4]
60+
*/
61+
@Override
62+
public void rotate(int[] nums, int k) {
63+
k = k % nums.length;
64+
rotate(nums, 0, nums.length - k - 1);
65+
rotate(nums, nums.length - k, nums.length - 1);
66+
rotate(nums, 0, nums.length - 1);
67+
}
68+
69+
private void rotate(int[] nums, int start, int end) {
70+
while (start < end) {
71+
nums[start] ^= nums[end];
72+
nums[end] ^= nums[start];
73+
nums[start] ^= nums[end];
74+
start++;
75+
end--;
76+
}
77+
}
78+
79+
}
8480
}
8581

8682

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
// Copyright 2016 Baidu Inc. All rights reserved.
2+
3+
package joshua.leetcode.array;
4+
5+
/**
6+
* 48. Rotate Image<br>
7+
* <a href="https://leetcode.com/problems/rotate-image/">leetcode link</a>
8+
*
9+
* @author Jiang Yong ([email protected])
10+
*/
11+
public abstract class RotateImage {
12+
13+
/**
14+
* You are given an n x n 2D matrix representing an image.
15+
* <p/>
16+
* Rotate the image by 90 degrees (clockwise).
17+
* <p/>
18+
* Follow up:
19+
* <b>Could you do this in-place?</b>
20+
*
21+
* @param matrix
22+
*/
23+
public abstract void rotate(int[][] matrix);
24+
25+
/**
26+
* 可以从外到内,分层旋转。关键是厘清所有的下标转换。以大小为4*4的矩阵为例。
27+
* (0,0),(0,1),(0,2),(0,3)
28+
* (1,0),(1,1),(1,2),(1,3)
29+
* (2,0),(2,1),(2,2),(2,3)
30+
* (3,0),(3,1),(3,2),(3,3)
31+
* 最外层,上下界为min = 0和 max = 3,旋转为:
32+
* 上 -> 右 -> 下 -> 左 - > 上, 从上面的元素开始,(min,min) 到 (min,max-1), 记 i begins with min ,ends with max-1.
33+
* (0,0) -> (0,3) -> (3,3) -> (3,0) -> (0,0), i = 0
34+
* (0,1) -> (1,3) -> (3,2) -> (2,0) -> (0,1), i = 1
35+
* (0,2) -> (2,3) -> (3,1) -> (1,0) -> (0,2), i = 2
36+
*/
37+
public static class Solution1 extends RotateImage {
38+
39+
@Override
40+
public void rotate(int[][] matrix) {
41+
int size = matrix.length;
42+
for (int layer = 0; layer < size / 2; layer ++) {
43+
int min = layer;
44+
int max = size -1 - layer;
45+
for(int i = min; i < max ; i++) {
46+
int offset = i - min;
47+
// save top
48+
int top = matrix[min][i];// begins with (min,min), end with (min, max -1)
49+
// left to top
50+
matrix[min][i] = matrix[max - offset][min];
51+
// bottom to left
52+
matrix[max - offset][min] = matrix[max][max - offset];
53+
//right to bottom
54+
matrix[max][max - offset] = matrix[i][max];
55+
//top to right
56+
matrix[i][max] = top;
57+
}
58+
}
59+
}
60+
}
61+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package joshua.leetcode.array;
2+
3+
import org.apache.commons.lang3.ArrayUtils;
4+
import org.junit.Before;
5+
import org.junit.Test;
6+
7+
public class RotateImageTest {
8+
9+
10+
private int[][] matrix;
11+
12+
@Before
13+
public void setUp() {
14+
matrix = new int[][]{
15+
{0, 1, 2, 3},
16+
{4, 5, 6, 7},
17+
{8, 9, 10, 11},
18+
{12, 13, 14, 15}};
19+
// matrix = new int[][]{
20+
// {0, 1},
21+
// {4, 5}};
22+
}
23+
24+
@Test
25+
public void testSolution1() {
26+
RotateImage image = new RotateImage.Solution1();
27+
image.rotate(matrix);
28+
System.out.println(ArrayUtils.toString(image));
29+
for (int i =0; i< matrix.length; i++) {
30+
for(int j = 0; j<matrix[0].length;j++) {
31+
System.out.print(matrix[i][j]);
32+
System.out.print("\t");
33+
}
34+
System.out.print("\n");
35+
}
36+
}
37+
38+
}

0 commit comments

Comments
 (0)