-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution1.java
More file actions
59 lines (47 loc) · 1.15 KB
/
Solution1.java
File metadata and controls
59 lines (47 loc) · 1.15 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/**
* code by : 老王
* https://github.com/simplemain
*/
import java.util.Arrays;
public class Solution1
{
public static void main(String[] args)
{
final String s = "PAYPALISHIRING";
final int n = 4;
final Solution sol = new Solution1().new Solution();
final String result = sol.convert(s, n);
System.out.printf("%s\n", result);
}
class Solution
{
public String convert(String s, int n)
{
if (n == 1) return s;
final int maxHeight = Math.min(s.length(), n);
final int maxWidth = s.length();
final char[][] matrix = new char[maxHeight][];
for (int i = 0; i < matrix.length; i++) matrix[i] = new char[maxWidth];
final int[] pos = new int[maxHeight];
Arrays.fill(pos, 0);
for (int rowNum = 0, direct = 1, p = 0; p < s.length(); p++, rowNum += direct)
{
if (rowNum >= n || rowNum < 0)
{
direct *= -1;
rowNum += 2 * direct;
}
matrix[rowNum][pos[rowNum]++] = s.charAt(p);
}
final char[] res = new char[s.length()];
for (int i = 0, p = 0; i < maxHeight; i++)
{
for (int j = 0; j < pos[i]; j++)
{
res[p++] = matrix[i][j];
}
}
return new String(res);
}
}
}