岛屿数量(200)

class Solution {

    public int numIslands(char[][] grid) {

        int res = 0;
int m = grid.length;
int n = grid[0].length; for (int i = 0; i < m ; i++){
for (int j = 0; j < n; j++){
if (grid[i][j] == '1'){
res+=1;
markLand(grid, i, j);
} }
}
return res;
} private void markLand(char[][] grid, int i, int j){
if (i < 0 || i > grid.length-1 || j < 0 || j > grid[0].length-1 || grid[i][j] == '0'){
return ;
}
grid[i][j] = '0'; markLand(grid, i+1, j);
markLand(grid, i-1, j);
markLand(grid, i, j+1);
markLand(grid, i, j-1);
}
}
  • 分析

简单的四向寻找

腐烂的橘子(994)

class Solution {
private static final int[][] DIRECTIONS = {{1,0}, {-1,0}, {0,1}, {0,-1}};
public int orangesRotting(int[][] grid) {
int res = 0;
int fresh = 0;
int m = grid.length;
int n = grid[0].length;
Deque<int []> rotArray = new ArrayDeque<>(); for (int i = 0; i < m; i++){
for (int j = 0; j < n; j++){
if (grid[i][j] == 1) fresh++;
else if (grid[i][j] == 2) rotArray.addFirst(new int[] {i, j});
}
} while (fresh > 0 && !rotArray.isEmpty()){
res+=1;
int len = rotArray.size();
for (int i = 0; i < len; i++){
int[] temp = rotArray.removeLast();
for (int[] dir : DIRECTIONS){
int row = temp[0] + dir[0];
int col = temp[1] + dir[1]; if (0 <= row && row < m && 0 <= col && col < n
&& grid[row][col] == 1){
fresh--;
grid[row][col] = 2;
rotArray.addFirst(new int[] {row, col});
}
}
}
} return fresh>0 ? -1 : res;
}
}
  • 分析

相比于普通的四向寻找, 难点在于轮次, 可以类比于二叉树的层序遍历, 一层一层扩展

课程表(207)

class Solution {
public boolean canFinish(int numCourses, int[][] prerequisites) {
List<Integer>[] line = new ArrayList[numCourses];
Arrays.setAll(line, i -> new ArrayList<>());
for (int[] p : prerequisites){
line[p[1]].add(p[0]);
} int[] colors = new int[numCourses];
for (int i = 0; i < numCourses; i++){
if (colors[i] == 0 && dfs(i, colors, line)) return false;
} return true; } private boolean dfs(int i, int[] colors, List<Integer>[] line){
colors[i] = 1;
for (int j : line[i]){
if (colors[j] == 1 || colors[j] == 0 && dfs(j, colors, line)){
return true;
}
}
colors[i] = 2;
return false;
}
}
  • 分析

判断图中是否有环

利用三色标记法 {0, 1, 2} →{未达, 正在使用, 可达}

实现Tire(208)

代码太长了就不放了

 private static class Node {
Node[] son = new Node[26];
boolean end;
} private final Node root;
  • 分析

insert() 部分 实际上是开辟道路 if (curr.son[c] == null) curr.son[c] = new Node();

search() 和 startsWith() 实际上都是一路沿son下移, 遇到没开辟完的路线 return false

如果移动结束了, 如果当前节点的end == true 则为完整单词 , 否则为前缀

