Google - Reconstruct To Chain
/*
4. 给你一串input,比如:
A -> B
B -> C
X -> Y
Z -> X
。
。
。
然后让你设计一个data structure来存这些关系,最后读完了以后呢要输出这些关系链:[A -> B -> C, Z -> X -> Y]
如果遇到invalid的case就直接raise error,比如你已经有了A->B->C,这时候给你一个D->C或者B->D就是invalid的。
followup我感觉就是他看你什么case没有cover就提出来然后问你怎么改你的代码和data structure
比如遇到A->B两次,再比如遇到环
这题相当开放,面试官说他遇到过4,5种不同的解法,总之就是最好保证insert是O(1), reconstruct是O(n) 补充内容 (2018-11-19 01:02):
第四题好像说是关系链有点误导大家,它题目的意思更像是directed graph,然后每次读进来的都是一条edge,要求是你的graph里只能有链条,不能有branch,不能有cycle,所以假设你有A->B->C,这时候又来了一个A->C算错
*/
public class Main {
public static void main(String[] args) { String[] strs = {"A -> B", "B -> C", "X -> Y", "Z -> X"};
try{
Solution s = new Solution();
s.insert(strs);
s.reconstruct();
//System.out.println("Hello World!");
}
catch(Exception e){
e.printStackTrace();
} }
} class Solution{ class Node {
char val;
Node pre;
Node next; public Node(char v){
val = v;
}
} HashMap<Character, Node> map= new HashMap<>();
HashSet<Character> roots = new HashSet<>(); public void insertSingle(String str) throws Exception{
String[] strs = str.split(" -> ");
char parent = strs[0].charAt(0);
char child = strs[1].charAt(0);
//check parent side
Node pNode = null;
if(!map.containsKey(parent)){
pNode = new Node(parent);
map.put(parent, pNode);
roots.add(parent);
}
else{
if(map.get(parent).next != null && map.get(parent).next.val != child){
throw new Exception("multiple children!");
}
pNode = map.get(parent);
}
//check child side
Node cNode = null;
if(!map.containsKey(child)){
cNode = new Node(child);
map.put(child, cNode);
}
else{
if(map.get(child).pre != null && map.get(child).pre.val != parent){
throw new Exception("multiple parents!");
}
if(roots.contains(child)){
roots.remove(child);
}
cNode = map.get(child);
}
pNode.next = cNode;
cNode.pre = pNode;
} public void insert(String[] strs) throws Exception{
for(String str : strs){
insertSingle(str);
}
}
//cycle will be detected here by adding an array to check if the node is visited or not.
public void reconstruct() throws Exception{ for(char root : roots){
Node node = map.get(root);
while(node != null){
System.out.print(node.val);
node = node.next;
}
System.out.println("");
} }
}
Google - Reconstruct To Chain的更多相关文章
- Android HTTPS(2)HttpURLConnection.getInputStream异常的原因及解决方案
Common Problems Verifying Server Certificates InputStream in = urlConnection.getInputStream(); getIn ...
- struts升级到最高版本后遇到的问题。关于actionmessage传递问题。
Struts2升级到最新版本遇到的一些问题 首先是更换对应的jar,如asm.common.ongl.struts等等.更换后发现系统启动不了,按照网上的介绍,先后又更新了slf4j-log4j12- ...
- Key Technologies Primer 读书笔记,翻译 --- Struct 学习 1
原文链接:https://struts.apache.org/primer.html 本来想写成读书笔记的,结果还是变成翻译,谨作记录,学习. 1.HTML -- 见我前面文章 2.Interne ...
- ssh端口转发功能
一.SSH 端口转发能够提供两大功能: 1.加密SSH Client 端至SSH Server 端之间的通讯数据 2.突破防火墙的限制完成一些之前无法建立的TCP 连接 (隧道功能) 二:SSH端口 ...
- [转载] google mock cookbook
原文: https://code.google.com/p/googlemock/wiki/CookBook Creating Mock Classes Mocking Private or Prot ...
- How Google Backs Up The Internet Along With Exabytes Of Other Data
出处:http://highscalability.com/blog/2014/2/3/how-google-backs-up-the-internet-along-with-exabytes-of- ...
- Google C++ Style Guide
Background C++ is one of the main development languages used by many of Google's open-source project ...
- Google 如何修复 TrustManager 实施方式不安全的应用
引用谷歌市场的帮助说明:https://support.google.com/faqs/answer/6346016 本文面向的是发布的应用中 X509TrustManager 接口实施方式不安全的开 ...
- java 请求 google translate
// */ // ]]> java 请求 google translate Table of Contents 1. 使用Java获取Google Translate结果 1.1. 开发环境设置 ...
随机推荐
- null与undefined的区别
null和undefined是JavaScript五种基本数据类型中的两种. null是一个特殊值,但我们常常误解它,有时候我们会把它和另一个数据类型undefined的含义互相混淆. 首先我们来了解 ...
- E: Unable to locate package git
git can’t install 报错信息: root@281eef85bb5d:~# apt-get install git Reading package lists... Done Build ...
- Thinkphph 使用RelationModel的三表关联查询机制
有如下三个表 a表 b表 c表id bid other id cid other id other a表的bid关联b表的id,b表的cid关联c表的id 现在需要查询a表的时候顺带把b表和c表的相关 ...
- _map_char_stats
可以控制玩家进入地图后进行属性平衡. `comment` 备注 `map` 地图ID `vip`vip等级 `shengming`生命 `liliang` 力量 `minjie` 敏捷 `zhili` ...
- Harbor私有仓库中如何彻底删除镜像释放存储空间?
简介: Harbor私有仓库运行一段时间后,仓库中存有大量镜像,会占用太多的存储空间.直接通过Harbor界面删除相关镜像,并不会自动删除存储中的文件和镜像.需要停止Harbor服务,执行垃圾回收命令 ...
- [python]python2与python3版本的区别
python2和python3的区别 区别: print函数 整数相除 Unicode 异常处理 xrange map函数 不支持has_key print函数: Python 2: print是语句 ...
- legend2---开发日志10(ajax请求的方法是否同样会执行base控制器里面的方法)
legend2---开发日志10(ajax请求的方法是否同样会执行base控制器里面的方法) 一.总结 一句话总结:会执行的,所以写base控制器里面的方法要注意,base控制器里面的方法要以查数据为 ...
- dedecmsv5.7 ueditor编辑器上传视频/修改,视频显示空白,解决方案
dedecmsv5.7 ueditor 在上传视频之后,显示空白.其实是有视频的,就是显示空白.找了资料,记录一下. 解决方案: 找到include下面的ueditor下面的ueditor.all.j ...
- openLDAP安装时无法操作根节点数据,提示的是This base cannot be created with PLA.
1.无法操作根节点数据,提示的是This base cannot be created with PLA. 解决办法 1)添加一个base.ldif文件,里面的dc和配置文件里的保持一致即可 dn: ...
- python入门-基础语法
一.变量 定义字符串要加单引号‘’ 变量命名规范: 变量名只能是字母.数字或下划线的任意组合 变量名的第一个字符不能是数字 变量名不能用关键字 变量名不要用中文 变量名不要太长,区分大小写 面就用单引 ...