-
Notifications
You must be signed in to change notification settings - Fork 180
Expand file tree
/
Copy pathq006_ZigZagConversion.java
More file actions
90 lines (73 loc) · 2.57 KB
/
q006_ZigZagConversion.java
File metadata and controls
90 lines (73 loc) · 2.57 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package leetcode_algorithm;
/**
The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility)
P A H N
A P L S I I G
Y I R
And then read line by line: "PAHNAPLSIIGYIR"
*/
/**
n=numRows
Δ=2n-2 1 2n-1 4n-3
Δ= 2 2n-2 2n 4n-4 4n-2
Δ= 3 2n-3 2n+1 4n-5 .
Δ= . . . . .
Δ= . n+2 . 3n .
Δ= n-1 n+1 3n-3 3n-1 5n-5
Δ=2n-2 n 3n-2 5n-4
*/
public class q006_ZigZagConversion {
public static void main(String[] args) {
System.out.println(convert("PAYPALISHIRING" , 3));
System.out.println(convert2("PAYPALISHIRING" , 3));
}
/**
* 解法1(个人解法)
* @param s
* @param numRows
* @return
*/
public static String convert(String s, int numRows) {
if (numRows == 1 || s.length() <= numRows) {
return s;
}
StringBuilder sb = new StringBuilder();
for(int i = 0;i < numRows;i++) {
sb.append(s.charAt(i));
int index = 2*numRows -1;
while (index - 1 - i< s.length()){
if(i != numRows -1 ){
sb.append(s.charAt(index - 1 - i));
}
if(index - 1 + i < s.length() && i != 0){
sb.append(s.charAt(index -1 + i));
}
index = index + 2*numRows - 2;
}
}
return sb.toString();
}
/**
* 解法2
*
* @param s
* @param nRows
* @return
*/
public static String convert2(String s, int nRows) {
char[] c = s.toCharArray();
int len = c.length;
StringBuffer[] sb = new StringBuffer[nRows];
for (int i = 0; i < sb.length; i++) sb[i] = new StringBuffer();
int i = 0;
while (i < len) {
for (int idx = 0; idx < nRows && i < len; idx++) // vertically down
sb[idx].append(c[i++]);
for (int idx = nRows-2; idx >= 1 && i < len; idx--) // obliquely up
sb[idx].append(c[i++]);
}
for (int idx = 1; idx < sb.length; idx++)
sb[0].append(sb[idx]);
return sb[0].toString();
}
}