当前位置:网站首页>[brother hero's July training] day 27: picture
[brother hero's July training] day 27: picture
2022-07-28 02:44:00 【If I were Wen Shuai】
List of articles
One 、1791. Find the central node of the star graph
1791. Find the central node of the star graph
There is an undirected Star type chart , from n A number from 1 To n Node composition of . The star graph has a center node , And there is n - 1 The edges connect the central node to each other .
Here's a two-dimensional array of integers edges , among edges[i] = [ui, vi] At the node ui and vi There is an edge between . Please find out and return to edges The center node of the star graph represented by .
leetcode 1791 java
Ideas
The degree is greater than 1
class Solution {
public int findCenter(int[][] edges) {
int[] degree = new int[edges.length+2];
for(int[] edge:edges){
degree[edge[0]]++;
degree[edge[1]]++;
}
for(int i=0; ;i++){
if(degree[i]>1){
return i;
}
}
}
}
Two 、797. All possible paths
Here you are n Of nodes Directed acyclic graph (DAG), Please find all slave nodes 0 To the node n-1 And output ( No specific order is required )
graph[i] Is a slave node i List of all nodes that can be accessed ( That is, the slave node i To the node graph[i][j] There is a directed side ).
leetcode 797 java
Ideas
dfs
class Solution {
List<List<Integer>> ans = new ArrayList<>();
Deque<Integer> stack = new ArrayDeque<>();
public List<List<Integer>> allPathsSourceTarget(int[][] graph) {
stack.offerLast(0);
dfs(graph,0,graph.length-1);
return ans;
}
public void dfs(int[][] graph, int x, int n){
if(x==n){
ans.add(new ArrayList<Integer>(stack));
}
for(int y:graph[x]){
stack.offerLast(y);
dfs(graph,y,n);
stack.pollLast();
}
}
}
3、 ... and 、851. Noise and wealth
There is a group n Individuals as subjects , from 0 To n - 1 Number , Each of them has a different amount of money , And different levels of quiet value (quietness). For convenience , We'll number it x The person of is abbreviated as "person x ".
Give you an array richer , among richer[i] = [ai, bi] Express person ai Than person bi More money . Give you another integer array quiet , among quiet[i] yes person i Quiet value of .richer The data given in Self-consistent logic ( in other words , stay person x Than person y More money at the same time , There will be no person y Than person x More money ).
Now? , Returns an array of integers answer As the answer , among answer[x] = y The premise is , There must be no less than person x Of the people ,person y Is the quietest person ( That is, quiet value quiet[y] The youngest ).
leetcode 851 java
Ideas
Adjacency matrix
class Solution {
public int[] loudAndRich(int[][] richer, int[] quiet) {
int n = quiet.length;
int[][] w = new int[n][n];
int[] in = new int[n];
for(int[] rich:richer){
w[rich[0]][rich[1]]=1;
in[rich[1]]++;
}
Deque<Integer> queue = new ArrayDeque<>();
// Initialize stack and result set
int[] ans = new int[n];
for(int i=0;i<n;i++){
ans[i]=i;
if(in[i]==0) queue.addLast(i);
}
while(!queue.isEmpty()){
int t = queue.pollFirst();
for(int u=0;u<n;u++){
if(w[t][u]==1){
if(quiet[ans[t]]<quiet[ans[u]]) ans[u]=ans[t];
if(--in[u]==0) queue.addLast(u);
}
}
}
return ans;
}
}
Four 、959. Area by slash
In by 1 x 1 Made up of squares n x n grid grid in , Every 1 x 1 The square is made of ‘/’、‘’ Or a space . These characters will divide the blocks into some common areas .
Given grid grid Expressed as an array of strings , return The number of areas .
Please note that , The backslash character is escaped , therefore ‘’ use ‘\’ Express .
leetcode 959 java
Ideas
dfs
class Solution {
int[][] dir = {
{
0,1},{
1,0},{
0,-1},{
-1,0}};
public void dfs(int[][] grid,int x,int y){
int m =grid.length;
for(int i=0 ;i<4 ;i++ ){
int nx = x + dir[i][0], ny = y + dir[i][1];
if(nx>=0 && nx<m && ny>=0 && ny<m && grid[nx][ny]==0){
grid[nx][ny]=1;
dfs(grid,nx,ny);
}
}
}
public int regionsBySlashes(String[] grid) {
int m = grid.length; // That's ok
int[][] new_grid = new int[3*m][3*m];
for(int i=0;i<m;i++){
for(int j =0;j<m;j++){
if(grid[i].charAt(j)=='/'){
new_grid[3*i][3*j+2]=1;
new_grid[3*i+1][3*j+1]=1;
new_grid[3*i+2][3*j]=1;
} else if(grid[i].charAt(j)=='\\'){
new_grid[3*i][3*j]=1;
new_grid[3*i+1][3*j+1]=1;
new_grid[3*i+2][3*j+2]=1;
}
}
}
int cnt=0;
for(int i=0;i<3*m;i++){
for(int j=0;j<3*m;j++){
if(new_grid[i][j]==0){
cnt++;
new_grid[i][j]=1;
dfs(new_grid,i,j);
}
}
}
return cnt;
}
}
summary
边栏推荐
- MySQL blocking monitoring script
- When iPhone copies photos to the computer, the device connection often fails and the transmission is interrupted. Here's the way
- MySQL's way to solve deadlock - lock analysis of common SQL statements
- Pytorch optimizer settings
- Deep Residual Learning for Image Recognition浅读与实现
- unordered_map的hash function及hash bucket存储方式探索
- Detailed explanation of the lock algorithm of MySQL lock series (glory Collection Edition)
- 别人发你的jar包你如何使用(如何使用别人发您的jar包)
- Today in history: the father of database passed away; Apple buys cups code; IBM chip Alliance
- Cesium3Dtilesets 使用customShader的解读以及泛光效果示例
猜你喜欢

关于Sqli-labs单引号不报错的问题

1313_ Pyserial installation and document generation

A 64 bit 8-stage pipelined adder based on FPGA

How do you use the jar package sent by others (how to use the jar package sent by others)
![[software testing] - unittest framework for automated testing](/img/7a/29b222cb0b6a5953b98f8d797cd106.png)
[software testing] - unittest framework for automated testing

Should programmers choose outsourcing companies
![[advanced ROS] Lecture 9 robot model motion based on rviz and arbotix control](/img/7f/f0360210e8a9f7e45410d79635bfd9.png)
[advanced ROS] Lecture 9 robot model motion based on rviz and arbotix control
![[understanding of opportunity -53]: Yang Mou stands up and plots to defend himself](/img/93/2f61993770d93d9adc80a9fa89e71c.jpg)
[understanding of opportunity -53]: Yang Mou stands up and plots to defend himself

Say yes, I will love you, and I will love you well

POC simulation attack weapon - Introduction to nucleus (I)
随机推荐
Unity saves pictures to albums and rights management
With elephant & nbsp; Eplato created by swap, analysis of the high premium behind it
Why is there no unified quotation for third-party testing fees of software products?
Alipay applet authorization / obtaining user information
Red hat official announced the new president and CEO! Paul Cormier, a key figure in transformation, is "retiring"
Pytorch optimizer settings
unity中物体碰撞反弹(学习)
【TA-霜狼_may-《百人计划》】图形3.7 移动端TP(D)R架构
First knowledge of C language -- structure, branch and loop statements
Leetcode judge whether palindrome number
Emotional drama in the world Zhou Bingkun lost his job because he saw Tu Zhiqiang and was shot
【信号处理】基于高阶统计量特征的通信系统中微弱信号检测附matlab代码
Sword finger offer special assault edition day 12
1313_pyserial的安装以及文档的生成
Chapter 3 business function development (batch export of market activities, Apache POI)
"Risking your life to upload" proe/creo product structure design - seam and buckle
树的孩子兄弟表示法
What can you say to comfort your girlfriend or daughter-in-law
软件产品第三方测试费用为什么没有统一的报价?
How MySQL uses indexes (glory Collection Edition)