-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathZigZagConversion.java
More file actions
33 lines (33 loc) · 1.13 KB
/
ZigZagConversion.java
File metadata and controls
33 lines (33 loc) · 1.13 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
class Solution {
public String convert(String s, int numRows) {
// Simple Simulation on Matrix Zig Zagly and first loop mein col ko constant rkho second loop ke andr column to increase krte jao because of zig zag motion
if (numRows == 1) {
return s;
}
String[][] matrix = new String[numRows][s.length()];
int pointer = 0;
int col = 0;
while (pointer < s.length()) {
for (int i = 0; i < numRows && pointer < s.length(); i++) {
matrix[i][col] = String.valueOf(s.charAt(pointer));
pointer++;
}
col++;
for (int i = numRows - 2; i > 0 && pointer < s.length(); i--) {
matrix[i][col] = String.valueOf(s.charAt(pointer));
pointer++;
col++;
}
}
StringBuilder sb = new StringBuilder();
for (String[] mat : matrix) {
for (String str : mat) {
if (str == null) {
continue;
}
sb.append(str);
}
}
return sb.toString();
}
}