Skip to content

Commit 778cef6

Browse files
committed
[LeetCode]Zigzag_conversion
1 parent b043e7c commit 778cef6

File tree

1 file changed

+56
-0
lines changed

1 file changed

+56
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
Runtime 380 ms
3+
Beats 94.83%
4+
--------------
5+
Memory 160.4 MB
6+
Beats 43.10%
7+
*/
8+
9+
// import 'package:flutter_test/flutter_test.dart';
10+
11+
class Solution {
12+
late final int _sliceLen;
13+
14+
String convert(String s, int numRows) {
15+
// numRows 가 1인경우, 원본 스트링을 그대로 반환하면 된다.
16+
if (numRows == 1) return s;
17+
_sliceLen = numRows + (numRows - 2);
18+
List<List<String>> ans = List.generate(numRows, (index) => []);
19+
for (int i = 0; i < s.length; i = i + _sliceLen) {
20+
int endIdx = i + _sliceLen < s.length ? i + _sliceLen : s.length;
21+
_reorderString(ans, s.substring(i, endIdx), numRows);
22+
}
23+
24+
return ans.map((e) => e.join()).join();
25+
}
26+
27+
void _reorderString(List<List<String>> ans, String slicedString, int numRows) {
28+
for (int idx = 0; idx < slicedString.length; idx++) {
29+
if (idx == 0 || idx == numRows - 1 || idx < numRows) {
30+
ans[idx].add(slicedString[idx]);
31+
continue;
32+
}
33+
ans[_sliceLen - idx].add(slicedString[idx]);
34+
}
35+
}
36+
}
37+
38+
/*
39+
TEST_CODE
40+
--------
41+
void main() {
42+
group(
43+
"Zigzag Conversion",
44+
() {
45+
late Solution solution;
46+
setUp(() {
47+
solution = Solution();
48+
});
49+
50+
test('case01', () => expect(solution.convert("PAYPALISHIRING", 3), equals("PAHNAPLSIIGYIR")));
51+
test('case02', () => expect(solution.convert("PAYPALISHIRING", 4), equals("PINALSIGYAHRPI")));
52+
test('case03', () => expect(solution.convert("A", 1), equals("A")));
53+
},
54+
);
55+
}
56+
*/

0 commit comments

Comments
 (0)