Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit ef93bdc

Browse files
add
Change-Id: I57ba633bc949f310ab9a1adb764e6ef1acfe65ca
1 parent 307b2bc commit ef93bdc

6 files changed

+569
-0
lines changed
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/*
2+
* @lc app=leetcode.cn id=19 lang=cpp
3+
*
4+
* [19] 删除链表的倒数第 N 个结点
5+
*
6+
* https://leetcode.cn/problems/remove-nth-node-from-end-of-list/description/
7+
*
8+
* algorithms
9+
* Medium (45.01%)
10+
* Likes: 2376
11+
* Dislikes: 0
12+
* Total Accepted: 1M
13+
* Total Submissions: 2.3M
14+
* Testcase Example: '[1,2,3,4,5]\n2'
15+
*
16+
* 给你一个链表,删除链表的倒数第 n 个结点,并且返回链表的头结点。
17+
*
18+
*
19+
*
20+
* 示例 1:
21+
*
22+
*
23+
* 输入:head = [1,2,3,4,5], n = 2
24+
* 输出:[1,2,3,5]
25+
*
26+
*
27+
* 示例 2:
28+
*
29+
*
30+
* 输入:head = [1], n = 1
31+
* 输出:[]
32+
*
33+
*
34+
* 示例 3:
35+
*
36+
*
37+
* 输入:head = [1,2], n = 1
38+
* 输出:[1]
39+
*
40+
*
41+
*
42+
*
43+
* 提示:
44+
*
45+
*
46+
* 链表中结点的数目为 sz
47+
* 1 <= sz <= 30
48+
* 0 <= Node.val <= 100
49+
* 1 <= n <= sz
50+
*
51+
*
52+
*
53+
*
54+
* 进阶:你能尝试使用一趟扫描实现吗?
55+
*
56+
*/
57+
58+
// Definition for singly-linked list.
59+
struct ListNode {
60+
int val;
61+
ListNode *next;
62+
ListNode() : val(0), next(nullptr) {}
63+
ListNode(int x) : val(x), next(nullptr) {}
64+
ListNode(int x, ListNode *next) : val(x), next(next) {}
65+
};
66+
67+
// @lc code=start
68+
class Solution {
69+
public:
70+
ListNode *removeNthFromEnd(ListNode *head, int n) {
71+
ListNode dummy;
72+
dummy.next = head;
73+
ListNode *pre = &dummy;
74+
ListNode *cur = head;
75+
for (int i = 0; i < n; i++) {
76+
if (cur == nullptr) {
77+
return head;
78+
}
79+
cur = cur->next;
80+
}
81+
while (cur != nullptr) {
82+
pre = pre->next;
83+
cur = cur->next;
84+
}
85+
ListNode *tmp = pre->next;
86+
pre->next = tmp->next;
87+
tmp->next = nullptr;
88+
return dummy.next;
89+
}
90+
};
91+
// @lc code=end

