-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution6.java
More file actions
executable file
·45 lines (45 loc) · 1.36 KB
/
Copy pathSolution6.java
File metadata and controls
executable file
·45 lines (45 loc) · 1.36 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
34
35
36
37
38
39
40
41
42
43
44
45
class Solution6 {
public String convert(String s, int numRows) {
int len, dim, cup, y;
String res ="";
len = s.length();
if (numRows == 1)
return s;
cup = numRows * 2 - 2;
dim = len / (numRows * 2 - 2);
if ((len - dim * cup) <= numRows)
dim = dim * (numRows - 1) + 1;
else
dim = dim * (numRows - 1) + 1 + (len - dim * cup - numRows);
char []sarray = s.toCharArray();
char [][]out = new char[numRows][dim];
for (int i=0; i<numRows; i++)
for (int j=0; j<dim; j++)
out[i][j] = '*';
for (int i=0,j=0; i<dim; i++){
if (j == len)
break;
if (i % (numRows - 1) == 0)
for(int k=0; k<numRows; k++){
out[k][i] = sarray[j];
j++;
if (j == len)
break;
}
else{
y = numRows - 1 - i % (numRows - 1);
out[y][i] = sarray[j];
j++;
if (j == len)
break;
}
}
for (int i=0; i<numRows; i++)
for (int j=0; j<dim; j++){
if (out[i][j] == '*')
continue;
res = res + out[i][j];
}
return res;
}
}