Skip to content

Commit b369d12

Browse files
authored
Update 006_ZigZag_conversion.java (#77)
* Update 006_ZigZag_conversion.java Contributed by @green-study
1 parent 9c0ddc4 commit b369d12

File tree

1 file changed

+33
-1
lines changed

1 file changed

+33
-1
lines changed

java/006_ZigZag_Conversion.java

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,33 @@
1-
//TODO
1+
//006_ZigZag_Conversion.java
2+
class Solution {
3+
public String convert(String s, int numRows) {
4+
if(numRows==1) {
5+
return s;
6+
}
7+
8+
String answer = "";
9+
String[] str_array = new String[numRows];
10+
11+
for(int i=0;i<numRows;i++){
12+
str_array[i]="";
13+
}
14+
15+
int mod = 2*numRows-2;
16+
17+
for(int i=0;i<s.length();i++){
18+
char c = s.charAt(i);
19+
int index = i%mod;
20+
if(index >= numRows) {
21+
index = 2*(numRows-1) - index;
22+
}
23+
str_array[index]+=c;
24+
}
25+
26+
for(int i=0;i<numRows;i++){
27+
answer += str_array[i];
28+
}
29+
30+
return answer;
31+
}
32+
}
33+

0 commit comments

Comments
 (0)