forked from Google-DSC-TMSL/ProjectAlgorithms
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathzigzagconversion.java
More file actions
Latest commit
33 lines (30 loc) · 1000 Bytes
/
Copy pathzigzagconversion.java
File metadata and controls
33 lines (30 loc) · 1000 Bytes
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
classSolution {
publicStringconvert(Strings, intnumRows) {
//Define StringBuilders
StringBuilder[] sbs = newStringBuilder[numRows];
for(inti = 0; i < numRows; i++){
sbs[i] = newStringBuilder();
}
//Define Variables
char[] arr = s.toCharArray();
intn = arr.length;
intindex = 0;
//Traverse zig zag
while(index < n){
//Go down
for(intj = 0; j < numRows && index < n; j++){
sbs[j].append(arr[index++]);
}
//Go Up before start
for(intj = numRows - 2; j > 0 && index < n; j--){
sbs[j].append(arr[index++]);
}
}
//Combine all stringbuilders into one
StringBuilderres = sbs[0];
for(inti = 1; i < numRows; i++){
res.append(sbs[i].toString());
}
returnres.toString();
}
}