题目是给你一堆域名,其中一些是另一些的parent,比如.com是.youku.com的parent,然后.youku.com是.service.youku.com的parent这样,然后再给你一个网址,让你在那堆域名中找到这个网址的parent里面最长的一个,然后再往前退一个返回。语言有点不好描述,举个栗子:

Domains:[
“.com”,
“.cn”
“.service.com”
“.net”
“.youku.net”
]
.
url: “yeah.hello.youku.net” 这里.net和.youku.net都是这个url的parent,其中最长的是.youku.net,再往前退一个是hello,所以返回“hello.youku.net”

虽然我觉得这道题用set倒着来就可以解决,但是看到一个Trie的做法也很不错

这里TrieNode.val不再是char, 而是String.  children array也变成了Map<String, TrieNode>

 public class LongestSubDomain {
private class TrieNode {
String str;
Map<String, TrieNode> map;
boolean isLeaf;
public TrieNode(String str) {
this.str = str;
this.map = new HashMap<>();
this.isLeaf = false;
}
}
TrieNode root = new TrieNode("#");
public static void main(String[] args) {
LongestSubDomain lsd = new LongestSubDomain();
String[] domains = {".com", ".cn", ".service.com", ".net", ".youku.net"};
String url = "yeah.hello.youku.net";
for (String str : domains) {
lsd.insert(str);
}
String res = lsd.startWith(url);
System.out.println(res);
}
public void insert(String domain) {
String[] temp = domain.split("\\.");
TrieNode node = root;
for (int i = temp.length - 1; i >= 0; i--) {
if (temp[i].length() == 0) continue;
if (node.map.containsKey(temp[i])) {
node = node.map.get(temp[i]);
} else {
TrieNode newNode = new TrieNode(temp[i]);
node.map.put(temp[i], newNode);
node = newNode;
}
}
node.isLeaf = true;
}
public String startWith(String url) {
String[] temp = url.split("\\.");
TrieNode node = root;
String res = "";
int index = temp.length - 1;
for (int i = temp.length - 1; i >= 0; i--) {
if (temp[i].length() == 0) continue;
if (node.map.containsKey(temp[i])) {
res = "." + temp[i] + res;
node = node.map.get(temp[i]);
} else {
if (!node.isLeaf) {
res = "";
} else {
index = i;
}
break;
}
}
return temp[index] + res;
}
}

