当前位置: 首页 > news >正文

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);
}
};

 

http://www.dtcms.com/a/284314.html

相关文章:

  • 第十八节:第三部分:java高级:反射-获取构造器对象并使用
  • 【参数初始化】——深度学习.全连接网络
  • Cadence SPB 2024软件下载及安装教程|Cadence软件安装详细步骤附下载链接
  • Spring IoCDI_2_使用篇
  • Anime.js 超级炫酷的网页动画库详解
  • 【图像质量评价指标】信噪比(Signal-to-Noise Ratio,SNR)
  • Linux 基础指令
  • makefile missing separator. Stop.问题
  • 一款实用的.NET Core加密解密工具类库
  • C++11之lambda表达式与包装器
  • 20.轮廓特征与近似,改变图像的轮廓识别画线的精确度,同时画出轮廓对应的矩形
  • GitHub 趋势日报 (2025年07月16日)
  • 【Burp入门第三十八篇】Repeater Strike:AI 驱动的 Burp Suite 插件
  • Effective Modern C++ 条款13:优先考虑const_iterator而非iterator
  • Magenta RT 正式开源!实时生成多种风格音乐,让创作无门槛
  • 【C++详解】STL-stack、queue的模拟实现,容器适配器,deque双端队列介绍
  • Java 大视界 -- Java 大数据在智能交通智能公交站台乘客流量预测与服务优化中的应用(349)
  • 19.删除链表的倒数第 N 个结点
  • 多线程--sem_wait(sem)特殊用法
  • 拿到安全工程师证后,能从事哪些岗位?
  • C函数实现strcopy strcat strcmp strstr
  • javax.servlet.http.HttpServletResponse;API导入报错解决方案
  • Kotlin集合与空值
  • 产品经理如何绘制流程图
  • Linux中的数据库操作基础
  • SpringMVC 执行原理
  • 79、【OS】【Nuttx】【启动】caller-saved 和 callee-saved 示例:r7 寄存器
  • Modbus
  • PyCharm2024安装包社区版和专业版
  • TESOLLO五指灵巧手遥操作解决方案