图论第三天|127. 单词接龙 841.钥匙和房间 463. 岛屿的周长 1971. 寻找图中是否存在路径 684.冗余连接 685.冗余连接II

目录

  • Leetcode127. 单词接龙
  • Leetcode841.钥匙和房间
  • Leetcode463. 岛屿的周长
  • Leetcode1971. 寻找图中是否存在路径
  • Leetcode684.冗余连接
  • Leetcode685.冗余连接II

Leetcode127. 单词接龙

文章链接:代码随想录
题目链接:127. 单词接龙

思路:广搜搜出来直接就是最短路径,深搜还需要判断;广搜相当于先把这一层路径的单词下一步走法都扫出来再走下一步;而深搜找到一条路径就先走到头,再返回来走下一条路径,需要判断路径长度,麻烦
另外需要标记位,wordMap,避免死循环

class Solution {
public:int ladderLength(string beginWord, string endWord, vector<string>& wordList) {unordered_set<string> wordSet(wordList.begin(), wordList.end());if (wordSet.find(endWord) == wordSet.end()) return 0;unordered_map<string, int> wordMap;wordMap.insert(pair<string, int>(beginWord, 1));queue<string> que;que.push(beginWord);while(!que.empty()){string word = que.front();que.pop();int path = wordMap[word];for (int i = 0; i < word.size(); i++){string newword = word;for (int j = 0; j < 26; j++){newword[i] = j + 'a';if (newword == endWord) return path + 1;if (wordSet.find(newword) != wordSet.end() && wordMap.find(newword) == wordMap.end()) {wordMap.insert(pair<string, int>(newword, path + 1));que.push(newword);}}}}return 0;}
};

Leetcode841.钥匙和房间

文章链接:代码随想录
题目链接:841.钥匙和房间

思路:dfs

class Solution {
public:void dfs(vector<vector<int>>& rooms, vector<bool>& visited, int key){if (visited[key]) return;visited[key] = true;for (int i : rooms[key]){dfs(rooms, visited, i);}}bool canVisitAllRooms(vector<vector<int>>& rooms) {vector<bool> visited(rooms.size(), false);dfs(rooms, visited, 0);for(int i : visited){if (i == false) return false;}return true;}
};

Leetcode463. 岛屿的周长

文章链接:代码随想录
题目链接:463. 岛屿的周长

思路:不用深搜或广搜,遍历就好,不要想复杂。

class Solution {
public:int count = 0;int dir[4][2] = {1, 0, -1, 0, 0, 1, 0, -1};    int islandPerimeter(vector<vector<int>>& grid) {int m = grid.size();int n = grid[0].size();for (int i = 0; i < m; i++){for (int j = 0; j < n; j++){if (grid[i][j] == 1){for (int k = 0; k < 4; k++){int nex = i + dir[k][0];int ney = j + dir[k][1];if (nex < 0 || nex >= grid.size() || ney < 0 || ney >= grid[0].size() || grid[nex][ney] == 0){count++;}}}}}return count;}
};

Leetcode1971. 寻找图中是否存在路径

文章链接:代码随想录
题目链接:1971. 寻找图中是否存在路径

思路:并查集入门

class Solution {
private:int n = 200005;vector<int> father = vector<int> (n);void init(){for (int i = 0; i < n; i++) father[i] = i;}int find(int u){return u == father[u] ? u : father[u] = find(father[u]);}bool isSame(int u, int v){u = find(u);v = find(v);return u == v;}void join(int u, int v){u = find(u);v = find(v);if (u == v) return ;father[v] = u;}public:bool validPath(int n, vector<vector<int>>& edges, int source, int destination) {init();for (int i = 0; i < edges.size(); i++){join(edges[i][0], edges[i][1]);}return isSame(source, destination);}
};

Leetcode684.冗余连接

文章链接:代码随想录
题目链接:684.冗余连接

思路:并查集入门,用于解决无向有环图问题

class Solution {
private:int n = 1005;vector<int> father = vector<int>(n);void init(){for (int i = 0; i < n; i++){father[i] = i;}}int find (int u){return u == father[u] ? u : father[u] = find(father[u]);}bool isSame(int u, int v){u = find(u);v = find(v);return u == v;}void join(int u, int v){u = find(u);v = find(v);if (u == v) return ;father[u] = v;}public:vector<int> findRedundantConnection(vector<vector<int>>& edges) {init();for (int i = 0; i < edges.size(); i++){if (isSame(edges[i][0], edges[i][1])) return edges[i];else join(edges[i][0], edges[i][1]);}return {};}
};

Leetcode685.冗余连接II

文章链接:代码随想录
题目链接:685.冗余连接II

思路:将有向图问题拆解成两个无向图有环问题。
另外注意const int n = 1005; n前需加const,否则用n初始化数组会报错,因为n 是一个可变的值

