当前位置:网站首页>五月刷题27——图
五月刷题27——图
2022-07-06 09:02:00 【追逐梦想的阿光】
今日刷题内容: 图
前言
- 一个算法废材的刷题之路开更了, 更新每天刷题的题解内容
- 注重个人理解,看难度更新题目数量
- 题目来源于力扣
- 争取每日都能做出至少一题
- 语言java、python、c\c++
一、今日题目
只做了两题,昨天没多少时间写题解,今天补上,后面两题等后面不那么忙再刷吧
二、解题思路
1. 1791. 找出星型图的中心节点
edges[i]
都是由两个节点构成,已知中心节点会和所有其他节点相连- 从一个节点集合中任选一点,和另一节点集合中的两点进行比较
- 如果在另一节点集合中出现则说明该点就是中心节点
- 否则是另一节点
class Solution {
public int findCenter(int[][] edges) {
int e = edges[0][0];
if (e == edges[1][0] || e == edges[1][1]){
return e;
}
return edges[0][1];
}
}
/* * 根据边来判断,如果是中心点,说明一定在两个点集中都出现 */
2. 797. 所有可能的路径
又是深度优先搜索,这里先用递归做一下,下次用循环来解决
- 要找到所有的路径,即暴力枚举
graph[i]
表示点i
能到达的所有点集- 定义一个栈
stack
,初始存入0
这个节点- 递的过程把所有点入栈,如果终点是最后一个节点,则把
stack
中的内容加入列表中- 归的过程中再把
stack
中的元素出栈
class Solution {
List<Integer> stack;
List<List<Integer>> ret;
public void dfs(int n, int[][]graph, int now){
if (now == n - 1){
ret.add(new ArrayList<>(stack)); // 不能直接传入stack,只能深复制一个对象
return;
}
for(int i = 0; i < graph[now].length; i++){
stack.add(graph[now][i]);
dfs(n, graph, graph[now][i]);
stack.remove(stack.size() - 1);
}
}
public List<List<Integer>> allPathsSourceTarget(int[][] graph) {
int n = graph.length; // 获取所有的点的数量
stack = new ArrayList<>();
ret = new ArrayList<>();
stack.add(0);
dfs(n, graph, 0);
return ret;
}
}
/* * dfs(要找的数, 邻接矩阵, 当前的数) */
3. 851. 喧闹和富有
4. 959. 由斜杠划分区域
边栏推荐
- Design and implementation of online snack sales system based on b/s (attached: source code paper SQL file)
- QDialog
- Kratos ares microservice framework (III)
- 068.查找插入位置--二分查找
- Redis之性能指标、监控方式
- Minio distributed file storage cluster for full stack development
- [oc]- < getting started with UI> -- common controls - prompt dialog box and wait for the prompt (circle)
- Heap (priority queue) topic
- Sqlmap installation tutorial and problem explanation under Windows Environment -- "sqlmap installation | CSDN creation punch in"
- [Yu Yue education] reference materials of power electronics technology of Jiangxi University of science and technology
猜你喜欢
Redis之核心配置
Redis之五大基础数据结构深入、应用场景
Improved deep embedded clustering with local structure preservation (Idec)
QML type: locale, date
【shell脚本】——归档文件脚本
【深度学习】语义分割-源代码汇总
Kratos战神微服务框架(一)
The carousel component of ant design calls prev and next methods in TS (typescript) environment
Advance Computer Network Review(1)——FatTree
【深度學習】語義分割-源代碼匯總
随机推荐
发生OOM了,你知道是什么原因吗,又该怎么解决呢?
面渣逆袭:Redis连环五十二问,图文详解,这下面试稳了
Mysql database recovery (using mysqlbinlog command)
【深度学习】语义分割-源代码汇总
一文读懂,DDD落地数据库设计实战
小白带你重游Spark生态圈!
Mapreduce实例(六):倒排索引
为什么要数据分层
有软件负载均衡,也有硬件负载均衡,选择哪个?
O & M, let go of monitoring - let go of yourself
AcWing 2456. Notepad
Redis之Geospatial
Basic concepts of libuv
Global and Chinese market for annunciator panels 2022-2028: Research Report on technology, participants, trends, market size and share
Global and Chinese market of metallized flexible packaging 2022-2028: Research Report on technology, participants, trends, market size and share
Withdrawal of wechat applet (enterprise payment to change)
Redis之连接redis服务命令
Blue Bridge Cup_ Single chip microcomputer_ Measure the frequency of 555
数据建模有哪些模型
One article read, DDD landing database design practice