当前位置:网站首页>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 pointi
All point sets that can be reached- Define a stack
stack
, Initial deposit0
This node- The delivery process puts all points on the stack , If the end point is the last node , Then put
stack
Add the contents of to the list- In the process of returning
stack
Out 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
边栏推荐
- 美团二面:为什么 Redis 会有哨兵?
- CAP理论
- Compilation of libwebsocket
- Le modèle sentinelle de redis
- MapReduce instance (VIII): Map end join
- Mapreduce实例(七):单表join
- Redis之cluster集群
- Global and Chinese market of cup masks 2022-2028: Research Report on technology, participants, trends, market size and share
- Detailed explanation of cookies and sessions
- Redis之哨兵模式
猜你喜欢
随机推荐
Master slave replication of redis
Blue Bridge Cup_ Single chip microcomputer_ Measure the frequency of 555
[shell script] - archive file script
Global and Chinese market of metallized flexible packaging 2022-2028: Research Report on technology, participants, trends, market size and share
[deep learning] semantic segmentation - source code summary
[three storage methods of graph] just use adjacency matrix to go out
068. Find the insertion position -- binary search
Mapreduce实例(八):Map端join
为什么要数据分层
Global and Chinese market of appointment reminder software 2022-2028: Research Report on technology, participants, trends, market size and share
Publish and subscribe to redis
YARN组织架构
美团二面:为什么 Redis 会有哨兵?
六月刷题02——字符串
Seven layer network architecture
MapReduce instance (VI): inverted index
Redis之主从复制
Cap theory
六月刷题01——数组
MapReduce instance (IV): natural sorting