Trie Service
Description
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的更多相关文章
- 通过AngularJS实现前端与后台的数据对接(二)——服务(service,$http)篇
什么是服务? 服务提供了一种能在应用的整个生命周期内保持数据的方法,它能够在控制器之间进行通信,并且能保证数据的一致性. 服务是一个单例对象,在每个应用中只会被实例化一次(被$injector实例化) ...
- Azure Service Fabric 开发环境搭建
微服务体系结构是一种将服务器应用程序构建为一组小型服务的方法,每个服务都按自己的进程运行,并通过 HTTP 和 WebSocket 等协议相互通信.每个微服务都在特定的界定上下文(每服务)中实现特定的 ...
- 无法向会话状态服务器发出会话状态请求。请确保 ASP.NET State Service (ASP.NET 状态服务)已启动,并且客户端端口与服务器端口相同。如果服务器位于远程计算机上,请检查。。。
异常处理汇总-服 务 器 http://www.cnblogs.com/dunitian/p/4522983.html 无法向会话状态服务器发出会话状态请求.请确保 ASP.NET State Ser ...
- C#创建、安装、卸载、调试Windows Service(Windows 服务)的简单教程
前言:Microsoft Windows 服务能够创建在它们自己的 Windows 会话中可长时间运行的可执行应用程序.这些服务可以在计算机启动时自动启动,可以暂停和重新启动而且不显示任何用户界面.这 ...
- java中Action层、Service层和Dao层的功能区分
Action/Service/DAO简介: Action是管理业务(Service)调度和管理跳转的. Service是管理具体的功能的. Action只负责管理,而Service负责实施. DAO只 ...
- 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 [ ...
- Android—Service与Activity的交互
service-Android的四大组件之一.人称"后台服务"指其本身的运行并不依赖于用户可视的UI界面 实际开发中我们经常需要service和activity之间可以相互传递数据 ...
- angularjs 1 开发简单案例(包含common.js,service.js,controller.js,page)
common.js var app = angular.module('app', ['ngFileUpload']) .factory('SV_Common', function ($http) { ...
- IIS启动失败,启动Windows Process Activation Service时,出现错误13:数据无效 ;HTTP 错误 401.2 - Unauthorized 由于身份验证头无效,您无权查看此页
因为修改过管理员账号的密码后重启服务器导致IIS无法启动,出现已下异常 1.解决:"启动Windows Process Activation Service时,出现错误13:数据无效&quo ...
随机推荐
- 『Blocks 区间dp』
Blocks Description Some of you may have played a game called 'Blocks'. There are n blocks in a row, ...
- MATLAB datenum日期转换为Python日期
摘要 MATLAB datenum时间格式参数众多,本文只简单关注 units 参数,即基准年份和计时度量(天.小时). 命令行演示在 ipython 和 Octave 中进行. 示例1:小时制,基准 ...
- Maven distributionManagement 分发构件至远程仓库
https://blog.csdn.net/qq827245563/article/details/82661583 maven发布到本地仓库,和私服https://blog.csdn.net/u01 ...
- 创建和使用CI / CD管道【译】【原】
在GitLab 8.8中引入. 介绍 管道是持续集成,交付和部署的顶级组件. 管道包括: 定义要运行的作业的作业.例如,代码编译或测试运行. 定义何时以及如何运行的阶段.例如,该测试仅在代码编译后运行 ...
- SpringBoot构建RESTful API
1.RESTful介绍 RESTful是一种软件架构风格! RESTful架构风格规定,数据的元操作,即CRUD(create, read, update和delete,即数据的增删查改)操作,分别对 ...
- Java自学-数字与字符串 比较字符串
Java 比较字符串 示例 1 : 是否是同一个对象 str1和str2的内容一定是一样的! 但是,并不是同一个字符串对象 package character; public class TestSt ...
- js 递归遍历对象 插入属性 遍历树结构
// 向 info下面 每一项 插入 isShow test() { const _this = this; _this.info.isShow = false; let iteration = fu ...
- ORACLE数据库导出表,字段名,长度,类型,字段注释,表注释语句
转自:https://www.cnblogs.com/superming/p/11040455.html --数据库导出表,字段名,长度,类型,字段注释,表注释语句 SELECT T1.TABLE_N ...
- Spring boot应用如何支持https
首先使用命令行生成一个keystore文件: keytool -genkey -alias tomcat -keyalg RSA -keystore ./jerry.keystore 保存到本地项目文 ...
- Pycharm安装模块提示module 'pip' has no attribute 'main'的问题
解决pycharm问题:module 'pip' has no attribute 'main' 转自: <解决pycharm问题:module 'pip' has no attribute ' ...