Skip to content

Commit

Permalink
Rotate Image
Browse files Browse the repository at this point in the history
  • Loading branch information
biezhihua committed Mar 6, 2018
1 parent d4b6779 commit dfc0257
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
46 changes: 46 additions & 0 deletions src/com/bzh/leetcode/array/RotateImage.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package com.bzh.leetcode.array;

import org.junit.Assert;
import org.junit.Test;

public class RotateImage {
@Test
public void test() {
int[][] test1 = new int[][]{
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};

rotate(test1);
Assert.assertArrayEquals(new int[][]{
{7, 4, 1},
{8, 5, 2},
{9, 6, 3}
}, test1);
}


public void rotate(int[][] matrix) {

int length = matrix.length;

// 调换对角元素
for (int i = 0; i < length; i++) {
for (int j = 0; j < length - i; j++) {
int tmp = matrix[i][j];
matrix[i][j] = matrix[length - j - 1][length - i - 1];
matrix[length - j - 1][length - i - 1] = tmp;
}
}

// 调换列元素
for (int i = 0; i < length; i++) {
for (int j = 0; j < length / 2; j++) {
int tmp = matrix[j][i];
matrix[j][i] = matrix[length - j - 1][i];
matrix[length - j - 1][i] = tmp;
}
}
}
}
9 changes: 9 additions & 0 deletions src/com/bzh/leetcode/array/ValidSudoKu.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.bzh.leetcode.array;

import org.junit.Assert;
import org.junit.Test;

public class ValidSudoKu {


}

0 comments on commit dfc0257

Please sign in to comment.