当前位置:网站首页>May brush question 27 - figure
May brush question 27 - figure
2022-07-06 09:38:00 【A Guang chasing dreams】
May brush questions 27—— chart
Today's brush topic content : chart
Preface
- An algorithm opens the way to brush the questions of waste materials , Update the problem solution content of the problem brush every day
- Focus on personal understanding , Look at the difficulty and update the number of questions
- The title comes from Li Kou
- Try to work out at least one question every day
- Language java、python、c\c++
One 、 Today's topic
I only did two questions , I didn't have much time to write solutions yesterday , Make up today , The next two questions will be brushed later when you are not so busy
- 1791. Find the central node of the star graph
- 797. All possible paths
- 851. Noise and wealth
- 959. Area by slash
Two 、 Their thinking
1. 1791. Find the central node of the star graph
edges[i]Both are composed of two nodes , It is known that the central node will be connected to all other nodes- Select any point from a set of nodes , Compare with two points in another node set
- If it appears in another node set, it means that this point is the central node
- Otherwise, it is another node
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];
}
}
/* * Judge according to the edge , If it is the center point , It must appear in both sets */
2. 797. All possible paths
Depth first search again , Let's use recursion first , Next time use a loop to solve
- To find all the paths , That is, violent enumeration
graph[i]Indication pointiAll point sets that can be reached- Define a stack
stack, Initial deposit0This node- The delivery process puts all points on the stack , If the end point is the last node , Then put
stackAdd the contents of to the list- In the process of returning
stackOut of the 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)); // It can't be passed in directly stack, Only one object can be copied deeply
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; // Get the number of all points
stack = new ArrayList<>();
ret = new ArrayList<>();
stack.add(0);
dfs(n, graph, 0);
return ret;
}
}
/* * dfs( Number to find , Adjacency matrix , The current number ) */
3. 851. Noise and wealth
4. 959. Area by slash
边栏推荐
- Full stack development of quartz distributed timed task scheduling cluster
- [shell script] - archive file script
- Reids之删除策略
- Meituan Er Mian: why does redis have sentinels?
- Connexion d'initialisation pour go redis
- Mapreduce实例(五):二次排序
- Minio distributed file storage cluster for full stack development
- Kratos战神微服务框架(三)
- What are the models of data modeling
- Redis之Lua脚本
猜你喜欢
随机推荐
发生OOM了,你知道是什么原因吗,又该怎么解决呢?
Kratos ares microservice framework (I)
Redis之性能指标、监控方式
Redis cluster
Redis之五大基础数据结构深入、应用场景
Take you back to spark ecosystem!
Mapreduce实例(五):二次排序
英雄联盟轮播图自动轮播
DCDC power ripple test
Nacos installation and service registration
工作流—activiti7环境搭建
小白带你重游Spark生态圈!
MapReduce instance (IX): reduce end join
Why data Tiering
数据建模有哪些模型
【深度学习】语义分割-源代码汇总
Basic usage of xargs command
018.有效的回文
Go redis initialization connection
MapReduce working mechanism








