-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathzigzag.java
More file actions
37 lines (27 loc) · 815 Bytes
/
zigzag.java
File metadata and controls
37 lines (27 loc) · 815 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
public class zigzag {
public static void main(String[] args){
String hehe = convert("PAYPALISHIRING",3);
System.out.println(hehe);
}
// Leetcode Online Judge Accepted
public static String convert(String s, int nRows) {
if (nRows <=1) return s;
char[] schar = s.toCharArray();
String[] sstring = new String[nRows];
for(int j =0; j<sstring.length; j++){
sstring[j] = "";
}
for(int i =0; i<schar.length; i++){
int rowno = i%(2*nRows - 2);
if(rowno >= nRows){
rowno = 2*nRows - rowno-2;
}
sstring[rowno] += schar[i];
}
String all ="";
for(int t =0; t < sstring.length; t++){
all += sstring[t];
}
return all;
}
}