Description

Build tries from a list of <word, freq> pairs. Save top 10 for each node.

Example

Example1

Input:
<"abc", 2>
<"ac", 4>
<"ab", 9>
Output:<a[9,4,2]<b[9,2]<c[2]<>>c[4]<>>>
Explanation:
Root
/
a(9,4,2)
/ \
b(9,2) c(4)
/
c(2)

Example2

Input:
<"a", 10>
<"c", 41>
<"b", 50>
<"abc", 5>
Output: <a[10,5]<b[5]<c[5]<>>>b[50]<>c[41]<>>

思路:先遍历一遍数,然后重构树进行求解
/**
* Definition of TrieNode:
* public class TrieNode {
* public NavigableMap<Character, TrieNode> children;
* public List<Integer> top10;
* public TrieNode() {
* children = new TreeMap<Character, TrieNode>();
* top10 = new ArrayList<Integer>();
* }
* }
*/
public class TrieService { private TrieNode root = null; public TrieService() {
root = new TrieNode();
} public TrieNode getRoot() {
// Return root of trie root, and
// lintcode will print the tree struct.
return root;
} // @param word a string
// @param frequency an integer
public void insert(String word, int frequency) {
// Write your cod here
TrieNode cur = root;
int n = word.length(); for (int i = 0; i < n; ++i) {
Character c = word.charAt(i);
if (!cur.children.containsKey(c))
cur.children.put(c, new TrieNode()); cur = cur.children.get(c);
addFrequency(cur.top10, frequency);
}
} public void addFrequency(List<Integer> top10, int frequency) {
top10.add(frequency);
int n = top10.size();
int index = n - 1;
while (index > 0) {
if (top10.get(index) > top10.get(index - 1)) {
int temp1 = top10.get(index);
int temp2 = top10.get(index - 1);
top10.set(index, temp2);
top10.set(index - 1, temp1);
index -= 1;
} else
break;
}
if (n > 10)
top10.remove(n - 1);
}
}

  

Trie Service的更多相关文章

  1. 通过AngularJS实现前端与后台的数据对接(二)——服务(service,$http)篇

    什么是服务? 服务提供了一种能在应用的整个生命周期内保持数据的方法,它能够在控制器之间进行通信,并且能保证数据的一致性. 服务是一个单例对象,在每个应用中只会被实例化一次(被$injector实例化) ...

  2. Azure Service Fabric 开发环境搭建

    微服务体系结构是一种将服务器应用程序构建为一组小型服务的方法,每个服务都按自己的进程运行,并通过 HTTP 和 WebSocket 等协议相互通信.每个微服务都在特定的界定上下文(每服务)中实现特定的 ...

  3. 无法向会话状态服务器发出会话状态请求。请确保 ASP.NET State Service (ASP.NET 状态服务)已启动,并且客户端端口与服务器端口相同。如果服务器位于远程计算机上,请检查。。。

    异常处理汇总-服 务 器 http://www.cnblogs.com/dunitian/p/4522983.html 无法向会话状态服务器发出会话状态请求.请确保 ASP.NET State Ser ...

  4. C#创建、安装、卸载、调试Windows Service(Windows 服务)的简单教程

    前言:Microsoft Windows 服务能够创建在它们自己的 Windows 会话中可长时间运行的可执行应用程序.这些服务可以在计算机启动时自动启动,可以暂停和重新启动而且不显示任何用户界面.这 ...

  5. java中Action层、Service层和Dao层的功能区分

    Action/Service/DAO简介: Action是管理业务(Service)调度和管理跳转的. Service是管理具体的功能的. Action只负责管理,而Service负责实施. DAO只 ...

  6. org.jboss.deployment.DeploymentException: Trying to install an already registered mbean: jboss.jca:service=LocalTxCM,name=egmasDS

    17:34:37,235 INFO [Http11Protocol] Starting Coyote HTTP/1.1 on http-0.0.0.0-8080 17:34:37,281 INFO [ ...

  7. Android—Service与Activity的交互

    service-Android的四大组件之一.人称"后台服务"指其本身的运行并不依赖于用户可视的UI界面 实际开发中我们经常需要service和activity之间可以相互传递数据 ...

  8. angularjs 1 开发简单案例(包含common.js,service.js,controller.js,page)

    common.js var app = angular.module('app', ['ngFileUpload']) .factory('SV_Common', function ($http) { ...

  9. IIS启动失败,启动Windows Process Activation Service时,出现错误13:数据无效 ;HTTP 错误 401.2 - Unauthorized 由于身份验证头无效,您无权查看此页

    因为修改过管理员账号的密码后重启服务器导致IIS无法启动,出现已下异常 1.解决:"启动Windows Process Activation Service时,出现错误13:数据无效&quo ...

随机推荐

  1. [转帖]统一操作系统 UOS 龙芯版上线

    统一操作系统 UOS 龙芯版上线 看评论很有必要 搞一波 深度的操作系统了https://www.oschina.net/news/112065/chinauos-with-loongson?p=4 ...

  2. eDiary

    多年过去,您经历了BBS.论坛.博客.推特.微博 ...,但在电脑的某个 角落,eDiary始终为你守护发自你内心的声音. eDiary的作用不仅仅在于写日记,您也可以用它来记流水帐.写工作日志, 记 ...

  3. NOI2019网络同步赛游记

    我发的邮件**f没收到,后来去专门询问才整到一个名额(估计是嫌我太菜,参加了也是垫底) day -1 上午写了到类似随机游走的高斯消元期望dp,然后颓颓颓 下午打洛咕月赛.T1一直50pts,后来才知 ...

  4. IntelliJ IDEA启动一个普通的java web项目的配置

    文:朱季谦 这是我很久以前刚开始用IntelliJ IDEA时记录的笔记,正好赶上最近离职了,可以有比较多的时间把以前的记录整理一下,可以让刚接触到IntelliJ IDEA的童鞋学习如何在Intel ...

  5. [Windows] - DNS防污染工具Pcap_DNSProxy

    最近试过非常多的DNS防污染工具(包括:dnsforwarder.dnsforwarder.dnscrypt-proxy.SimpleDNSCrypt等),感觉这个Pcap_DNSProxy简单.快捷 ...

  6. 2019-07-30 ThinkPHP文件上传

    文件上传就是获取到待上传文件的临时路径,把它移动到服务器下的相应文件夹中. 文件上传,必须在表单中的form标签中写入:enctype="multipart/form-data" ...

  7. Beego 学习笔记9:Boostrap使用介绍

    BootStrap布局 1>     下载地址: http://v3.bootcss.com/getting-started/#download 根据自己的需要,下载不同的版本.我这里使用的是1 ...

  8. Beego 学习比较8:SQL语句

    SQL语句 1>     常用的SQL语句 1->新增  insert into 表名(字段A,字段B,…) Values(字段A值,字段B值,…) 2->更新  update 表名 ...

  9. echarts 3D地球实现自动旋转

    素材已上传至https://gitee.com/i1520/echarts3DEarth.git     https://github.com/i1520/echarts3DEarth 1.引入js文 ...

  10. windows 2003 windows 2008 windows 2012 导出域控hash的方法

    quarkspwdump作者介绍的用法: 1. Windows 2008       Microsoft recently implements VSS (Volume Shadow Copy Ser ...