-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathZigZag Conversion.java
More file actions
40 lines (29 loc) · 993 Bytes
/
Copy pathZigZag Conversion.java
File metadata and controls
40 lines (29 loc) · 993 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
34
35
36
37
38
39
40
import java.util.*;
class Solution {
public String convert(String s, int numRows) {
String output = "";
int depth = 1;
int index = 0;
if(numRows == 1 || s.length() <2){
return s;
}
while(s.length()>=depth){
output += Character.toString(s.charAt(index));
if(depth >1 && depth < numRows){
index += 2*(numRows-depth);
if(index < s.length()){
output += Character.toString(s.charAt(index));
}
index += 2*(depth-1);
}else{
index += 2*(numRows-1);
}
if(index>s.length()-1){
depth++;
if(depth>numRows)break;
index = depth-1;
}
}
return output;
}
}