<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 ...
随机推荐
- Ted:1 Vulnhub Walkthrough
主机层面端口扫描: ╰─ nmap -p1-65535 -sV -A 10.10.202.134 Starting Nmap 7.70 ( https://nmap.org ) at 2019-08- ...
- [Go]TCP服务中读写进行协程分离
读写两部分进行一下分离,中间通过chan进行传递数据 ,这样可以方便的在write中进行一些业务处理 single/snet/tcpconn.go package snet import ( &quo ...
- Keystone安装与配置
一.实验目的: 1.掌握OpenStack环境搭建的基础工作 2.掌握keystone的安装与配置方法 3.掌握keystone基础接口的调用方法 二.实验步骤: 1.利用最初创建的快照克隆两台Cen ...
- 如何使用第三方ui库vant-weapp
如何使用第三方ui库vant-weapp 1==>创建文件夹demo 2==> 在小程序 中打开 注意 要先在小程序中打开 如果要想在小程序的开发工具中打开某一个 文件夹 要么是空文件夹 ...
- 《移动WEB前端高级开发实践@www.java1234.com.pdf》——2
5.3 作用域.闭包和this let 声明的变量只存在于其所在的代码块中 由于 JS 是基于词法(静态)作用域的语言,词法作用域的含义是在函数定义时就确定了作用域,而不是函数执行时再确定 calcu ...
- dom元素的tabindex属性介绍及在vue项目中的应用
dom元素的tabindex属性介绍及在vue项目中的应用 tabindex属性作用 让普通dom元素变为可聚焦的元素 让普通dom元素可以参与顺序键盘导航(通常使用Tab键,因此得名). tabin ...
- 设计模式-工厂模式(Factory)(创建型模式)
以下代码来源: 设计模式精解-GoF 23种设计模式解析附C++实现源码 //Product.h #pragma once class Product { public: ; protected: P ...
- Python Django 支付宝 扫码支付
安装python-alipay-sdk pip install python-alipay-sdk --upgradepip install crypto 如果是python 2.7安装0.6.4这个 ...
- Metasploit从文件中读取目标地址
本文简单介绍如何使用Metasploit从文件中读取目标地址,来执行检测. 以检测MS17-010漏洞为例,在设定RHOSTS参数时,可设定目标地址范围和CIDR地址块,设定单个IP的目标也是可以的. ...
- ubuntu18.04 安装 QQ
参照大佬文章https://www.lulinux.com/archives/1319 我将安装过程需要的命令行总结出来,便于直接快速安装. # 安装 wine git clone https://g ...