news 2026/1/22 20:10:19

博弈-翻转|hash<string>|smid

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
博弈-翻转|hash<string>|smid

lc267

回文排列

lc311

稀疏矩阵

预处理标记非0+二分

class Solution {
public:
vector<vector<int>> multiply(vector<vector<int>>& A, vector<vector<int>>& B) {
if (A.size() < 1 || B.size() < 1 || B[0].size() < 1) return {};
int m = A.size();
int n = A[0].size();
int k = B[0].size();
vector<vector<int>> a(m);
vector<vector<int>> b(k);
for (int i = 0; i < m; ++i) {
for (int j = 0; j < n; ++j) {
if (A[i][j] != 0) a[i].push_back(j);
}
}
for (int i = 0; i < n; ++i) {
for (int j = 0; j < k; ++j) {
if (B[i][j] != 0) b[j].push_back(i);
}
}
vector<vector<int>> res(m, vector<int>(k, 0));
for (int i = 0; i < m; ++i) {
for (int j = 0; j < k; ++j) {
if (a[i].size() < 1 || b[j].size() < 1) continue;
auto ai = a[i].begin();
auto bi = b[j].begin();
//int cur_max = 0;
int sum = 0;
while (ai != a[i].end() && bi != b[j].end()) {
//cur_max = max(*ai, *bi);
if (*ai == *bi) {
sum += A[i][*ai] * B[*bi][j];
ai++;
bi++;
}
else {
if (*ai > *bi) bi = lower_bound(bi, b[j].end(), *ai);
else ai = lower_bound(ai, a[i].end(), *bi);
}
}
res[i][j] = sum;
}
}
return res;
}
};

暴力

//行列 对应位置 的乘积 和
res[r][c] += (mat1[r][j] * mat2[j][c]);

class Solution
{
public:
vector<vector<int>> multiply(vector<vector<int>>& mat1, vector<vector<int>>& mat2)
{
int r1 = mat1.size(), r2 = mat2.size();
if (r1 == 0 || r2 == 0)
return vector<vector<int>>{};
int c1 = mat1[0].size(), c2 = mat2[0].size();

vector<vector<int>> res(r1, vector<int>(c2, 0));

for (int r = 0; r < r1; r ++)
{
for (int c = 0; c < c2; c ++)
{
for (int j = 0; j < c1; j ++)//行列 对应位置 的乘积 和
{
res[r][c] += (mat1[r][j] * mat2[j][c]);
}
}
}
return res;
}
};

simd写法

行列遍历+跳过零元素+并行transform

逐元素累加实现矩阵乘法,计算ans[i][j] = mat1[i][l]*mat2[l][j]总和。

#include <execution>
class Solution {
public:
vector<vector<int>> multiply(vector<vector<int>>& mat1, vector<vector<int>>& mat2) {
int n = mat1.size(), k = mat2.size(), m = mat2[0].size();
vector ans(n, vector(m, 0));
for (int i = 0; i < n; ++i) {
for (int l = 0; l < k; ++l) {
int val = mat1[i][l];
if (!val) continue;
transform(execution::unseq, mat2[l].begin(), mat2[l].end(), ans[i].begin(), ans[i].begin(), [val](auto&& a, auto&& b) {return b + val * a;});
// ranges::transform(mat2[l], ans[i], ans[i].begin(), [val](auto&& a, auto&& b) { return b + val * a; });
}
}
return ans;
}
};

lc294

博弈论 memo 翻转

hash包装器

size_t h = hash<string>()(cur);

笔记一下没找到,大概就是注意无冲突的情况下,可以使用hash<string>()包装string

class Solution {
unordered_map<size_t, bool> memo;
public:
bool canWin(string &cur) {
size_t h = hash<string>()(cur);
if (memo.count(h))
return memo[h];

for (int i = 1; i < cur.size(); i++)
{
if (cur[i] == '+' && cur[i - 1] == '+')
{
cur[i] = cur[i - 1] = '-';
bool ans = canWin(cur);
cur[i] = cur[i - 1] = '+';//回溯

if (!ans)
return memo[h]=true;
//下一个不存在答案 那么上一个就为true
//博弈论
}
}
return memo[h]=false;
}
};

lc156

从下到上 重连后 记得置空

tnode* dfs 在找最左节点的过程中重连

class Solution {

public:

TreeNode* upsideDownBinaryTree(TreeNode* root)

{

if(!root || !root->left) return root;

auto dfs=[&](this auto&& dfs,TreeNode* node)->TreeNode*

{

if(!node->left)

return node;

auto mxl=dfs(node->left);

node->left->left=node->right;

node->left->right=node;

//从下到上 连好了后置空

node->left=nullptr;

node->right=nullptr;

return mxl;

};

return dfs(root);

//最左边 节点为根

}

};

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/1/21 22:29:54

TIA博途虚拟机:三版本一体化自动化工程解决方案

TIA博途虚拟机&#xff1a;三版本一体化自动化工程解决方案 【免费下载链接】TIA博途虚拟机文件V17V16V15.1可直接使用 本仓库提供了一个TIA博途虚拟机文件&#xff0c;包含TIA Portal V17、V16和V15.1版本&#xff0c;用户可以直接使用这些虚拟机进行开发和测试。虚拟机文件已…

作者头像 李华
网站建设 2026/1/22 20:14:56

17、Puppet 4新特性与Hiera数据分离实践

Puppet 4新特性与Hiera数据分离实践 1. Puppet 4新特性 1.1 新风格与Ruby DSL的变化 Puppet 4引入了新的风格,例如: class syslog_ng {... } include syslog_ng同时,Puppet 4不再支持Ruby DSL。在之前,有人会将.rb文件作为清单放在模块中,这些.rb文件包含Ruby代码,主…

作者头像 李华
网站建设 2026/1/22 18:28:53

腾讯混元3D引擎:10秒生成专业级3D模型的终极解决方案

腾讯混元3D引擎&#xff1a;10秒生成专业级3D模型的终极解决方案 【免费下载链接】Hunyuan3D-1 项目地址: https://ai.gitcode.com/hf_mirrors/tencent/Hunyuan3D-1 在当今数字内容爆炸式增长的时代&#xff0c;腾讯混元3D引擎作为革命性的AI驱动3D内容生成工具&#x…

作者头像 李华
网站建设 2026/1/20 8:53:22

vscode-jest测试插件v5版本终极使用指南

vscode-jest测试插件v5版本终极使用指南 【免费下载链接】vscode-jest The optimal flow for Jest based testing in VS Code 项目地址: https://gitcode.com/gh_mirrors/vs/vscode-jest vscode-jest是Visual Studio Code中最强大的Jest集成测试工具&#xff0c;专为提升…

作者头像 李华
网站建设 2026/1/15 6:02:00

一致性模型:重新定义高效图像生成的AI技术

一致性模型&#xff1a;重新定义高效图像生成的AI技术 【免费下载链接】diffusers-ct_imagenet64 项目地址: https://ai.gitcode.com/hf_mirrors/openai/diffusers-ct_imagenet64 在生成式AI快速发展的今天&#xff0c;研究人员不断追求更高效的图像生成方案。一致性模…

作者头像 李华
网站建设 2026/1/21 13:20:50

抖音买单系统谁发明的?

抖音买单系统是我国著名聚合支付头部品牌“网付”于2025年10月15日发明的系统&#xff0c;抖音买单系统是基于抖音技术开放平台研发的第三方抖音买单系统。网付是发明抖音买单系统的开山鼻祖。网付研发系统不仅支持抖音买单&#xff0c;还支持支付宝支付、微信支付、云闪付、数…

作者头像 李华