news 2026/4/21 0:36:31

博弈-翻转|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/4/19 21:15:28

Whisper.cpp语音识别:5步快速上手完整指南

Whisper.cpp语音识别&#xff1a;5步快速上手完整指南 【免费下载链接】whisper.cpp 项目地址: https://ai.gitcode.com/hf_mirrors/ai-gitcode/whisper.cpp 还在为语音转文字发愁吗&#xff1f;Whisper.cpp作为OpenAI Whisper模型的C实现版本&#xff0c;为你提供了高…

作者头像 李华
网站建设 2026/4/16 12:57:41

SOLIDWORKS材质库大全:终极免费资源让你的设计质感倍增 [特殊字符]

SOLIDWORKS材质库大全&#xff1a;终极免费资源让你的设计质感倍增 &#x1f680; 【免费下载链接】SOLIDWORKS材质库大全 SOLIDWORKS材质库大全为设计者提供了丰富的材质资源&#xff0c;扩展了标准库的选择范围。无论是机械设计、产品渲染还是仿真模拟&#xff0c;这些多样化…

作者头像 李华
网站建设 2026/4/18 13:21:24

如何快速使用GPTstudio:R语言AI编程的终极指南

如何快速使用GPTstudio&#xff1a;R语言AI编程的终极指南 【免费下载链接】gptstudio GPT RStudio addins that enable GPT assisted coding, writing & analysis 项目地址: https://gitcode.com/gh_mirrors/gp/gptstudio GPTstudio是一个专为R语言开发者设计的AI编…

作者头像 李华
网站建设 2026/4/18 12:53:09

5分钟极速上手:零基础文档信息抽取实战教程

5分钟极速上手&#xff1a;零基础文档信息抽取实战教程 【免费下载链接】Transformers-Tutorials This repository contains demos I made with the Transformers library by HuggingFace. 项目地址: https://gitcode.com/GitHub_Trending/tr/Transformers-Tutorials 还…

作者头像 李华
网站建设 2026/4/19 9:41:24

NutUI Vue3移动端组件库:京东风格多端适配完整指南

NutUI Vue3移动端组件库&#xff1a;京东风格多端适配完整指南 【免费下载链接】nutui 京东风格的移动端 Vue2、Vue3 组件库 、支持多端小程序(A Vue.js UI Toolkit for Mobile Web) 项目地址: https://gitcode.com/gh_mirrors/nu/nutui 在移动端开发日益复杂的今天&…

作者头像 李华
网站建设 2026/4/20 16:32:32

3步掌握YOLOv8 AI自瞄系统:从安装到实战的完整教程

3步掌握YOLOv8 AI自瞄系统&#xff1a;从安装到实战的完整教程 【免费下载链接】RookieAI_yolov8 基于yolov8实现的AI自瞄项目 项目地址: https://gitcode.com/gh_mirrors/ro/RookieAI_yolov8 基于YOLOv8深度学习算法的AI自瞄系统正在重新定义游戏辅助技术的标准。这款智…

作者头像 李华