class Solution {
private:const int n = 1005;vector<int> father = vector<int>(n);void init(){for (int i = 0; i < n; i++){father[i] = i;}}int find(int u){return u == father[u] ? u : father[u] = find(father[u]);}bool isSame(int u, int v){u = find(u);v = find(v);return u == v;}void join(int u, int v){u = find(u);v = find(v);if (u == v) return;father[v] = u;}vector<int> getRemoveEdge(const vector<vector<int>>& edges){init();for (int i = 0; i < edges.size(); i++){if (isSame(edges[i][0], edges[i][1])) return edges[i];join(edges[i][0], edges[i][1]);}return {};}bool isTree(const vector<vector<int>>& edges, int i){init();for (int j = 0; j < edges.size(); j++){if (j == i) continue;if (isSame(edges[j][0], edges[j][1])) return false;join(edges[j][0], edges[j][1]);}return true;}public:vector<int> findRedundantDirectedConnection(vector<vector<int>>& edges) {int inDegree[1005] = {0};for (int i = 0; i < edges.size(); i++){inDegree[edges[i][1]]++;}vector<int> vec;for (int i = edges.size() - 1; i >= 0; i--){if(inDegree[edges[i][1]] == 2) vec.push_back(i);}if (vec.size() > 0){if (isTree(edges, vec[0])) return edges[vec[0]];else return edges[vec[1]];}return getRemoveEdge(edges);}
};

图论第三天打卡,目前随想录上的图论问题刷完,加油!!!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/655361.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Vue之初识Vue CLI 脚手架

Vue CLI 是Vue 官方提供的一个全局命令工具 可以帮助我们快速创建一个开发Vue项目的标准化基础架子。【集成了webpack配置】 脚手架有什么好处&#xff1f; 1.开箱即用&#xff0c;零配置 2.内置 babel 等工具 3.标准化 使用步骤: 1.全局安装(一次):yarn globaladd vue/cli …

webassembly003 whisper.cpp的main项目-4 一些宏定义细节

标记函数为已弃用宏&#xff1a;WHISPER_DEPRECATED 对于 GNU 编译器&#xff08;__GNUC__&#xff09;&#xff0c;使用 __attribute__((deprecated(hint))) 属性将函数标记为已弃用&#xff0c;并附带指定的提示信息。对于微软 Visual C 编译器&#xff08;_MSC_VER&#xf…

Hack The Box-Pov

信息收集&端口利用 先使用nmap对主机进行端口扫描 nmap 10.10.11.251只开放了80端口 访问网站后发现没有什么功能点,尝试目录扫描 并没有什么能够利用的点 使用gobuster进行子域名扫描 gobuster dns -d pov.htb -w /usr/share/wordlists/seclists/Discovery/DNS/subdo…

MSE Nacos 配置变更审计平台使用指南

作者&#xff1a;孙立&#xff08;涌月&#xff09;、邢学超&#xff08;于怀&#xff09;、李艳林&#xff08;彦林&#xff09; 配置审计平台简介 Nacos [ 1] 作为一款业界主流的微服务注册中心和配置中心&#xff0c;管理着企业核心的配置资产&#xff0c;由于配置变更的安…

聚醚醚酮(Polyether Ether Ketone)PEEK在粘接使用时使用UV胶水的优势有哪些?要注意哪些事项?

使用UV胶水在聚醚醚酮&#xff08;Polyether Ether Ketone&#xff0c;PEEK&#xff09;上进行粘接可能具有一些优势&#xff0c;但同时也需要注意一些事项。以下是使用UV胶水的优势和需要考虑的事项&#xff1a; 优势&#xff1a; 1.快速固化&#xff1a; UV胶水通常具有快速…

spark window源码探索

核心类&#xff1a; 1. WindowExec 物理执行逻辑入口&#xff0c;主要doExecute()和父类WindowExecBase 2. WindowFunctionFrame 窗框执行抽象&#xff0c;其子类对应sql语句的不同窗框 其中又抽象出BoundOrdering类, 用于判断一行是否在界限内(Bound), 分为RowBoundOrdering…

flask 实现token生成以及携带token请求接口

提示&#xff1a;文章写完后&#xff0c;目录可以自动生成&#xff0c;如何生成可参考右边的帮助文档 文章目录 前言一、Flask-JWT-Extended是什么&#xff1f;二、使用步骤1.引入库2.请求验证 总结 前言 提示&#xff1a;这里可以添加本文要记录的大概内容&#xff1a; 提示&…

总结10(break和continue)

break break如果用于循环是用来终止循环 break如果用于switch&#xff0c;则是用于终止switch break不能直接用于if&#xff0c;除非if属于循环内部的一个子句&#xff08;下图为举例&#xff09; 例1&#xff1a; &#xff08;该图中break与if没有关系&#xff0c;只终止for循…

3、css设置样式总结、节点、节点之间关系、创建元素的方式、BOM

