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 e72d71c

Browse files
github-actionsgithub-actions
github-actions
authored and
github-actions
committed
Formatted with Google Java Formatter
1 parent dc2d3d3 commit e72d71c

File tree

1 file changed

+41
-52
lines changed

1 file changed

+41
-52
lines changed

‎Others/RotateMatriceBy90Degree.java

Lines changed: 41 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,71 +1,60 @@
11
package Others;
22

33
/**
4-
* Given a matrix of size n x n
5-
* We have to rotate this matrix by 90 Degree
6-
* Here is the algorithm for this problem .
7-
*
4+
* Given a matrix of size n x n We have to rotate this matrix by 90 Degree Here is the algorithm for
5+
* this problem .
86
*/
9-
107
import java.util.*;
118

129
class Rotate_by_90_degree {
13-
public static void main(String[] args) {
14-
Scanner sc = new Scanner(System.in);
15-
int t = sc.nextInt();
16-
17-
while (t-- > 0) {
18-
int n = sc.nextInt();
19-
int[][] arr = new int[n][n];
10+
public static void main(String[] args) {
11+
Scanner sc = new Scanner(System.in);
12+
int t = sc.nextInt();
2013

21-
for (inti = 0; i < n; i++)
22-
for (int j = 0; j < n; j++)
23-
arr[i][j] = sc.nextInt();
14+
while (t-- > 0) {
15+
int n = sc.nextInt();
16+
int[][] arr = newint[n][n];
2417

25-
Rotate g = new Rotate();
26-
g.rotate(arr);
27-
printMatrix(arr);
18+
for (int i = 0; i < n; i++) for (int j = 0; j < n; j++) arr[i][j] = sc.nextInt();
2819

29-
}
30-
sc.close();
20+
Rotate g = new Rotate();
21+
g.rotate(arr);
22+
printMatrix(arr);
3123
}
24+
sc.close();
25+
}
3226

33-
static void printMatrix(int arr[][]) {
34-
for (int i = 0; i < arr.length; i++) {
35-
for (int j = 0; j < arr[0].length; j++)
36-
System.out.print(arr[i][j] + " ");
37-
System.out.println("");
38-
}
27+
static void printMatrix(int arr[][]) {
28+
for (int i = 0; i < arr.length; i++) {
29+
for (int j = 0; j < arr[0].length; j++) System.out.print(arr[i][j] + " ");
30+
System.out.println("");
3931
}
32+
}
4033
}
4134

42-
/**
43-
* Class containing the algo to roate matrix by 90 degree
44-
*/
45-
35+
/** Class containing the algo to roate matrix by 90 degree */
4636
class Rotate {
47-
static void rotate(int a[][]) {
48-
int n = a.length;
49-
for (int i = 0; i < n; i++) {
50-
for (int j = 0; j < n; j++) {
51-
if (i > j) {
52-
int temp = a[i][j];
53-
a[i][j] = a[j][i];
54-
a[j][i] = temp;
55-
}
56-
}
57-
}
58-
int i = 0, k = n - 1;
59-
while (i < k) {
60-
for (int j = 0; j < n; j++) {
61-
int temp = a[i][j];
62-
a[i][j] = a[k][j];
63-
a[k][j] = temp;
64-
}
65-
66-
i++;
67-
k--;
37+
static void rotate(int a[][]) {
38+
int n = a.length;
39+
for (int i = 0; i < n; i++) {
40+
for (int j = 0; j < n; j++) {
41+
if (i > j) {
42+
int temp = a[i][j];
43+
a[i][j] = a[j][i];
44+
a[j][i] = temp;
6845
}
69-
46+
}
47+
}
48+
int i = 0, k = n - 1;
49+
while (i < k) {
50+
for (int j = 0; j < n; j++) {
51+
int temp = a[i][j];
52+
a[i][j] = a[k][j];
53+
a[k][j] = temp;
54+
}
55+
56+
i++;
57+
k--;
7058
}
59+
}
7160
}

0 commit comments

Comments
(0)

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