Skip to content

Commit 2a2afe1

Browse files
committed
Matrix Rotations
Matrix Rotations
1 parent a6f549c commit 2a2afe1

File tree

4 files changed

+129
-0
lines changed

4 files changed

+129
-0
lines changed

Diff for: bin/chapter06trees/CheckAVL.class

-179 Bytes
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*Copyright (c) Dec 22, 2014 CareerMonk Publications and others.
2+
* E-Mail : info@careermonk.com
3+
* Creation Date : 2015-01-10 06:15:46
4+
* Last modification : 2006-05-31
5+
by : Narasimha Karumanchi
6+
* File Name : AddOneToNumber.java
7+
* Book Title : Data Structures And Algorithms Made In Java
8+
* Warranty : This software is provided "as is" without any
9+
* warranty; without even the implied warranty of
10+
* merchantability or fitness for a particular purpose.
11+
*
12+
*/
13+
14+
package chapter21miscconcepts;
15+
public class SquareMatrixRotationByKElementsTest {
16+
public static void main(String[] args) {
17+
SquareMatrixRotationByOneElement testIntance = new SquareMatrixRotationByOneElement();
18+
int[][] input = new int[3][3];
19+
input[0] = new int[]{1,2,3};
20+
input[1] = new int[]{4,5,6};
21+
input[2] = new int[]{7,8,9};
22+
int n = input.length;
23+
System.out.println("Input:");
24+
for(int i = 0 ; i < n; i++) {
25+
for(int j = 0 ; j < n; j++) {
26+
System.out.print(input[i][j] + " ");
27+
28+
}
29+
System.out.println(" ");
30+
}
31+
int k = 2;
32+
for(int i = 0 ; i < k; i++) {
33+
input = testIntance.rotateByOneElementClockwise(input);
34+
}
35+
System.out.println("Output:");
36+
for(int i = 0 ; i < n; i++) {
37+
for(int j = 0 ; j < n; j++) {
38+
System.out.print(input[i][j] + " ");
39+
40+
}
41+
System.out.println(" ");
42+
}
43+
}
44+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*Copyright (c) Dec 22, 2014 CareerMonk Publications and others.
2+
* E-Mail : info@careermonk.com
3+
* Creation Date : 2015-01-10 06:15:46
4+
* Last modification : 2006-05-31
5+
by : Narasimha Karumanchi
6+
* File Name : AddOneToNumber.java
7+
* Book Title : Data Structures And Algorithms Made In Java
8+
* Warranty : This software is provided "as is" without any
9+
* warranty; without even the implied warranty of
10+
* merchantability or fitness for a particular purpose.
11+
*
12+
*/
13+
14+
15+
package chapter21miscconcepts;
16+
17+
public class SquareMatrixRotationByOneElement {
18+
int[][] rotateByOneElementClockwise(int[][] matrix) {
19+
20+
final int n = matrix.length;
21+
int[][] roataedMatrix = new int[n][n];
22+
for(int i = 0 ; i < n; i++) {
23+
for(int j = 0 ; j < n; j++) {
24+
if(i+j<n-1 && i<=j) {
25+
// move right
26+
roataedMatrix[i][j+1] = matrix[i][j];
27+
} else if(i+j>n-1 && i>=j) {
28+
// move left
29+
roataedMatrix[i][j-1] = matrix[i][j];
30+
} else if(i+j<=n-1 && i>j){
31+
// move top
32+
roataedMatrix[i-1][j] = matrix[i][j];
33+
} else if(i+j>=n-1 && i<j) {
34+
// move down
35+
roataedMatrix[i+1][j] = matrix[i][j];
36+
} else {
37+
roataedMatrix[i][j] = matrix[i][j];
38+
}
39+
}
40+
}
41+
return roataedMatrix;
42+
}
43+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*Copyright (c) Dec 22, 2014 CareerMonk Publications and others.
2+
* E-Mail : info@careermonk.com
3+
* Creation Date : 2015-01-10 06:15:46
4+
* Last modification : 2006-05-31
5+
by : Narasimha Karumanchi
6+
* File Name : AddOneToNumber.java
7+
* Book Title : Data Structures And Algorithms Made In Java
8+
* Warranty : This software is provided "as is" without any
9+
* warranty; without even the implied warranty of
10+
* merchantability or fitness for a particular purpose.
11+
*
12+
*/
13+
14+
15+
package chapter21miscconcepts;
16+
public class SquareMatrixRotationByOneElementTest {
17+
public static void main(String[] args) {
18+
SquareMatrixRotationByOneElement testIntance = new SquareMatrixRotationByOneElement();
19+
final int[][] input = new int[3][3];
20+
input[0] = new int[]{1,2,3};
21+
input[1] = new int[]{4,5,6};
22+
input[2] = new int[]{7,8,9};
23+
int n = input.length;
24+
final int[][] result = testIntance.rotateByOneElementClockwise(input);
25+
System.out.println("Input:");
26+
for(int i = 0 ; i < n; i++) {
27+
for(int j = 0 ; j < n; j++) {
28+
System.out.print(input[i][j] + " ");
29+
30+
}
31+
System.out.println(" ");
32+
}
33+
System.out.println("Output:");
34+
for(int i = 0 ; i < n; i++) {
35+
for(int j = 0 ; j < n; j++) {
36+
System.out.print(result[i][j] + " ");
37+
38+
}
39+
System.out.println(" ");
40+
}
41+
}
42+
}

0 commit comments

Comments
 (0)