7.18 note
lc3417. 之字形
i的奇偶行,控制j的正逆遍历
class Solution {
public:
vector<int> zigzagTraversal(vector<vector<int>>& grid) {
int n = grid[0].size();
int end = n - 1 - n % 2;
vector<int> ans;
for (int i = 0; i < grid.size(); i++) {
if (i % 2) {
for (int j = end; j >= 0; j -= 2) {
ans.push_back(grid[i][j]);
}
} else {
for (int j = 0; j < n; j += 2) {
ans.push_back(grid[i][j]);
}
}
}
return ans;
}
};
lcr186.筛子概率
初始
#include <vector>
using namespace std;
class Solution {
public:
vector<double> statisticsProbability(int num) {
vector<vector<double>> dp(num + 1, vector<double>(6 * num + 1, 0));
for (int j = 1; j <= 6; ++j) {
dp[1][j] = 1.0 / 6.0;
}
for (int i = 2; i <= num; ++i) {
for (int j = i; j <= 6 * i; ++j) {
for (int k = 1; k <= 6; ++k) {
if (j - k >= i - 1) {
dp[i][j] += dp[i - 1][j - k] / 6.0;
}
}
}
}
return vector<double>(dp[num].begin() + num, dp[num].end());
}
};
优化
class Solution {
public:
vector<double> statisticsProbability(int num) {
vector<double> dp(6, 1.0 / 6.0);
for (int i = 2; i <= num; i++) {
vector<double> tmp(5 * i + 1, 0);
for (int j = 0; j < dp.size(); j++) {
for (int k = 0; k < 6; k++) {
tmp[j + k] += dp[j] / 6.0;
}
}
dp = tmp;
}
return dp;
}
};
卷积法
#include <vector>
#include <torch/torch.h>
using namespace std;
using namespace torch;
class Solution {
public:
vector<float> dicesProbability(int n) {
// 初始化单个骰子的概率分布
Tensor single_dice_prob = torch::full({1, 1, 6}, 1.0 / 6.0);
// 初始情况,只有一个骰子
Tensor n_dice_prob = torch::full({1, 1, 6}, 1.0 / 6.0);
for (int i = 0; i < n - 1; ++i) {
// 更新概率分布(卷积实现多项式乘法)
n_dice_prob = torch::conv1d(
n_dice_prob,
single_dice_prob,
{}, // 无偏置
1, // 步长
5 // padding
).view({1, 1, -1});
}
// 转换为vector<float>返回
return vector<float>(
n_dice_prob.data_ptr<float>(),
n_dice_prob.data_ptr<float>() + n_dice_prob.numel()
);
}
};
lc332.欧拉图
C++的multiset是能存多个相同元素、自动排序且方便查找删除的集合。
class Solution {
public:
vector<string> findItinerary(vector<vector<string>>& tickets) {
vector<string> res = {"JFK"};
unordered_map<string, vector<string>> map;
// 构建邻接表
for (auto& ticket : tickets) {
string from = ticket[0], to = ticket[1];
if (map.find(from) == map.end()) {
map[from] = vector<string>();
}
map[from].push_back(to);
}
// 对每个城市的可达城市排序
for (auto& pair : map) {
sort(pair.second.begin(), pair.second.end());
}
// 深度优先搜索
function<bool(string, int)> dfs = [&](string city, int used) {
if (used == tickets.size()) {
return true;
}
if (map.find(city) == map.end()) {
return false;
}
vector<string>& nextCities = map[city];
for (int i = 0; i < nextCities.size(); ++i) {
string next = nextCities[i];
// 移除当前选择的城市
nextCities.erase(nextCities.begin() + i);
res.push_back(next);
if (dfs(next, used + 1)) {
return true;
}
// 回溯:恢复城市和路径
res.pop_back();
nextCities.insert(nextCities.begin() + i, next);
}
return false;
};
dfs("JFK", 0);
return res;
}
};
class Solution {
unordered_map<string, multiset<string>> g;
vector<string> ret;
public:
vector<string> findItinerary(vector<vector<string>>& tickets) {
for (auto& t : tickets) {
g[t[0]].insert(t[1]);
}
dfs("JFK");
reverse(ret.begin(), ret.end());
return ret;
}
void dfs(string b)
{
while (!g[b].empty())
{
string next = *g[b].begin();
g[b].erase(g[b].begin());
dfs(next);
}
//放最后,合理的时候return
ret.push_back(b);
}
};