我的做法:

 package uberOnsite;

 import java.util.*;

 public class LongestParent {
public class TrieNode {
String val;
Map<String, TrieNode> children;
boolean end;
public TrieNode(String str) {
val = str;
children = new HashMap<String, TrieNode>();
end = false;
}
} TrieNode root = new TrieNode(""); public void insert(String str) {
String[] arr = str.split("\\.");
TrieNode node = root;
for (int i=arr.length-; i>=; i--) {
if (arr[i].length() == ) continue;
if (!node.children.containsKey(arr[i])) {
node.children.put(arr[i], new TrieNode(arr[i]));
}
node = node.children.get(arr[i]);
}
node.end = true;
} public String findLongest(String str) {
String[] input = str.split("\\.");
TrieNode node = root;
StringBuffer res = new StringBuffer();
for (int i=input.length-; i>=; i--) {
String cur = input[i];
if (node.children.containsKey(cur)) {
res.insert(, cur);
res.insert(, ".");
node = node.children.get(cur);
}
else {
res.insert(, cur);
return res.toString();
}
}
return "";
} /**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
LongestParent sol = new LongestParent();
String[] domains = {".com", ".cn", ".service.com", ".net", ".youku.net"};
String url = "yeah.hello.youku.net";
for (String each : domains) {
sol.insert(each);
}
String res = sol.findLongest(url);
System.out.println(res);
} }

U面经Prepare: Web Address的更多相关文章

  1. VS Extension: Open Web Address with Visual Studio Browser

    使用VS 打开链接 using Microsoft.VisualStudio.Shell; using Microsoft.VisualStudio.Shell.Interop; ... public ...

  2. Some web Address

    1.可视化算法(Data Structure Visualizations) https://www.cs.usfca.edu/~galles/visualization/Algorithms.htm ...

  3. SharePoint 2013 创建web应用程序报错"This page can’t be displayed"

    错误描述 This page can’t be displayed •Make sure the web address http://centeradmin is correct. •Look fo ...

  4. ASP.NET Web API 实例

    ASP.NET Web API 入门大杂烩 创建Web API解决方案,命名为VCoinWebApi,并且创建了同名的Project,然后,创建一个Empty Project:Models,创建一个W ...

  5. Understanding Responsive Web Design: Cross-browser Compatibility

    http://www.sitepoint.com/understanding-responsive-web-design-cross-browser-compatibility/ In the las ...

  6. SharePoint 2013 创建web应用程序报错&quot;This page can’t be displayed&quot;

    错误描写叙述 This page can't be displayed •Make sure the web address http://centeradmin is correct. •Look ...

  7. kubernetes使用Traefik暴露web服务-转载51cto

    Traefix介绍(摘自网络) traefik 是一个前端负载均衡器,对于微服务架构尤其是 kubernetes 等编排工具具有良好的支持:同 nginx 等相比,traefik 能够自动感知后端容器 ...

  8. Accepting PayPal in games(完整的Paypal在Unity的支付)

      Hello and welcome back to my blog! In this article I’m going to talk about the process of acceptin ...

  9. CentOS6.5上Zabbix3.0的RPM安装【一】-安装并配置Server

    一.Environment OS:CentOS6.5 64bit [桌面版安装] Server端:192.168.201.109 ServerName Clinet端:192.168.201.199 ...

随机推荐

  1. Redis自学笔记:5.实践

    第5章实践 5.3 python与redis 5.3.1安装 pip install redis 5.3.2使用方法 自己补充内容:Ubuntu下redis开启远程连接 打开redis配置:sudo ...

  2. [BOI2007]Mokia 摩基亚

    Description: 摩尔瓦多的移动电话公司摩基亚(Mokia)设计出了一种新的用户定位系统.和其他的定位系统一样,它能够迅速回答任何形如"用户C的位置在哪?"的问题,精确到毫 ...

  3. XVIII Open Cup named after E.V. Pankratiev. Grand Prix of Saratov

    A. Three Arrays 枚举每个$a_i$,双指针出$b$和$c$的范围,对于$b$中每个预先双指针出$c$的范围,那么对于每个$b$,在对应$c$的区间加$1$,在$a$处区间求和即可. 树 ...

  4. 2.7python简历心得(重点)

    2019-2-7 20:07:20 绝地求生被盗了,...因为群邮件木马导致! 正好不要想买显卡啦!又省了好多钱!!! 努力多学技能!并且深入了解,精通自己技能!!!坚持学习!! 要学会拓展自己的技能 ...

  5. [LeetCode] Max Increase to Keep City Skyline 保持城市天际线的最大增高

    In a 2 dimensional array grid, each value grid[i][j] represents the height of a building located the ...

  6. uri&url

    统一资源标志符URI就是在某一规则下能把一个资源独一无二地标识出来. 拿人做例子,假设这个世界上所有人的名字都不能重复,那么名字就是URI的一个实例,通过名字这个字符串就可以标识出唯一的一个人.现实当 ...

  7. 解决C#中FileSystemWatcher类的Changed事件触发多次的问题

    public static void WatchFile() { FileSystemWatcher watcher = new FileSystemWatcher(); watcher.Path = ...

  8. Tarjan求割点 || Luogu P3388 【模板】割点(割顶)

    题面:P3388 [模板]割点(割顶) 题解:无 代码: #include<cstdio> #include<iostream> #include<cstring> ...

  9. 报文分析4、TCP协议的头结构

    TCP协议的头结构 来源端口(2字节) 目的端口(2字节) 序号(4字节) 确认序号(4字节) 头长度(4位) 保留(6位) URG ACK PSH RST SYN PIN 窗口大小(2字节) 校验和 ...

  10. pip3更新后install package出现ImportError: cannot import name 'main'

    linux下pip3更新后,install包出现main不能导入的情况: bear@bear:~/eclipse-workspace/Python-toolbox$ pip3 install pycr ...