当前位置:网站首页>[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
边栏推荐
- Detailed explanation of the lock algorithm of MySQL lock series (glory Collection Edition)
- How do you use the jar package sent by others (how to use the jar package sent by others)
- MySQL's way to solve deadlock - lock analysis of common SQL statements
- Mysql Explain 详解(荣耀典藏版)
- From prediction to decision-making, Chapter 9 Yunji datacanvas launched the ylearn causal learning open source project
- 修改MySQL密码的四种方法(适合初学者)
- 1313_ Pyserial installation and document generation
- 别人发你的jar包你如何使用(如何使用别人发您的jar包)
- [TA frost wolf \u may - hundred people plan] Figure 3.7 TP (d) r architecture of mobile terminal
- 【OpenGL】GLES20.glClear
猜你喜欢

OBS keyboard plug-in custom DIY

【TA-霜狼_may-《百人计划》】图形3.5 Early-z 和 Z-prepass

The virtual host website cannot access the self-test method

unordered_map的hash function及hash bucket存储方式探索

作业7.27 IO进程

MySQL是如何利用索引的(荣耀典藏版)

Which users are suitable for applying for rapidssl certificate

Newline required at end of file but not found.

Please, don't use the command line to configure MySQL master-slave replication. Isn't it fragrant to deploy with urlos interface?

新基建助力智能化道路交通领域的转型发展
随机推荐
【HCIP】路由策略、策略路由
unordered_ The hash function of map and the storage mode of hash bucket
Interviewer: what is the factory method mode?
Lock mechanism in MySQL database InnoDB storage engine (glory Collection Edition)
【ELM分类】基于核极限学习机和极限学习机实现UCI数据集分类附matlab代码
[leetcode] 13. linked list cycle · circular linked list
0动态规划中等 LeetCode873. 最长的斐波那契子序列的长度
【软件测试】—— 自动化测试之unittest框架
What problems should be avoided when using BigDecimal type? (glory Collection Edition)
unordered_map的hash function及hash bucket存储方式探索
Representation of children and brothers of trees
Class notes (5) (1) - 593. Binary search
Common SQL statement query
QT implementation disable shortcut key
功能测试和非功能测试区别简析,上海好口碑软件测试公司推荐
Detailed explanation of the lock algorithm of MySQL lock series (glory Collection Edition)
2022.7.8 eth price analysis
Four methods of modifying MySQL password (suitable for beginners)
Feign calls get and post records
Network must know topics