Skip to content

Commit 16f4bef

Browse files
author
kwu130
committed
feat(src): add 22-generateParenthesis, 23-mergeKLists, 24-swapPairs, 25-reverseKGroup
1 parent d7c9c85 commit 16f4bef

4 files changed

Lines changed: 241 additions & 0 deletions

File tree

‎src/22-generateParenthesis.cpp‎

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
//
2+
// Created by 1412 on 2025/10/16.
3+
//
4+
5+
#include <bits/stdc++.h>
6+
7+
using namespace std;
8+
9+
class Solution {
10+
public:
11+
vector<string> generateParenthesis(int n) {
12+
vector<string> res;
13+
dfs(res, "", n, n);
14+
return res;
15+
}
16+
17+
private:
18+
void dfs(vector<string>& res, string&& path, int l, int r) {
19+
if (l == 0 && r == 0) {
20+
res.push_back(path);
21+
return;
22+
}
23+
if (l > 0) {
24+
path.push_back('(');
25+
dfs(res, std::move(path), l - 1, r);
26+
path.pop_back();
27+
}
28+
if (l < r) {
29+
path.push_back(')');
30+
dfs(res, std::move(path), l, r - 1);
31+
path.pop_back();
32+
}
33+
}
34+
};
35+
36+
void test() {
37+
Solution s;
38+
vector<string> expected = {"((()))", "(()())", "(())()", "()(())", "()()()"};
39+
assert(s.generateParenthesis(3) == expected);
40+
41+
expected = {"()"};
42+
assert(s.generateParenthesis(1) == expected);
43+
}
44+
45+
int main() {
46+
test();
47+
return 0;
48+
}

‎src/23-mergeKLists.cpp‎

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
//
2+
// Created by 1412 on 2025/10/16.
3+
//
4+
5+
#include <bits/stdc++.h>
6+
#include "../include/common.h"
7+
8+
using namespace std;
9+
using listHelper::ListNode;
10+
11+
class Solution {
12+
public:
13+
ListNode* mergeKLists(vector<ListNode*>& lists) {
14+
auto cmp = [](ListNode* lhs, ListNode* rhs) { return lhs->val > rhs->val; };
15+
priority_queue<ListNode*, vector<ListNode*>, decltype(cmp)> pq(cmp);
16+
for (auto list : lists) {
17+
if (list) {
18+
pq.push(list);
19+
}
20+
}
21+
22+
ListNode* dummy = new ListNode(-1);
23+
ListNode* cur = dummy;
24+
while (!pq.empty()) {
25+
auto node = pq.top();
26+
pq.pop();
27+
cur->next = node;
28+
cur = cur->next;
29+
if (node->next) {
30+
pq.push(node->next);
31+
}
32+
}
33+
34+
return dummy->next;
35+
}
36+
};
37+
38+
void test() {
39+
Solution s;
40+
ListNode* l1 = listHelper::build({1, 4, 5});
41+
ListNode* l2 = listHelper::build({1, 3, 4});
42+
ListNode* l3 = listHelper::build({2, 6});
43+
vector<ListNode*> lists = {l1, l2, l3};
44+
ListNode* expected = listHelper::build({1, 1, 2, 3, 4, 4, 5, 6});
45+
ListNode* res = s.mergeKLists(lists);
46+
assert(listHelper::equal(res, expected));
47+
listHelper::free(res);
48+
listHelper::free(expected);
49+
50+
lists = {};
51+
expected = listHelper::build({});
52+
res = s.mergeKLists(lists);
53+
assert(listHelper::equal(res, expected));
54+
listHelper::free(res);
55+
listHelper::free(expected);
56+
57+
lists = {nullptr};
58+
expected = listHelper::build({});
59+
res = s.mergeKLists(lists);
60+
assert(listHelper::equal(res, expected));
61+
listHelper::free(res);
62+
listHelper::free(expected);
63+
}
64+
65+
int main() {
66+
test();
67+
return 0;
68+
}

