leetcode刷题

刷题遇到的一些通用解决方法

Leetcode刷题

分割数组

长度为 n 的数组分成长度为 k 的子数组的问题

简单嵌套法

滑动窗口法

索引数组法

动态规划

KMP字符串匹配

寻路算法

A*算法

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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#include <vector>
#include <queue>
#include <cmath>
#include <algorithm>

struct Node {
int x, y;
int f, g, h; // f = g + h
Node* parent;

Node(int x, int y) : x(x), y(y), f(0), g(0), h(0), parent(nullptr) {}
};

struct CompareNode {
bool operator()(const Node* a, const Node* b) {
return a->f > b->f;
}
};

class AStar {
private:
std::vector<std::vector<int>> grid;
int rows, cols;
std::vector<std::pair<int, int>> directions{{-1,0}, {1,0}, {0,-1}, {0,1}};
public:
// 添加构造函数,创建空地图
AStar(int rows, int cols) : rows(rows), cols(cols) {
grid = std::vector<std::vector<int>>(rows, std::vector<int>(cols, 0));
}

// 添加设置障碍物的方法
void setObstacle(int x, int y) {
if (isValidPosition(x, y)) {
grid[x][y] = 1;
}
}

// 添加移除障碍物的方法
void removeObstacle(int x, int y) {
if (isValidPosition(x, y)) {
grid[x][y] = 0;
}
}

// 添加清除所有障碍物的方法
void clearObstacles() {
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
grid[i][j] = 0;
}
}
}
// 添加检查位置是否有障碍物的方法
bool hasObstacle(int x, int y) const {
if (isValidPosition(x, y)) {
return grid[x][y] == 1;
}
return true; // 地图外的位置视为障碍
}

private:
// 检查位置是否在地图范围内
bool isValidPosition(int x, int y) const {
return x >= 0 && x < rows && y >= 0 && y < cols;
}
public:
int calculateH(int x, int y, int endX, int endY) {
// 使用曼哈顿距离作为启发函数
return std::abs(x - endX) + std::abs(y - endY);
}

std::vector<std::pair<int, int>> findPath(int startX, int startY, int endX, int endY) {
std::priority_queue<Node*, std::vector<Node*>, CompareNode> openList;
std::vector<std::vector<bool>> closedList(rows, std::vector<bool>(cols, false));

Node* startNode = new Node(startX, startY);
openList.push(startNode);

while (!openList.empty()) {
Node* current = openList.top();
openList.pop();

if (current->x == endX && current->y == endY) {
return reconstructPath(current);
}

closedList[current->x][current->y] = true;

// 检查四个方向的相邻节点
for (const auto& dir : directions) {
int newX = current->x + dir.first;
int newY = current->y + dir.second;

if (newX < 0 || newX >= rows || newY < 0 || newY >= cols ||
grid[newX][newY] == 1 || closedList[newX][newY]) {
continue;
}

int newG = current->g + 1;
Node* neighbor = new Node(newX, newY);
neighbor->g = newG;
neighbor->h = calculateH(newX, newY, endX, endY);
neighbor->f = neighbor->g + neighbor->h;
neighbor->parent = current;

openList.push(neighbor);
}
}

return std::vector<std::pair<int, int>>(); // 没找到路径
}

private:
std::vector<std::pair<int, int>> reconstructPath(Node* endNode) {
std::vector<std::pair<int, int>> path;
Node* current = endNode;

while (current != nullptr) {
path.push_back({current->x, current->y});
current = current->parent;
}

std::reverse(path.begin(), path.end());
return path;
}
};