Description

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

Example

Example1

  1. Input:
  2. <"abc", 2>
  3. <"ac", 4>
  4. <"ab", 9>
  5. Output:<a[9,4,2]<b[9,2]<c[2]<>>c[4]<>>>
  6. Explanation:
  7. Root
  8. /
  9. a(9,4,2)
  10. / \
  11. b(9,2) c(4)
  12. /
  13. c(2)

Example2

  1. Input:
  2. <"a", 10>
  3. <"c", 41>
  4. <"b", 50>
  5. <"abc", 5>
  6. Output: <a[10,5]<b[5]<c[5]<>>>b[50]<>c[41]<>>
    思路:先遍历一遍数,然后重构树进行求解
  1. /**
  2. * Definition of TrieNode:
  3. * public class TrieNode {
  4. * public NavigableMap<Character, TrieNode> children;
  5. * public List<Integer> top10;
  6. * public TrieNode() {
  7. * children = new TreeMap<Character, TrieNode>();
  8. * top10 = new ArrayList<Integer>();
  9. * }
  10. * }
  11. */
  12. public class TrieService {
  13.  
  14. private TrieNode root = null;
  15.  
  16. public TrieService() {
  17. root = new TrieNode();
  18. }
  19.  
  20. public TrieNode getRoot() {
  21. // Return root of trie root, and
  22. // lintcode will print the tree struct.
  23. return root;
  24. }
  25.  
  26. // @param word a string
  27. // @param frequency an integer
  28. public void insert(String word, int frequency) {
  29. // Write your cod here
  30. TrieNode cur = root;
  31. int n = word.length();
  32.  
  33. for (int i = 0; i < n; ++i) {
  34. Character c = word.charAt(i);
  35. if (!cur.children.containsKey(c))
  36. cur.children.put(c, new TrieNode());
  37.  
  38. cur = cur.children.get(c);
  39. addFrequency(cur.top10, frequency);
  40. }
  41. }
  42.  
  43. public void addFrequency(List<Integer> top10, int frequency) {
  44. top10.add(frequency);
  45. int n = top10.size();
  46. int index = n - 1;
  47. while (index > 0) {
  48. if (top10.get(index) > top10.get(index - 1)) {
  49. int temp1 = top10.get(index);
  50. int temp2 = top10.get(index - 1);
  51. top10.set(index, temp2);
  52. top10.set(index - 1, temp1);
  53. index -= 1;
  54. } else
  55. break;
  56. }
  57. if (n > 10)
  58. top10.remove(n - 1);
  59. }
  60. }

  

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. 公众号后台开发(SpingMVC接收与响应公众号消息)

    1.准备 1.准备服务 与微信对接的url要具备以下条件: (1)在公网上能够访问 (2)端口只支持80端口 在这里如果是公网能够访问的服务最好,也可以通过花生壳或者其他外网映射工具进行映射,比如ng ...

  2. Spring Security 解析(四) ——短信登录开发

    Spring Security 解析(四) -- 短信登录开发   在学习Spring Cloud 时,遇到了授权服务oauth 相关内容时,总是一知半解,因此决定先把Spring Security ...

  3. SQL Server 中获取一个表的字段信息

    直接贴代码了: SELECT sysobjects.name AS TableName, syscolumns.Id AS TableId, syscolumns.name AS DbColumnNa ...

  4. CentOS修改ulimit

    一.概述 查看limit配置 # ulimit -a core data seg size (kbytes, -d) unlimited scheduling priority (-e) file s ...

  5. python 笔记——生成器和迭代器

    #-*- coding:utf-8 -*- a=[1,2,3,4] for i,j in enumerate(a): print i,j '''只有ij时,''' a=[1,2,3,4] for i ...

  6. SET QUOTED_IDENTIFIER选项对索引的影响

    早上来到公司,发现用于整理索引碎片的Job跑失败了,查看job history,发现以下错误消息: ALTER INDEX failed because the following SET optio ...

  7. python中 jsonchema 与 shema 效率比较

    前面几篇文章总结了python中jsonschema与schema的用法,这里测试一下两者的效率: 上代码: import time from jsonschema import validate, ...

  8. 服务器同时安装python2支持的py-faster-rcnn以及python3支持的keras

    最近把服务器折腾一下,搞定这两个.

  9. Django的安全攻击

    目录 Django的安全攻击 XSS XSS(跨站脚本攻击) 危害 原理 防护 csrf(Cross Site Request Forgery) csrf(跨站域请求伪造) 过程 Django 提供的 ...

  10. 组件切换方式(Vue.js)

    这里,我用一个注册登录两组件的切换实例来演示: 切换方式一 <!DOCTYPE html> <html lang="zh-CN"> <head> ...