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 ...
随机推荐
- 【Python教程】《零基础入门学习Python》(小甲鱼)
[Python教程]<零基础入门学习Python>(小甲鱼) 讲解通俗易懂,诙谐. 哈哈哈. https://www.bilibili.com/video/av27789609
- 洛谷【P4551】最长异或路径
浅谈\(Trie\):https://www.cnblogs.com/AKMer/p/10444829.html 题目传送门:https://www.luogu.org/problemnew/show ...
- 如何使用JSON格式 POST数据到服务器
1. JSON的数据格式a) 按照最简单的形式,可以用下面这样的 JSON 表示名称/值对: { "firstName": "Brett" } b) 可以创建包 ...
- Vim下的Web开发之html,CSS,javascript插件
Vim下的Web开发之html,CSS,javascript插件 HTML 下载HTML.zip 解压HTML.zip,然后将里面的所有文件copy到C:\Program Files\Vim\vi ...
- 微信web端生成支付二维码
授权获取二维码类: <?php /** * Trade类 * @author xyyphp * @date 2016/10/10 */ abstract class TradeControlle ...
- Java里的常用运算符及其优先级顺序
知道了八种基本数据类型后,在使用中弄清运算符的优先级是很有必要的.具体如下图: 这里需要注意的是,强制类型转换的优先级是位于乘除前面而处于单目运算符后面的,这是比较容易出错的地方.比如用Math.R ...
- java代码---charAt()和toCharry()的用法
总结:charAt()是返回字符型,把字符串拆分成某个字符,返回指定位置的字符.可以把字符串看成char型数组 package com.sads; ///输出一个大写英文字母的 public clas ...
- 斗地主AI
斗地主AI设计 一.牌型 1 火箭:大小王在一起的牌型,即双王牌,此牌型最大,什么牌型都可以打. 2 炸弹:相同点数的四张牌在一起的牌型,比如四条A.除火箭外,它可以打 ...
- Linux学习笔记 -- 文件包含
简述 简单来讲,shell 中的文件包含指的是在一个文件中引用另外一个文件.通过这种方式,我们可以将一些公用的代码封装为一个独立的文件,并在需要的时候引用它即可. 语法 . filename # 注意 ...
- java软件设计模式——单例设计模式中的【饿汉式】与 【懒汉式】示例
以下为单例设计模式中的两种经典模式的代码示意: 单例设计模式(spring框架IOC,默认创建的对象都是单例的): 饿汉式: public class SingleClass { private Si ...