-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathDiagonal Matrix
More file actions
55 lines (51 loc) · 1.38 KB
/
Diagonal Matrix
File metadata and controls
55 lines (51 loc) · 1.38 KB
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
code in java *******************************************
import java.util.Arrays;
public class solution {
public static void printInDiagonalForm(int[][] arr) {
boolean up = true;
int rowlen = arr.length;
int collen = arr[0].length;
int[] ans = new int[rowlen*collen];
int row = 0;
int col = 0;
int n = 0;
while(n<rowlen*collen){
ans[n] = arr[row][col];
++n;
if(up){
if(col==collen-1){
up = false;
++row;
continue;
}
if(row==0){
up = false;
++col;
continue;
}
//no flips
--row;
++col;
continue;
}else{
if(row==rowlen-1){
up = true;
++col;
continue;
}
if(col==0){
up = true;
++row;
continue;
}
//no flips
++row;
--col;
}
}
//return ans;
for(int i = 0; i < ans.length; i++){
System.out.print(ans[i] + " ");
}
}
}