合并两个有序链表
分析:
设置一个前哨结点prev,prev始终指向L1和L2中较小的节点,这样就能依次将节点按照从小到大的顺序串起来。
代码:
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution {
public:
ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
ListNode* s = new ListNode(-1);
ListNode* prev = s;
while(l1 != nullptr && l2 != nullptr) {
if(l1->val < l2->val) {
prev->next = l1;
l1 = l1->next;
}else {
prev->next = l2;
l2 = l2->next;
}
prev = prev->next;
}
prev->next = l1 == nullptr ? l2 : l1;
return s->next;
}
};
反转链表
分析:
笨办法:先遍历一遍链表并保存下所有的节点值,接着逆序串在一起。
代码:
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution {
public:
ListNode* reverseList(ListNode* head) {
if(!head) {
return nullptr;
}
ListNode* ptr = head;
vector<int> d;
while(ptr) {
d.push_back(ptr->val);
ptr = ptr->next;
}
int n = d.size();
ListNode* wail = new ListNode(d[n - 1]);
ListNode* p = wail;
for(int i = n - 2; i >= 0; i--) {
ListNode* temp = new ListNode(d[i]);
p->next = temp;
p = p->next;
}
p->next = nullptr;
return wail;
}
};
01矩阵
分析:
多源bfs:先将矩阵中所有0的坐标入队列,同时记录所有位置到0的距离(初始为0);接着采用传统的BFS方法,入队时将距离加1即可。
代码:
class Solution {
private:
static constexpr int dirs[4][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};
public:
vector<vector<int>> updateMatrix(vector<vector<int>>& matrix) {
int m = matrix.size(), n = matrix[0].size();
vector<vector<int>> dist(m, vector<int>(n));
vector<vector<int>> seen(m, vector<int>(n));
queue<pair<int, int>> q;
// 将所有的 0 添加进初始队列中
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
if (matrix[i][j] == 0) {
q.emplace(i, j);
seen[i][j] = 1;
}
}
}
// 广度优先搜索
while (!q.empty()) {
auto [i, j] = q.front();
q.pop();
for (int d = 0; d < 4; ++d) {
int ni = i + dirs[d][0];
int nj = j + dirs[d][1];
if (ni >= 0 && ni < m && nj >= 0 && nj < n && !seen[ni][nj]) {
dist[ni][nj] = dist[i][j] + 1;
q.emplace(ni, nj);
seen[ni][nj] = 1;
}
}
}
return dist;
}
};
爬楼梯
分析:
典型的动态规划例题:f[i] = f[i-1] + f[i - 2]。
代码:
class Solution {
public:
int climbStairs(int n) {
int x = 1, y = 2, z;
if(n == 1) {
return 1;
}
if(n == 2) {
return 2;
}
for(int i = 3; i <= n; i++) {
z = x + y;
x = y;
y = z;
}
return z;
}
};
腐烂的橘子
分析:
多源bfs,同上。
代码:
class Solution {
public:
int orangesRotting(vector<vector<int>>& grid) {
int dirs[4][2] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
queue<pair<int, int>> que;
int cnt = 0;
int m = grid.size(), n = grid[0].size();
vector<vector<int>> seen(m, vector<int>(n));
vector<vector<int>> dist(m, vector<int>(n));
for(int i = 0; i < m; i++) {
for(int j = 0; j < n; j++) {
if(grid[i][j] == 2) {
que.emplace(i, j);
seen[i][j] = 1;
}else if(grid[i][j] == 1) {
cnt++;
}
}
}
int ans = 0;
while(!que.empty()) {
auto fr = que.front();
que.pop();
for(int i = 0; i < 4; i++) {
int x = fr.first + dirs[i][0];
int y = fr.second + dirs[i][1];
if(x >= 0 && x < m && y >= 0 && y < n && !seen[x][y]) {
seen[x][y] = 1;
dist[x][y] = dist[fr.first][fr.second] + 1;
if(grid[x][y] == 1) {
que.emplace(x, y);
cnt--;
ans = dist[x][y];
}
}
}
}
return cnt ? -1 : ans;
}
};