一、css设置样式的方式总结&#xff1a; 对象.style.css属性 对象.className ‘’ 会覆盖原来的类 对象.setAttribut(‘style’,‘css样式’) 对象.setAttribute(‘class’,‘类名’) 对象.style.setProperty(css属性名,css属性值) 对象.style.cssText “css样式表” …

利用外卖系统源码构建高效的在线订餐平台

在当今数字化时代&#xff0c;外卖服务已成为人们日常生活中不可或缺的一部分。为了满足用户需求&#xff0c;许多创业者和企业都希望搭建自己的在线订餐平台。利用现有的外卖系统源码&#xff0c;可以快速构建一个高效、安全的在线订餐平台。本文将介绍如何利用外卖系统源码来…

Latex warning: Ignoring empty anchor,如何解决?

目录 一、解决办法&#xff1a; 二、另一种潜在的方法&#xff1a; 一、解决办法&#xff1a; 在\footnotetext{....}前添加&#xff1a;\makeatletter\def\HyWarning#1{}\makeatother \makeatletter\def\HyWarning#1{}\makeatother \footnotetext{*Address Correspondence…

SeaTunnel集群安装

环境准备 服务器节点 节点名称 IP bigdata1 192.168.1.250 bigdata4 192.168.1.251 bigdata5 192.168.1.252 Java环境&#xff08;三个节点都需要&#xff09; java1.8 注意&#xff1a;在安装SeaTunnel集群时&#xff0c;最好是现在一个节点上将所有配置都修改完&a…

AI工具【OCR 01】Java可使用的OCR工具Tess4J使用举例(身份证信息识别核心代码及信息提前方法分享)

Java可使用的OCR工具Tess4J使用举例 1.简介1.1 简单介绍1.2 官方说明 2.使用举例2.1 依赖及语言数据包2.2 核心代码2.3 识别身份证信息2.3.1 核心代码2.3.2 截取指定字符2.3.3 去掉字符串里的非中文字符2.3.4 提取出生日期&#xff08;待优化&#xff09;2.3.5 实测 3.总结 1.简…

# Redis 分布式锁如何自动续期

Redis 分布式锁如何自动续期 何为分布式 分布式&#xff0c;从狭义上理解&#xff0c;也与集群差不多&#xff0c;但是它的组织比较松散&#xff0c;不像集群&#xff0c;有一定组织性&#xff0c;一台服务器宕了&#xff0c;其他的服务器可以顶上来。分布式的每一个节点&…

搭建 prometheus + grafana + springboot3 监控

下载安装包 下载prometheus&#xff1a;https://github.com/prometheus/prometheus/releases/download/v2.42.0/prometheus-2.42.0.windows-amd64.zip 下载grafana&#xff1a; https://dl.grafana.com/enterprise/release/grafana-enterprise-9.4.1.windows-amd64.zip Spr…

Python中容器类型的数据

目录 序列 序列的索引操作 加和乘操作 切片操作 成员测试 列表 创建列表 追加元素 插入元素 替换元素 删除元素 元组 创建元组 元组拆包 集合 创建集合 修改集合 字典 创建字典 修改字典 访问字典视图 遍历字典 若我们想将多个数据打包并且统一管理&…

Cloudreve个人网盘系统源码 支持云存储(七牛、阿里云OSS、腾讯云COS、又拍云、OneDrive) 基于Go框架

现在的网盘动不动就限速&#xff0c;涨价&#xff0c;弄得很是心烦。今天分享一款开源免费的网盘项目&#xff0c;基于 Go 语言开发的 Cloudreve。Cloudreve基于Go框架云存储个人网盘系统源码支持多家云存储驱动&#xff08;从机、七牛、阿里云 OSS、腾讯云 COS、又拍云、OneDr…

Win10 双网卡实现同时上内外网

因为需要同时上内网和外网&#xff0c;但公司做了网络隔离&#xff0c;不能同时上内外网&#xff0c;所以多加了块无线网卡&#xff0c;配置双网关实现同时上内外网&#xff0c;互不影响 打开 Windows PowerShell&#xff08;管理员&#xff09;&#xff0c;输入&#xff1a;ro…

翻译: GPT-4 Vision通过量身定制的推荐来增强应用的用户体验 升级Streamlit五

GPT-4 Vision 系列: 翻译: GPT-4 with Vision 升级 Streamlit 应用程序的 7 种方式一翻译: GPT-4 with Vision 升级 Streamlit 应用程序的 7 种方式二翻译: GPT-4 Vision静态图表转换为动态数据可视化 升级Streamlit 三翻译: GPT-4 Vision从图像转换为完全可编辑的表格 升级St…

【新书推荐】3.2节 位运算之加减乘除

本节内容&#xff1a;二进制移位运算&#xff0c;以及逻辑运算与算术运算之间的转换。任何进制的位运算本质都是一样的。 ■二进制数移位运算&#xff1a;二进制数向左移位运算相当于做2的幂乘法运算&#xff0c;二进制数向右移位运算&#xff0c;相当于做2的幂除法运算。 ■十…