‎cpp/leetcode/22.括号生成.cpp

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
/*
2+
* @lc app=leetcode.cn id=22 lang=cpp
3+
*
4+
* [22] 括号生成
5+
*
6+
* https://leetcode.cn/problems/generate-parentheses/description/
7+
*
8+
* algorithms
9+
* Medium (77.56%)
10+
* Likes: 3031
11+
* Dislikes: 0
12+
* Total Accepted: 639.4K
13+
* Total Submissions: 824.2K
14+
* Testcase Example: '3'
15+
*
16+
* 数字 n 代表生成括号的对数,请你设计一个函数,用于能够生成所有可能的并且
17+
* 有效的 括号组合。
18+
*
19+
*
20+
*
21+
* 示例 1:
22+
*
23+
*
24+
* 输入:n = 3
25+
* 输出:["((()))","(()())","(())()","()(())","()()()"]
26+
*
27+
*
28+
* 示例 2:
29+
*
30+
*
31+
* 输入:n = 1
32+
* 输出:["()"]
33+
*
34+
*
35+
*
36+
*
37+
* 提示:
38+
*
39+
*
40+
* 1 <= n <= 8
41+
*
42+
*
43+
*/
44+
45+
#include <string>
46+
#include <vector>
47+
48+
// @lc code=start
49+
class Solution {
50+
private:
51+
void helper(int left, int right, std::string &cur,
52+
std::vector<std::string> &result) {
53+
if (left > right) {
54+
return;
55+
}
56+
if (left == 0 && right == 0) {
57+
result.push_back(cur);
58+
return;
59+
}
60+
if (left > 0) {
61+
cur.push_back('(');
62+
helper(left - 1, right, cur, result);
63+
cur.pop_back();
64+
}
65+
if (right > 0) {
66+
cur.push_back(')');
67+
helper(left, right - 1, cur, result);
68+
cur.pop_back();
69+
}
70+
}
71+
72+
public:
73+
std::vector<std::string> generateParenthesis(int n) {
74+
std::vector<std::string> result;
75+
std::string cur;
76+
helper(n, n, cur, result);
77+
return result;
78+
}
79+
};
80+
// @lc code=end
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
/*
2+
* @lc app=leetcode.cn id=23 lang=cpp
3+
*
4+
* [23] 合并K个升序链表
5+
*
6+
* https://leetcode-cn.com/problems/merge-k-sorted-lists/description/
7+
*
8+
* algorithms
9+
* Hard (55.28%)
10+
* Likes: 1342
11+
* Dislikes: 0
12+
* Total Accepted: 268.7K
13+
* Total Submissions: 486K
14+
* Testcase Example: '[[1,4,5],[1,3,4],[2,6]]'
15+
*
16+
* 给你一个链表数组,每个链表都已经按升序排列。
17+
*
18+
* 请你将所有链表合并到一个升序链表中,返回合并后的链表。
19+
*
20+
*
21+
*
22+
* 示例 1:
23+
*
24+
* 输入:lists = [[1,4,5],[1,3,4],[2,6]]
25+
* 输出:[1,1,2,3,4,4,5,6]
26+
* 解释:链表数组如下:
27+
* [
28+
* ⁠ 1->4->5,
29+
* ⁠ 1->3->4,
30+
* ⁠ 2->6
31+
* ]
32+
* 将它们合并到一个有序链表中得到。
33+
* 1->1->2->3->4->4->5->6
34+
*
35+
*
36+
* 示例 2:
37+
*
38+
* 输入:lists = []
39+
* 输出:[]
40+
*
41+
*
42+
* 示例 3:
43+
*
44+
* 输入:lists = [[]]
45+
* 输出:[]
46+
*
47+
*
48+
*
49+
*
50+
* 提示:
51+
*
52+
*
53+
* k == lists.length
54+
* 0 <= k <= 10^4
55+
* 0 <= lists[i].length <= 500
56+
* -10^4 <= lists[i][j] <= 10^4
57+
* lists[i] 按 升序 排列
58+
* lists[i].length 的总和不超过 10^4
59+
*
60+
*
61+
*/
62+
63+
// Definition for singly-linked list.
64+
#include <queue>
65+
#include <vector>
66+
struct ListNode {
67+
int val;
68+
ListNode *next;
69+
ListNode() : val(0), next(nullptr) {}
70+
ListNode(int x) : val(x), next(nullptr) {}
71+
ListNode(int x, ListNode *next) : val(x), next(next) {}
72+
};
73+
74+
// @lc code=start
75+
class Solution {
76+
private:
77+
struct Element {
78+
ListNode *ptr;
79+
bool operator<(const Element &tar) const {
80+
return ptr->val > tar.ptr->val; // 小顶堆,升序排序用
81+
}
82+
};
83+
84+
public:
85+
ListNode *mergeKLists(std::vector<ListNode *> &lists) {
86+
ListNode dummy;
87+
ListNode *cur = &dummy;
88+
std::priority_queue<Element> queue;
89+
for (ListNode *node : lists) {
90+
if (node != nullptr) {
91+
queue.push({node});
92+
}
93+
}
94+
while (!queue.empty()) {
95+
auto el = queue.top();
96+
queue.pop();
97+
cur->next = el.ptr;
98+
cur = cur->next;
99+
if (el.ptr->next != nullptr) {
100+
queue.push({el.ptr->next});
101+
}
102+
}
103+
return dummy.next;
104+
}
105+
};
106+
// @lc code=end
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
/*
2+
* @lc app=leetcode.cn id=25 lang=cpp
3+
*
4+
* [25] K 个一组翻转链表
5+
*
6+
* https://leetcode.cn/problems/reverse-nodes-in-k-group/description/
7+
*
8+
* algorithms
9+
* Hard (67.74%)
10+
* Likes: 1907
11+
* Dislikes: 0
12+
* Total Accepted: 430.9K
13+
* Total Submissions: 636.1K
14+
* Testcase Example: '[1,2,3,4,5]\n2'
15+
*
16+
* 给你链表的头节点 head ,每 k 个节点一组进行翻转,请你返回修改后的链表。
17+
*
18+
* k
19+
* 是一个正整数,它的值小于或等于链表的长度。如果节点总数不是 k 的整数倍,那么请将最后剩余的节点保持原有顺序。
20+
*
21+
* 你不能只是单纯的改变节点内部的值,而是需要实际进行节点交换。
22+
*
23+
*
24+
*
25+
* 示例 1:
26+
*
27+
*
28+
* 输入:head = [1,2,3,4,5], k = 2
29+
* 输出:[2,1,4,3,5]
30+
*
31+
*
32+
* 示例 2:
33+
*
34+
*
35+
*
36+
*
37+
* 输入:head = [1,2,3,4,5], k = 3
38+
* 输出:[3,2,1,4,5]
39+
*
40+
*
41+
*
42+
* 提示:
43+
*
44+
*
45+
* 链表中的节点数目为 n
46+
* 1 <= k <= n <= 5000
47+
* 0 <= Node.val <= 1000
48+
*
49+
*
50+
*
51+
*
52+
* 进阶:你可以设计一个只用 O(1) 额外内存空间的算法解决此问题吗?
53+
*
54+
*
55+
*
56+
*
57+
*/
58+
59+
// Definition for singly-linked list.
60+
#include <sys/_types/_wint_t.h>
61+
struct ListNode {
62+
int val;
63+
ListNode *next;
64+
ListNode() : val(0), next(nullptr) {}
65+
ListNode(int x) : val(x), next(nullptr) {}
66+
ListNode(int x, ListNode *next) : val(x), next(next) {}
67+
};
68+
69+
// @lc code=start
70+
class Solution {
71+
public:
72+
ListNode *reverseRange(ListNode *start, ListNode *end) {
73+
ListNode *cur = start->next, *next;
74+
while (cur->next != nullptr && cur->next != end) {
75+
next = cur->next; // s sn --- c n nn --- e => s n sn --- c nn --- e
76+
cur->next = next->next;
77+
next->next = start->next;
78+
start->next = next;
79+
}
80+
return cur; // 下一个start
81+
}
82+
ListNode *reverseKGroup(ListNode *head, int k) {
83+
ListNode dummy;
84+
dummy.next = head;
85+
int i = 0;
86+
ListNode *start = &dummy, *end = dummy.next;
87+
while (end != nullptr) {
88+
end = end->next;
89+
i++;
90+
if (i >= k) {
91+
start = reverseRange(start, end);
92+
i = 0;
93+
}
94+
}
95+
96+
return dummy.next;
97+
}
98+
};
99+
// @lc code=end

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /