269. Alien Dictionary火星语字典(拓扑排序)
[抄题]:
There is a new alien language which uses the latin alphabet. However, the order among letters are unknown to you. You receive a list of non-empty words from the dictionary, where words are sorted lexicographically by the rules of this new language. Derive the order of letters in this language.
Example 1:
Input:
[
"wrt",
"wrf",
"er",
"ett",
"rftt"
] Output:"wertf"
Example 2:
Input:
[
"z",
"x"
] Output:"zx"
Example 3:
Input:
[
"z",
"x",
"z"
] Output:""
Explanation: The order is invalid, so return""
.
[暴力解法]:
时间分析:
空间分析:
[优化后]:
时间分析:
空间分析:
[奇葩输出条件]:
[奇葩corner case]:
相同的c2,只需要存一次(没有就新存 有就不存),反正如果存过 就必须退出了(返回一组即可 不用重复加)
"["za","zb","ca","cb"]" How is this test case handled.
It should give out an empty string as the order can not be decided from the words given. but instead it returns "azbc". 回答:we can only now z-> c and a-> b
so 'azbc' is right but right result is not limited to this only one.
you can test 'zcab', 'abzc' are will all right as it is topological sort
结果字符串长度不等于度数(不是不等于单词数)
["z","z"]的度数 = 字符串长度1
正常,应该返回"z"而不是""
[思维问题]:
忘了拓扑排序用BFS怎么写了
[英文数据结构或算法,为什么不用别的数据结构或算法]:
DFS写拓扑排序似乎都很麻烦
存点到点的对应关系,用map(其中的字符必须存成包装类Character,但是循环的时候可以写char)
//存每个点的入度
Map<char, Integer> degree = new HashMap<>();
//存c1到c2...的对应关系
Map<char, Set<char>> map = new HashMap<>();
[一句话思路]:
[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):
[画图]:
[一刷]:
- 出现index[i + 1]时,需要提前备注把上线变为n - 1
[二刷]:
- BFS的时候别忘了吧把取出的c1添加到结果中去. 而且必须现有c1的key才能扩展。
[三刷]:
[四刷]:
[五刷]:
[五分钟肉眼debug的结果]:
[总结]:
c1 c2都要先判断一下有没有,再存
[复杂度]:Time complexity: O(n^2 单词数*字母数) Space complexity: O(n)
[算法思想:递归/分治/贪心]:
[关键模板化代码]:
BFS的存储和扩展是两个独立的步骤,扩展时必须先判断key是否存在,再做扩展
[其他解法]:
[Follow Up]:
[LC给出的题目变变变]:
[代码风格] :
[是否头一次写此类driver funcion的代码] :
class Solution {
public String alienOrder(String[] words) {
//ini:HashMap<Char, Integer> degree, store all chars into hashmap, String res
//存每个点的入度
Map<Character, Integer> degree = new HashMap<>();
//存c1到c2...的对应关系
Map<Character, Set<Character>> map = new HashMap<>();
String res = "";
for (String word : words) {
for (char c : word.toCharArray())
degree.put(c, 0);
} //compare and store, n - 1
for (int i = 0; i < words.length - 1; i++) {
String cur = words[i];
String next = words[i + 1];
int smallerLen = Math.min(cur.length(), next.length()); for (int j = 0; j < smallerLen; j++) {
char c1 = cur.charAt(j);
char c2 = next.charAt(j); if (c1 != c2) {
//new set
Set<Character> set = new HashSet<>();
//contains c1
if (map.containsKey(c1)) set = map.get(c1); //not contain c2
if (!set.contains(c2)) {
set.add(c2);
map.put(c1, set);
degree.put(c2, degree.get(c2) + 1);
}
break;
}
}
} //bfs, get answer
Queue<Character> q = new LinkedList<>();
for (char c : degree.keySet()) {
if (degree.get(c) == 0) q.offer(c);
} while (!q.isEmpty()) {
char c1 = q.remove();
res += c1;
if (map.containsKey(c1)) {
for (char c2 : map.get(c1)) {
degree.put(c2, degree.get(c2) - 1);
if (degree.get(c2) == 0) q.offer(c2);
}
}
} //cc at end
if (res.length() != degree.size()) return ""; return res;
}
}
269. Alien Dictionary火星语字典(拓扑排序)的更多相关文章
- [leetcode]269. Alien Dictionary外星字典
There is a new alien language which uses the latin alphabet. However, the order among letters are un ...
- [LeetCode] 269. Alien Dictionary 外文字典
There is a new alien language which uses the latin alphabet. However, the order among letters are un ...
- 269. Alien Dictionary 另类字典 *HARD*
There is a new alien language which uses the latin alphabet. However, the order among letters are un ...
- [LeetCode] 269. Alien Dictionary 另类字典
There is a new alien language which uses the latin alphabet. However, the order among letters are un ...
- LeetCode 269. Alien Dictionary
原题链接在这里:https://leetcode.com/problems/alien-dictionary/ 题目: There is a new alien language which uses ...
- 269. Alien Dictionary
题目: There is a new alien language which uses the latin alphabet. However, the order among letters ar ...
- LeetCode编程训练 - 拓扑排序(Topological Sort)
拓扑排序基础 拓扑排序用于解决有向无环图(DAG,Directed Acyclic Graph)按依赖关系排线性序列问题,直白地说解决这样的问题:有一组数据,其中一些数据依赖其他,问能否按依赖关系排序 ...
- 算法与数据结构基础 - 拓扑排序(Topological Sort)
拓扑排序基础 拓扑排序用于解决有向无环图(DAG,Directed Acyclic Graph)按依赖关系排线性序列问题,直白地说解决这样的问题:有一组数据,其中一些数据依赖其他,问能否按依赖关系排序 ...
- [USACO12DEC]第一!First!(字典树,拓扑排序)
[USACO12DEC]第一!First! 题目描述 Bessie has been playing with strings again. She found that by changing th ...
随机推荐
- ***XX-net 和 proxyee-down
看连接吧,留着方便自己查看 https://github.com/XX-net/XX-Net https://github.com/monkeyWie/proxyee-down/blob/master ...
- WCF引用方式之IIS方式寄宿服务
通过IIS方式寄宿服务 之前的例子是将控制台作为WCF的寄宿方式或者是直接添加契约项目的引用,然后通过配置或者是ChannelFactory的形式进行创建服务对象,其实在大多的开发中以IIS的形式创建 ...
- happynear_caffe编译时,缺少头文件caffe.pb.h的问题
由于一些问题,需要编译caffe 的windows版本,用的是happynear的caffe版本,在caffe.pb.h遇到了问题 如何生成 caffe.pb.h 将protobuf 里的 proto ...
- DS02--线性表
一.PTA实验作业 题目1:线性表元素的区间删除 给定一个顺序存储的线性表,请设计一个函数删除所有值大于min而且小于max的元素.删除后表中剩余元素保持顺序存储,并且相对位置不能改变. 1. 设计思 ...
- elasticsearch 5.0 获取 TransportClient 操作客户端java API
本文转载自:http://blog.csdn.net/likui1314159/article/details/53233881 elasticsearch 5.0 获取 TransportClien ...
- Linux:WebServer(Nginx 虚拟主机配置与伪静态实现)
ps + 查看方式 | grep + 服务/端口/软件等:查看状态: 一.基本操作 Nginx 多用于商业系统: 一个端口只能被一个服务使用: Nginx 可以同时监听多个端口,也就是配置时, ...
- 获取响应数据___JSON Extractor 后置处理器
对于大部分请求返回的结果,都是json,有一个更方便使用的插件:JSON Extractor 不过得首先下载插件 https://jmeter-plugins.org/wiki/JSONPathExt ...
- Java中的<< 和 >> 和 >>> 详细分析
<<表示左移移,不分正负数,低位补0: 注:以下数据类型默认为byte-8位 左移时不管正负,低位补0 正数:r = 20 << 2 20的二进制补码:0001 0100 向左 ...
- SpringBoot中RedisTemplate订阅发布对象
解说 RedisMessageListenerContainer Redis订阅发布的监听容器,你的消息发布.订阅配置都必须在这里面实现 addMessageListener(MessageListe ...
- UISegmentedControl-iOS
//建立UISegmentedControl的数组 NSArray *segmentedArray = [NSArray arrayWithObjects:@"线下培训",@&qu ...