hot100之图论的更多相关文章

  1. [leetcode] 题型整理之图论

    图论的常见题目有两类,一类是求两点间最短距离,另一类是拓扑排序,两种写起来都很烦. 求最短路径: 127. Word Ladder Given two words (beginWord and end ...

  2. 并查集(图论) LA 3644 X-Plosives

    题目传送门 题意:训练指南P191 分析:本题特殊,n个物品,n种元素则会爆炸,可以转移到图论里的n个点,连一条边表示u,v元素放在一起,如果不出现环,一定是n点,n-1条边,所以如果两个元素在同一个 ...

  3. NOIp 2014 #2 联合权值 Label:图论 !!!未AC

    题目描述 无向连通图G 有n 个点,n - 1 条边.点从1 到n 依次编号,编号为 i 的点的权值为W i ,每条边的长度均为1 .图上两点( u , v ) 的距离定义为u 点到v 点的最短距离. ...

  4. HDU 5521 [图论][最短路][建图灵感]

    /* 思前想后 还是决定坚持写博客吧... 题意: n个点,m个集合.每个集合里边的点是联通的且任意两点之间有一条dis[i]的边(每个集合一个dis[i]) 求同时从第1个点和第n个点出发的两个人相 ...

  5. SDUT 2141 【TEST】数据结构实验图论一:基于邻接矩阵的广度优先搜索遍历

    数据结构实验图论一:基于邻接矩阵的广度优先搜索遍历 Time Limit: 1000MS Memory Limit: 65536KB Submit Statistic Discuss Problem ...

  6. [转] POJ图论入门

    最短路问题此类问题类型不多,变形较少 POJ 2449 Remmarguts' Date(中等)http://acm.pku.edu.cn/JudgeOnline/problem?id=2449题意: ...

  7. HDU 5934 Bomb 【图论缩点】(2016年中国大学生程序设计竞赛(杭州))

    Bomb Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submis ...

  8. Codeforces 553C Love Triangles(图论)

    Solution: 比较好的图论的题. 要做这一题,首先要分析love关系和hate关系中,love关系具有传递性.更关键的一点,hate关系是不能成奇环的. 看到没有奇环很自然想到二分图的特性. 那 ...

  9. 图论(floyd算法):NOI2007 社交网络

    [NOI2007] 社交网络 ★★   输入文件:network1.in   输出文件:network1.out   简单对比 时间限制:1 s   内存限制:128 MB [问题描述] 在社交网络( ...

  10. 【图论】【宽搜】【染色】NCPC 2014 A Ades

    题目链接: http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1787 题目大意: N个点M条无向边(N,M<=200000),一个节点只能有一个 ...

随机推荐

  1. linux(centos)配置ipv6网卡

    1.ipv6网卡配置文件和ipv4在同一个网卡配置文件中 vim /etc/sysconfig/network-scripts/ifcfg-eth0 设置好之后重启网卡生效 2.测试

  2. OpenAI的GPT-4o:普通人的AI秘书来了

    1. 惊艳时刻:AI比你想象的更"人性" 早餐时,张三正埋头刷推送,一篇关于OpenAI发布GPT-4o的文章瞬间点燃了他的好奇心.这个AI简直是科技圈的惊雷!竟然可以像真人一样说 ...

  3. [框架应用系列:Quartz快速上手] Java定时任务解决方案之Quartz集群

    Quartz 是一个开源的作业调度框架,它完全由 Java 写成,并设计用于 J2SE 和 J2EE 应用中.它提供了巨大的灵 活性而不牺牲简单性.你能够用它来为执行一个作业而创建简单的或复杂的调度. ...

  4. 《HelloGitHub》第 108 期

    兴趣是最好的老师,HelloGitHub 让你对开源感兴趣! 简介 HelloGitHub 分享 GitHub 上有趣.入门级的开源项目. github.com/521xueweihan/HelloG ...

  5. 手把手带你从论文出发实战搭建分割FCN网络

    作者:SkyXZ CSDN:SkyXZ--CSDN博客 博客园:SkyXZ - 博客园 FCN论文地址:Fully Convolutional Networks for Semantic Segmen ...

  6. Reactjs之Vue用户0基础上手Reactjs笔记

    Reactjs之Vue用户0基础上手Reactjs笔记 - 搜栈网 (seekstack.cn)https://www.seekstack.cn/post/382

  7. 【解决方法】edge浏览器不小心删除收藏夹怎么办?

    C:\Users\用户名\AppData\Local\Microsoft\Edge\User Data\Default 进入该目录,找到名为Bookmarks或Bookmarks.bak或Bookma ...

  8. where 闭包查询

    $map1[] = ["like_article.user_id", 'not in', function ($query) use ($user_id) { $query-> ...

  9. 工具 | burpgpt

    0x00 简介 burpgpt是一款利用AI来检测安全漏洞的burpsuite插件.Burp Suite GPT扩展集成了OpenAI的GPT,以执行额外的被动扫描以发现高度定制的漏洞,并支持运行任何 ...

  10. 个人对Debian桌面系统的简单使用分享

    前言 自从安装Debian12作为双系统已经过了大半个月,平常主用Debian,偶尔切回Windows找找文档,总体来说体验还是很不错的.先贴个桌面照 为什么要使用Linux作为个人桌面 当初刚从Wi ...