/*
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. jackson中@JsonProperty、@JsonIgnore等常用注解总结

    本文为博主原创,未经允许不得转载: 最近用的比较多,把json相关的知识点都总结一下,jackjson的注解使用比较频繁, jackson的maven依赖 <dependency> < ...

  2. python+selenium,实现带有验证码的自动化登录功能

    python+selenium的环境准备,请自行安装完成,这里直接贴代码,方便做项目时直接使用. import time from selenium import webdriver from PIL ...

  3. js里面关于日期转换的问题

    我们拿到一个日期字符串:"2017-09-03",我们用new Date("2017-09-03")去转换成日期格式的时候,发现在火狐会报错,是因为火狐不支持这 ...

  4. 【微信小程序开发】使用button标签的open-type="getUserInfo"引导用户去授权

    一. 前言 小程序官方文档,上面说明 > wx.getUserInfo(OBJECT) 注意:此接口有调整,使用该接口将不再出现授权弹窗,请使用 <button open-type=&qu ...

  5. MVC实战之排球计分(八)——软件制作总结

    此系列博客目的是制作一款排球计分程序.这系列博客将讲述此软件的 各个功能的设计与实现.到这篇博客,此系列博客就算是结束了. 在最后的这篇博客里 我们来做一些总结. 一,制作此程序,我们使用的是MVC框 ...

  6. CDI

    CDI,JAVA用语 Java EE CDI 主要使用@Inject注解来实现依赖注入,把受管理的bean注入到由容器管理的其它资源中去.在本教程中,我们将会介绍在CDI环境下几种不同的可选策略来实现 ...

  7. 监听图片src发生改变时的事件

    $img.on('load', function() { $img.attr("src", getBase64Image($img.get(0))); $img.off('load ...

  8. 50个常用的Linux命令(二)sed

    [root@localhost cee]# echo this thisthisthis |sed 's/this/THIS/g'THIS THISTHISTHIS[root@localhost ce ...

  9. 2.4 利用FTP服务器下载和上传目录

    利用FTP服务器下载目录 import os,sys from ftplib import FTP from mimetypes import guess_type nonpassive = Fals ...

  10. Future模式介绍及入门使用

    FutureClient代码实现: package com.hjf.future; public class FutureClient { /** * 请求客户端 * @param queryStr ...