‎src/24-swapPairs.cpp‎

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
//
2+
// Created by 1412 on 2025/10/16.
3+
//
4+
5+
#include <bits/stdc++.h>
6+
#include "../include/common.h"
7+
8+
using namespace std;
9+
using listHelper::ListNode;
10+
11+
class Solution {
12+
public:
13+
ListNode* swapPairs(ListNode* head) {
14+
if (head == nullptr || head->next == nullptr) return head;
15+
ListNode* dummy = new ListNode(-1);
16+
dummy->next = head;
17+
ListNode *pre = dummy, *cur = head;
18+
while (cur != nullptr && cur->next != nullptr) {
19+
ListNode* next = cur->next;
20+
pre->next = next;
21+
cur->next = next->next;
22+
next->next = cur;
23+
pre = cur;
24+
cur = cur->next;
25+
}
26+
27+
return dummy->next;
28+
}
29+
};
30+
31+
void test() {
32+
Solution s;
33+
ListNode* l1 = listHelper::build({1, 2, 3, 4});
34+
ListNode* expected = listHelper::build({2, 1, 4, 3});
35+
ListNode* res = s.swapPairs(l1);
36+
assert(listHelper::equal(res, expected));
37+
listHelper::free(res);
38+
listHelper::free(expected);
39+
40+
l1 = listHelper::build({});
41+
expected = listHelper::build({});
42+
res = s.swapPairs(l1);
43+
assert(listHelper::equal(res, expected));
44+
listHelper::free(res);
45+
listHelper::free(expected);
46+
47+
l1 = listHelper::build({1});
48+
expected = listHelper::build({1});
49+
res = s.swapPairs(l1);
50+
assert(listHelper::equal(res, expected));
51+
listHelper::free(res);
52+
listHelper::free(expected);
53+
}
54+
55+
int main() {
56+
test();
57+
return 0;
58+
}

‎src/25-reverseKGroup.cpp‎

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
//
2+
// Created by 1412 on 2025/10/16.
3+
//
4+
5+
#include <bits/stdc++.h>
6+
#include "../include/common.h"
7+
8+
using namespace std;
9+
using listHelper::ListNode;
10+
11+
class Solution {
12+
public:
13+
ListNode* reverseKGroup(ListNode* head, int k) {
14+
if (head == nullptr || head->next == nullptr || k == 1) return head;
15+
ListNode* tail = head;
16+
for (int i = 0; i < k; i++) {
17+
// There are less than k nodes in the list, do nothing.
18+
if (tail == nullptr) return head;
19+
tail = tail->next;
20+
}
21+
ListNode* newHead = reverseInterval(head, tail);
22+
head->next = reverseKGroup(tail, k);
23+
return newHead;
24+
}
25+
26+
private:
27+
// reverse [head, tail)
28+
ListNode* reverseInterval(ListNode* head, ListNode* tail) {
29+
ListNode *pre = nullptr, *cur = head;
30+
while (cur != tail) {
31+
ListNode* next = cur->next;
32+
cur->next = pre;
33+
pre = cur;
34+
cur = next;
35+
}
36+
return pre;
37+
}
38+
};
39+
40+
void test() {
41+
Solution s;
42+
ListNode* l1 = listHelper::build({1, 2, 3, 4, 5});
43+
ListNode* expected = listHelper::build({2, 1, 4, 3, 5});
44+
ListNode* res = s.reverseKGroup(l1, 2);
45+
assert(listHelper::equal(res, expected));
46+
listHelper::free(res);
47+
listHelper::free(expected);
48+
49+
l1 = listHelper::build({1, 2, 3, 4, 5});
50+
expected = listHelper::build({3, 2, 1, 4, 5});
51+
res = s.reverseKGroup(l1, 3);
52+
assert(listHelper::equal(res, expected));
53+
listHelper::free(res);
54+
listHelper::free(expected);
55+
56+
l1 = listHelper::build({1, 2, 3, 4, 5});
57+
expected = listHelper::build({5, 4, 3, 2, 1});
58+
res = s.reverseKGroup(l1, 5);
59+
assert(listHelper::equal(res, expected));
60+
listHelper::free(res);
61+
listHelper::free(expected);
62+
}
63+
64+
int main() {
65+
test();
66+
return 0;
67+
}

0 commit comments

Comments
 (0)