반응형
leetcode.com/problems/zigzag-conversion/
Solution
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
|
struct NODE {
char c;
struct NODE* next;
};
struct NODE head[1000];
struct NODE* last[1000];
int cur;
int dir;
void add_node(char c, int cur) {
struct NODE* node = (struct NODE*)malloc(sizeof(struct NODE));
node->c = c;
node->next = 0;
last[cur]->next = node;
last[cur] = node;
}
void init() {
cur = 0;
dir = 1;
for (int i = 0; i < 1000; i++) {
head[i].next = 0;
last[i] = &head[i];
}
}
char* convert(char* s, int numRows) {
init();
int s_cnt = 1;
while (*s) {
add_node(*s, cur);
cur += dir;
if (cur == numRows || cur == -1) {
dir *= -1;
cur += dir*2;
if (cur == -1) cur = 0;
if (cur == numRows) cur = numRows - 1;
}
s++;
s_cnt++;
}
char* rst = (char*)malloc(sizeof(char) * s_cnt);
s_cnt = 0;
for (int i = 0; i < numRows; i++) {
struct NODE* node = head[i].next;
while (node) {
rst[s_cnt++] = node->c;
node = node->next;
}
}
rst[s_cnt] = 0;
return rst;
}
|
cs |
반응형
'코딩테스트 > Leetcode' 카테고리의 다른 글
Leetcode - [Easy]14. Longest Common Prefix (0) | 2021.04.04 |
---|---|
Leetcode - [Easy]14. Longest Common Prefix (0) | 2021.03.22 |
Leetcode - [Easy]13. Roman to Integer (0) | 2021.03.14 |
Leetcode - [Medium]3. Longest Substring Without Repeating Characters (0) | 2021.03.07 |
Leetcode - [Medium]2. Add Two Numbers (0) | 2021.03.04 |