<Topological Sort> ( 高频, hard) 269
269. Alien Dictionary
这些就是有向图的边,对于有向图中的每个结点,计算其入度,然后从入度为0的结点开始 BFS 遍历这个有向图,然后将遍历路径保存下来返回即可。下面来看具体的做法:
根据之前讲解,需用 TreeSet 来保存这些 pair,我们还需要一个 HashSet 来保存所有出现过的字母,需要一个一维数组 in 来保存每个字母的入度,另外还要一个 queue 来辅助拓扑遍历,我们先遍历单词集,把所有字母先存入 HashSet,然后我们每两个相邻的单词比较,找出顺序 pair,然后我们根据这些 pair 来赋度,我们把 HashSet 中入度为0的字母都排入 queue 中,然后开始遍历,如果字母在 TreeSet 中存在,则将其 pair 中对应的字母的入度减1,若此时入度减为0了,则将对应的字母排入 queue 中并且加入结果 res 中,直到遍历完成。
最后看结果 sb 和 map.size() 中的元素个数是否相同,若不相同则说明可能有环存在,返回空字符串
Map<out, in>
class Solution {
public String alienOrder(String[] words) {
int[] indegree = new int[26];
Map<Character, Set<Character>> g = new HashMap<>();
buildGraph(g, words, indegree);
return bfs(g, indegree);
} private void buildGraph(Map<Character, Set<Character>> g, String[] words, int[] indegree){
for(String word : words){
for(char c : word.toCharArray()){
g.putIfAbsent(c, new HashSet<>());
}
} for(int i = 1; i < words.length; i++){
String first = words[i - 1];
String second = words[i];
int len = Math.min(first.length(), second.length());
for(int j = 0; j < len; j++){
if(first.charAt(j) != second.charAt(j)){
char out = first.charAt(j);
char in = second.charAt(j);
if(!g.get(out).contains(in)){
g.get(out).add(in);
indegree[in - 'a']++;
}
break;
}
}
}
} private String bfs(Map<Character, Set<Character>> g, int[] indegree){
StringBuilder sb = new StringBuilder();
int totalChars = g.size();
Queue<Character> q = new LinkedList<>();
for(char c : g.keySet()){
if(indegree[c - 'a'] == 0){
sb.append(c);
q.offer(c);
}
} while(!q.isEmpty()){
char out = q.poll();
if(g.get(out) == null || g.get(out).size() == 0) continue;
for(char in : g.get(out)){
indegree[in - 'a']--;
if(indegree[in - 'a'] == 0){
q.offer(in);
sb.append(in);
}
}
}
return sb.length() == totalChars ? sb.toString() : "";
}
}
269. Alien Dictionary
<Topological Sort> ( 高频, hard) 269的更多相关文章
- 【拓扑排序】【线段树】Gym - 101102K - Topological Sort
Consider a directed graph G of N nodes and all edges (u→v) such that u < v. It is clear that this ...
- topological sort~~~~初学
今天讲了topological sort 问题: 判环:记录入队的点数,若<n则有环,可证: 算法:o(n):queue or stack,而不是o(n^2)枚举 #. 关系运算图(vijos ...
- topological sort
A topological sortof a dag G is a linear ordering of all its vertices such that if G contains anedg ...
- 拓扑排序(Topological Sort)
Graph 拓扑排序(Topological Sort) 假设一个应用场景:你用 C 编写了一个爬虫工具,其中有很多自定义的库:queue.c.queue.h.stack.c.stack.h.heap ...
- Some facts about topological sort
Definition: a topological sort of a DAG G is a sort such that for all edge (i,j) in G, i precedes j. ...
- 6-16 Topological Sort(25 分)
Write a program to find the topological order in a digraph. Format of functions: bool TopSort( LGrap ...
- [Algorithms] Topological Sort
Topological sort is an important application of DFS in directed acyclic graphs (DAG). For each edge ...
- [MIT6.006] 14. Depth-First Search (DFS), Topological Sort 深度优先搜索,拓扑排序
一.深度优先搜索 它的定义是:递归探索图,必要时要回溯,同时避免重复. 关于深度优先搜索的伪代码如下: 左边DFS-Visit(V, Adj.s)是只实现visit所有连接某个特定点(例如s)的其他点 ...
- Leetcode: Alien Dictionary && Summary: Topological Sort
There is a new alien language which uses the latin alphabet. However, the order among letters are un ...
随机推荐
- JS---DOM---点击操作---part1---20个案例
点击操作:------>事件: 就是一件事, 有触发和响应, 事件源 按钮被点击,弹出对话框 按钮---->事件源 点击---->事件名字 被点了--->触发了 弹框了---& ...
- Flutter学习笔记(22)--单个子元素的布局Widget(Container、Padding、Center、Align、FittedBox、Offstage、LimitedBox、OverflowBox、SizedBox)
如需转载,请注明出处:Flutter学习笔记(22)--单个子元素的布局Widget(Container.Padding.Center.Align.FittedBox.Offstage.Limited ...
- 一文解读JSON (转)
JSON作为目前Web主流的数据交换格式,是每个IT技术人员都必须要了解的一种数据交换格式.尤其是在Ajax和REST技术的大行其道的当今,JSON无疑成为了数据交换格式的首选! 今天我们一起来学习一 ...
- AWS云EC2(RHEL7)添加网络接口与路由调整
AWS云EC2(RHEL7)添加网络接口与路由调整 Amazon Linux(类似RHEL6,Centos6) 以及 RHEL7 修改MAC地址的说明 RHEL7 Centos7 添加路由 解决RHE ...
- Hive 时间函数总结【转】
1.日期函数UNIX时间戳转日期函数: from_unixtime语法:from_unixtime(bigint unixtime[, stringformat]) 返回值: string说明: 转化 ...
- python 基础学习笔记(6)--函数(1)
## **函数(1)** **函数的定义:** 1. [ ] 小时候大家应该都玩过乐高积木,只要通过想象和创意,可以用它怕拼凑出很多神奇的东西.随着学习的深入,编写的代码日益增加并且越来越复杂,所以需 ...
- java基础 - 泛型的使用
泛型的使用方式有泛型类,泛型接口,泛型方法. 泛型的意思是把参数类型也当成参数传入,也就是在使用时(类实例化或调用方法时)传入类型. 泛型类 在实例化时传入参数类型,不能对泛型类使用instancec ...
- 【带着canvas去流浪(9)】粒子动画
目录 一. 粒子特效 二. 开发中遇到的问题 2.1 卡顿 2.2 轨迹 2.3 复位 2.4 防护层 2.5 二维向量类 三. 实现讲解 3.1 粒子类的update方法 3.2 粒子群的绘制 3. ...
- 【spring-boot 源码解析】spring-boot 依赖管理梳理图
在文章 [spring-boot 源码解析]spring-boot 依赖管理 中,我梳理了 spring-boot-build.spring-boot-parent.spring-boot-depen ...
- PlayJava Day015
今日所学: /* 2019.08.19开始学习,此为补档. */ StringBuffer 定义: 可变字符序列 - 线程安全的 作用:基本与String相同,也是用于描述字符串 与String的区别 ...