-
-
Notifications
You must be signed in to change notification settings - Fork 291
Expand file tree
/
Copy path6.java
More file actions
135 lines (124 loc) · 4.45 KB
/
6.java
File metadata and controls
135 lines (124 loc) · 4.45 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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
__________________________________________________________________________________________________
2ms
class Solution {
public String convert(String s, int nRows) {
int length = s.length();
if (length <= nRows || nRows == 1) return s;
char[] chars = new char[length];
int step = 2 * (nRows - 1);
int count = 0;
for (int i = 0; i < nRows; i++){
int interval = step - 2 * i;
for (int j = i; j < length; j += step){
chars[count] = s.charAt(j);
count++;
if (interval < step && interval > 0
&& j + interval < length && count < length){
chars[count] = s.charAt(j + interval);
count++;
}
}
}
return new String(chars);
}
}
__________________________________________________________________________________________________
3sm
class Solution {
public String convert(String s, int numRows) {
if (s == null || s.length() < 2 || numRows < 2 || numRows >= s.length()) return s;
int len = s.length();
StringBuilder sb = new StringBuilder();
int curPos, skipDownChars, skipUpChars;
for (int i = 0; i < numRows; i++) { // Go by each level
curPos = i;
boolean goingDown = true;
skipDownChars = 2 * (numRows - 1 - i);
skipUpChars = 2 * i;
while (curPos < len) {
if (goingDown && skipDownChars > 0) {
sb.append(s.charAt(curPos));
curPos += skipDownChars;
} else if (!goingDown && skipUpChars > 0){
sb.append(s.charAt(curPos));
curPos += skipUpChars;
}
goingDown = !goingDown;
}
}
return sb.toString();
}
}
__________________________________________________________________________________________________
4ms
class Solution {
public String convert(String s, int numRows) {
if (numRows <= 1)
return s;
StringBuilder result = new StringBuilder();
int pos = 0;
while (pos < s.length()) {
result.append(s.charAt(pos));
pos += 2 * (numRows - 1);
}
for (int i = 1; i < numRows - 1; i++) {
pos = 0;
boolean down = true;
while (pos < s.length()) {
if (down && pos + i < s.length())
result.append(s.charAt(pos + i));
if (!down && pos + numRows - 1 - i < s.length())
result.append(s.charAt(pos + numRows - 1 - i));
pos += numRows - 1;
down = !down;
}
}
pos = numRows - 1;
while (pos < s.length()) {
result.append(s.charAt(pos));
pos += 2 * (numRows - 1);
}
return result.toString();
}
}
__________________________________________________________________________________________________
36328 kb
class Solution {
public String convert(String s, int numRows) {
if (numRows == 1) return s;
List<StringBuilder> rows = new ArrayList<>();
for (int i = 0; i < Math.min(numRows, s.length()); i++)
rows.add(new StringBuilder());
int curRow = 0;
boolean goingDown = false;
for (char c : s.toCharArray()) {
rows.get(curRow).append(c);
if (curRow == 0 || curRow == numRows - 1) goingDown = !goingDown;
curRow += goingDown ? 1 : -1;
}
StringBuilder ret = new StringBuilder();
for (StringBuilder row : rows) ret.append(row);
return ret.toString();
}
}
__________________________________________________________________________________________________
36572 kb
class Solution {
public String convert(String s, int numRows) {
//mathematic
if(numRows == 1)
return s;
StringBuilder res = new StringBuilder();
int n = s.length();
int len = 2 * numRows - 2;
for(int i = 0; i < numRows; i++) {
for(int j = 0; i + j < n; j += len) {
res.append(s.charAt(i + j));
if(i != 0 && i != numRows - 1 && i + j + 2 * (numRows - i - 1) < n)
res.append(s.charAt(i + j + 2 * (numRows - i - 1)));
}
}
return res.toString();
}
}
__________________________________________________________________________________________________