/*
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的更多相关文章

  1. Android HTTPS(2)HttpURLConnection.getInputStream异常的原因及解决方案

    Common Problems Verifying Server Certificates InputStream in = urlConnection.getInputStream(); getIn ...

  2. struts升级到最高版本后遇到的问题。关于actionmessage传递问题。

    Struts2升级到最新版本遇到的一些问题 首先是更换对应的jar,如asm.common.ongl.struts等等.更换后发现系统启动不了,按照网上的介绍,先后又更新了slf4j-log4j12- ...

  3. Key Technologies Primer 读书笔记,翻译 --- Struct 学习 1

    原文链接:https://struts.apache.org/primer.html 本来想写成读书笔记的,结果还是变成翻译,谨作记录,学习.   1.HTML -- 见我前面文章 2.Interne ...

  4. ssh端口转发功能

    一.SSH 端口转发能够提供两大功能: 1.加密SSH Client 端至SSH Server 端之间的通讯数据 2.突破防火墙的限制完成一些之前无法建立的TCP 连接  (隧道功能) 二:SSH端口 ...

  5. [转载] google mock cookbook

    原文: https://code.google.com/p/googlemock/wiki/CookBook Creating Mock Classes Mocking Private or Prot ...

  6. 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- ...

  7. Google C++ Style Guide

    Background C++ is one of the main development languages used by many of Google's open-source project ...

  8. Google 如何修复 TrustManager 实施方式不安全的应用

    引用谷歌市场的帮助说明:https://support.google.com/faqs/answer/6346016 本文面向的是发布的应用中 X509TrustManager 接口实施方式不安全的开 ...

  9. java 请求 google translate

    // */ // ]]> java 请求 google translate Table of Contents 1. 使用Java获取Google Translate结果 1.1. 开发环境设置 ...

随机推荐

  1. HDU 4283 You Are the One ——区间dp

    参考了许多大佬  尤其是https://blog.csdn.net/woshi250hua/article/details/7973824这一篇 ,最后我再加一点我的见解. 大意是 给定一个序列,序列 ...

  2. 浅谈 cosos2d-x 的ImageView和Sprite的区别

    ImageView 1. ImageView是继承于Widget的,是cocos2d-x的基类 2. 实现了类似于按钮监听的事件,通过addTouchEventListener添加事件 var ima ...

  3. Kafka-Record(消息格式)

    注:本文依赖于kafka-0.10.0.1-src kafka消息格式是经过多个版本的演变的,本文只说0.10.0.1版本的消息格式. 消息格式如图1所示: 图1 CRC:用于校验消息内容.占4个字节 ...

  4. guxh的python笔记六:类的属性

    1,私有属性 class Foo: def __init__(self, x): self.x = x 类的属性在实例化之后是可以更改的: f = Foo(1) print(f.x) # 1 f.x ...

  5. Introduce oneself

    首先,我是一个男生, 我很喜欢打游戏,钟爱LOL,接触它已经7年了.虽然还是很菜,但就是喜欢.选择计算机科学与技术这个专业呢,就是因为喜欢电脑,可以和室友一起开黑,然而室友都不玩,有点难受. 此外呢, ...

  6. 一个二维码如何自动识别是安卓(Android)还是苹果(IOS)

    思考问题: 通常,我们开发一个APP,有Android版本.IOS版本. 但是只有一个二维码?怎么办呢? 怎么让IOS用户扫描二维码下载IOS版本,Android用户扫描二维码下载到Android版本 ...

  7. 如何设置在html中保留超链接格式但不实现跳转

    ---恢复内容开始--- 老师布置了一个任务,要求用户登录或者不登录都会有一个主页(home.jsp),如果登录的话就会跳转至登录界面(login.jsp),在登录界面中有个验证码,还要求有个和很多登 ...

  8. hbuider配置初始

    { "forEach": { "prefix": "fec", "body": [ ".forEach(fun ...

  9. mybatis的jdbcType和javaType、oracle,MySQL的对应类型

    JdbcType介绍 数据库列字段都是有类型的,不同的数据库有不同的类型.为了表示这些数据类型,Java源码是采用枚举来定义的: public enum JDBCType implements SQL ...

  10. 词向量编码 word2vec

    word2vec word2vec 是Mikolov 在Bengio Neural Network Language Model(NNLM)的基础上构建的一种高效的词向量训练方法. 词向量 词向量(w ...