This repository was archived by the owner on Feb 4, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy path6.java
More file actions
44 lines (39 loc) · 1.21 KB
/
6.java
File metadata and controls
44 lines (39 loc) · 1.21 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
//https://leetcode.com/problems/zigzag-conversion/
//https://1ilsang.blog.me/221585391817
class Solution {
public String convert(String s, int numRows) {
String ret = "";
boolean flag = true;
int cnt = 0;
Node[] list = new Node[numRows];
for(int i = 0 ; i < list.length; i++) list[i] = new Node('h');
for(int i = 0 ; i < s.length(); i++) {
Node dump = list[cnt];
while(dump.next != null) dump = dump.next;
dump.next = new Node(s.charAt(i));
cnt += flag ? 1 : -1;
if(cnt == numRows) {
flag = false;
cnt = cnt - 2 >= 0 ? cnt - 2 : 0;
} else if(cnt == -1) {
flag = true;
cnt = cnt + 2 < numRows ? cnt + 2 : numRows - 1;
}
}
for(int i = 0; i < numRows; i++) {
list[i] = list[i].next;
while(list[i] != null) {
ret += list[i].str;
list[i] = list[i].next;
}
}
return ret;
}
private class Node {
char str;
Node next;
Node(char str) {
this.str = str;
}
}
}