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. 开发环境设置 ...
随机推荐
- scala 入门Eclipse环境搭建
scala 入门Eclipse环境搭建及第一个入门经典程序HelloWorld IDE选择并下载: scala for eclipse 下载: http://scala-ide.org/downloa ...
- IOS面试题2018/11/17
1.设计模式是什么?你知道哪些设计模式? 设计模式是一种编码经验,就是一种成熟的逻辑去处理某一种类型的事情. 1.MVC模式:model view controller,把模型,视图,控制器 层进行解 ...
- linux内核升级5.0
升级内核$ rpm --import https://www.elrepo.org/RPM-GPG-KEY-elrepo.org$ rpm -Uvh http://www.elrepo.org/elr ...
- C++中数组声名后不初始化,数组里的值都是0吗?
这得看数组的申明情况: 1.全局/静态数组 如果申明的是全局/静态数组,系统会把数组的内容自动初始化为0. 2.局部数组 如果申明的是局部数组,数组的内容会是随机的,不一定是0.如函数内声明: int ...
- hbuider配置初始
{ "forEach": { "prefix": "fec", "body": [ ".forEach(fun ...
- 跟随我在oracle学习php(3)
这次讲一下html中的列表和比较重要的表格 列表分为有序和无序,有序列表与无序列表都是块状元素 <ul>(父标签) 定义无序列表.复合标签(由父标签和子标签组成),不单独出现,用<l ...
- Vue 学习Day001
入门 基本使用 安装Vue 直接引入本地或者cdn Vue地址 使用npm 使用cli 示例 <!DOCTYPE html> <html lang="en"> ...
- 开始Flask项目
新建Flask项目. 设置调试模式. 理解Flask项目主程序. 使用装饰器,设置路径与函数之间的关系. 使用Flask中render_template,用不同的路径,返回首页.登录员.注册页. 用视 ...
- 18-11-01 pandas 学习03
[python]pandas display选项 import pandas as pd 1.pd.set_option('expand_frame_repr', False) True就是可以换行显 ...
- VC++、MFC Sqlite3数据库的使用
SQLite数据库是一种本地的轻型数据库,在存储一些本地的数据的时候,或者不需要用到Oracle,SQL2008之类的大型数据库的时候,Sqlite的优势就能够得到发挥.程序需要采集数据存储起来,可以 ...