-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path48. Rotate Image
69 lines (52 loc) · 1.29 KB
/
48. Rotate Image
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
Brute force approach
Runtime 0 ms
Beats 100%
Memory 41.5 MB
Beats 33.39%
class Solution {
public void rotate(int[][] old) {
int [][]current=new int[old.length][old[0].length];
for (int i = 0; i < old.length; i++)
for (int j = 0; j < old[0].length; j++)
current[i][j] = old[i][j] ;
int a=old.length-1;
for(int i=0;i<old.length;i++){
for(int j=0;j<old[i].length;j++){
old[j][a]=current[i][j];
}
a--;
}
}
}
Optizmed approach
Runtime 0 ms
Beats 100%
Memory 41.2 MB
Beats 79.25%
class Solution {
static void reverseArray(int [] arr)
{
int i=0;int j= arr.length-1;
while (i<j)
{
int temp =arr[i];
arr[i]=arr[j];
arr[j]=temp;
i++;
j--;
}
}
public void rotate(int[][] old) {
int temp=0;
for(int i=0;i<old.length-1;i++){
for(int j=i+1;j<old.length;j++){
temp=old[i][j];
old[i][j]=old[j][i];
old[j][i]=temp;
}
}
for(int i=0;i<old.length;i++){
reverseArray(old[i]);
}
}
}