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 ...
随机推荐
- (火狐浏览器)前端以FormData类形成表单(含文件),通过ajax提交,PHP后端iconv()报“文件名含有非法字符”且POST中的‘Ttitle’丢失
[错误信息]后端获取的POST中丢失了Ttile,文件名乱码导致iconv()出错,利用mb_detect_encoding()函数检测输入的字符串,返回false; mb_detect_encodi ...
- TestNG系列(一)TestNG+Eclipse环境配置
前提 1.JDK的安装与环境变量的配置 2.Eclipse的下载与安装 以上这些是基础中的基础,不再详细介绍. Eclipse安装TestNG插件 打开eclipse--->help---> ...
- golang 学习笔记 使用cmd
package main import ( "bytes" "fmt" "os/exec" ) func main() { cmd0 := ...
- 浅谈maven setting.xml 设置的mirrorof标签作用。
https://blog.csdn.net/whbing1471/article/details/53983779 A 看这一段 背景:写好的java项目放置到linux服务器上进行编辑的时候,由于m ...
- c#按指定长度分解数组
在操作数据库时,我们需要注意一点,就是in查询的参数不能超过1000个,否则会报错,所以我们在进行in查询的时候需要对参数数量进行控制: 用于分解数组的扩展方法: /// <summary> ...
- bytearray与矩阵转换对应关系
import numpy as npimport osa=bytearray(os.urandom(27))# for i in range(21):# print(a[i])a=np.array(a ...
- IntelliJ IDEA live template 方法配置
** * <p></p> * 功能描述 * $params$ * @return $return$ * @author abc * @date $time$ $date$ * ...
- 原生JS实现上拉下拉列表
话不多说,代码上来,有些知识点直接就在注释里 HTML <div class="list-down"> <button id="btn"> ...
- 配置集成测试环境 phpstudy
phpStudy是一个PHP调试环境的程序集成包,该程序包集成最新的Apache+PHP+MySQL+phpMyAdmin+ZendOptimizer,一次性安装,无须配置即可使用,是非常方便.好用的 ...
- [linux]查找最大的文件
查找最大的日志文件,命令: find ./ -type f | xargs -I{} du -m {}|sort -rnk1|head -10 offline一台机器保留限产,其他机器删除日志,命令: ...