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

图论第三天|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);}
};

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

相关文章:

  • 活字格V9获取图片失败bug,报错404,了解存储路径,已改为批量上传和批量获取
  • 【网络基础】mac地址
  • 五、Kotlin 函数进阶
  • 数据双向绑定v-modal
  • 分布式ID(4):雪花算法生成ID之Leaf(美团点评分布式ID生成系统)
  • Rollup:打包 TypeScript - React 组件库
  • webassembly003 TTS BARK.CPP
  • Docker 搭建MySQL主从复制-读写分离
  • 【极数系列】Flink集成DataSource读取文件数据(08)
  • 【大数据】Flink 架构(三):事件时间处理
  • idea 打包跳过测试
  • Keil/MDK平台 - 有符号与无符号变量比较注意事项
  • C语言第十三弹---VS使用调试技巧
  • TDengine 签约海博思创,助力储能运维平台数据管理
  • Flink 集成 Debezium Confluent Avro ( format=debezium-avro-confluent )
  • [数据结构]链表的实现在PHP中
  • 【许晓笛】 EOS 智能合约案例解析(3)
  • Java教程_软件开发基础
  • JWT究竟是什么呢?
  • leetcode98. Validate Binary Search Tree
  • PHP 程序员也能做的 Java 开发 30分钟使用 netty 轻松打造一个高性能 websocket 服务...
  • Promise面试题2实现异步串行执行
  • TiDB 源码阅读系列文章(十)Chunk 和执行框架简介
  • vue-loader 源码解析系列之 selector
  • 阿里云ubuntu14.04 Nginx反向代理Nodejs
  • 函数式编程与面向对象编程[4]:Scala的类型关联Type Alias
  • 机器人定位导航技术 激光SLAM与视觉SLAM谁更胜一筹?
  • 区块链共识机制优缺点对比都是什么
  • 如何打造100亿SDK累计覆盖量的大数据系统
  • 思否第一天
  • 无服务器化是企业 IT 架构的未来吗?
  • 一些基于React、Vue、Node.js、MongoDB技术栈的实践项目
  • 源码安装memcached和php memcache扩展
  • Play Store发现SimBad恶意软件,1.5亿Android用户成受害者 ...
  • 容器镜像
  • 如何在 Intellij IDEA 更高效地将应用部署到容器服务 Kubernetes ...
  • ​油烟净化器电源安全,保障健康餐饮生活
  • # C++之functional库用法整理
  • # Swust 12th acm 邀请赛# [ E ] 01 String [题解]
  • #pragma once与条件编译
  • #我与Java虚拟机的故事#连载02:“小蓝”陪伴的日日夜夜
  • $GOPATH/go.mod exists but should not goland
  • (01)ORB-SLAM2源码无死角解析-(56) 闭环线程→计算Sim3:理论推导(1)求解s,t
  • (2015)JS ES6 必知的十个 特性
  • (搬运以学习)flask 上下文的实现
  • (大众金融)SQL server面试题(1)-总销售量最少的3个型号的车及其总销售量
  • (原+转)Ubuntu16.04软件中心闪退及wifi消失
  • (转)微软牛津计划介绍——屌爆了的自然数据处理解决方案(人脸/语音识别,计算机视觉与语言理解)...
  • (转)用.Net的File控件上传文件的解决方案
  • .NET 反射 Reflect
  • .net 重复调用webservice_Java RMI 远程调用详解,优劣势说明
  • .net 逐行读取大文本文件_如何使用 Java 灵活读取 Excel 内容 ?
  • /run/containerd/containerd.sock connect: connection refused
  • :“Failed to access IIS metabase”解决方法
  • @软考考生,这份软考高分攻略你须知道