当前位置:网站首页>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
边栏推荐
猜你喜欢

Kratos ares microservice framework (II)

一大波开源小抄来袭
![[three storage methods of graph] just use adjacency matrix to go out](/img/79/337ee452d12ad477e6b7cb6b359027.png)
[three storage methods of graph] just use adjacency matrix to go out

Design and implementation of online snack sales system based on b/s (attached: source code paper SQL file)

Persistence practice of redis (Linux version)

MapReduce instance (VI): inverted index

Kratos战神微服务框架(一)

Take you back to spark ecosystem!

Redis之发布订阅

MapReduce working mechanism
随机推荐
QML type: overlay
英雄联盟轮播图手动轮播
MapReduce instance (V): secondary sorting
五月刷题01——数组
Yarn organizational structure
[deep learning] semantic segmentation: paper reading: (2021-12) mask2former
Solve the problem of too many small files
MapReduce instance (VIII): Map end join
Global and Chinese market of capacitive displacement sensors 2022-2028: Research Report on technology, participants, trends, market size and share
基于B/S的医院管理住院系统的研究与实现(附:源码 论文 sql文件)
Redis之连接redis服务命令
五月刷题02——字符串
[Yu Yue education] Wuhan University of science and technology securities investment reference
【深度學習】語義分割-源代碼匯總
Leetcode problem solving 2.1.1
Heap (priority queue) topic
MySQL数据库优化的几种方式(笔面试必问)
Global and Chinese market of airport kiosks 2022-2028: Research Report on technology, participants, trends, market size and share
Redis之Bitmap
Libuv thread