[Leedcode][JAVA][第820题][字典树][Set]

【问题描述】

给定一个单词列表,我们将这个列表编码成一个索引字符串 S 与一个索引列表 A。例如,如果这个列表是 ["time", "me", "bell"],我们就可以将其表示为 S = "time#bell#" 和 indexes = [0, 2, 5]。对于每一个索引,我们可以通过从字符串 S 中索引的位置开始读取字符串,直到 "#" 结束,来恢复我们之前的单词列表。那么成功对给定单词列表进行编码的最小字符串长度是多少呢?示例:输入: words = ["time", "me", "bell"]
输出: 10
说明: S = "time#bell#" , indexes = [0, 2, 5] 。提示:1 <= words.length <= 2000
1 <= words[i].length <= 7
每个单词都是小写字母 。

【解答思路】

1. 数组加入set中,切割set中每个单词后缀,剔除相同的后缀 (如切割time 剔除me)
  • word.substring(n) -> 从第n个下标开始切割(n<word.length())
    例子
  • time.substring(1) -> ime
  • time.substring(2) -> me
  • time.substring(3) -> e

image.png

class Solution {public int minimumLengthEncoding(String[] words) {Set<String> set = new HashSet<>(Arrays.asList(words));for (String word : words) {for (int i = 1; i < word.length(); i++) {set.remove(word.substring(i));}}int ans = 0;
//+1  按照题意#for (String word : set) {ans += word.length() + 1;}return ans;}
}
2. 字典树/Trie树/前缀树 O(N^2)

前缀树

  • 把单词的倒序插入字典树(后缀)长度越长优先插入
  • 字典树判断某个单词的逆序是否出现在字典树里
    image.png
class Solution {public int minimumLengthEncoding(String[] words) {int len = 0;Trie trie = new Trie();// 先对单词列表根据单词长度由长到短排序Arrays.sort(words, (s1, s2) -> s2.length() - s1.length());// 单词插入trie,返回该单词增加的编码长度for (String word: words) {len += trie.insert(word);}return len;}
}// 定义tire
class Trie {TrieNode root;public Trie() {root = new TrieNode();}public int insert(String word) {TrieNode cur = root;boolean isNew = false;// 倒着插入单词for (int i = word.length() - 1; i >= 0; i--) {int c = word.charAt(i) - 'a';if (cur.children[c] == null) {isNew = true; // 是新单词cur.children[c] = new TrieNode();}cur = cur.children[c];}// 如果是新单词的话编码长度增加新单词的长度+1,否则不变。return isNew? word.length() + 1: 0;}
}class TrieNode {char val;TrieNode[] children = new TrieNode[26];public TrieNode() {}
}作者:sweetiee
链接:https://leetcode-cn.com/problems/short-encoding-of-words/solution/99-java-trie-tu-xie-gong-lue-bao-jiao-bao-hui-by-s/

【总结】

  1. length 属性 length()方法
    -java中数组是没有length()方法的,只有length属性,数组array.length返回的是该数组的长度。

-字符串String是有length()方法的,str.length()返回的是该字符串的长度。

2.字典树出现地方

  • 搜索引擎 联想字段
  • 区块链 以太坊 Merkle Patricia Tree 默克尔树+前缀树
  • 英语分词

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/426092.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

java学习(132):hashtable使用map替代实体数据

import java.util.*;public class test71 {public static void main(String[] args){Scanner innew Scanner(System.in);System.out.println("请输入学生的信息");List syuListnew ArrayList();//存储所有学生for(int i1;i<3;i){HashMap stuMapnew HashMap();Syst…

spring----06 更多DI知识

一. 延迟初始化 延迟初始化也叫做惰性初始化&#xff0c;指不提前初始化Bean&#xff0c;而是只有在真正使用时才创建及初始化Bean。配置方式很简单只需在<bean>标签上指定 “lazy-init” 属性值为“true”即可延迟初始化Bean Spring容器会在创建容器时提前初始化“singl…

[Leedcode][JAVA][第1162题][BFS]

【问题描述】 你现在手里有一份大小为 N x N 的『地图』&#xff08;网格&#xff09; grid&#xff0c;上面的每个『区域』&#xff08;单元格&#xff09;都用 0 和 1 标记好了。其中 0 代表海洋&#xff0c;1 代表陆地&#xff0c;你知道距离陆地区域最远的海洋区域是是哪一…

java学习(133):泛型

public class Employee {private String name;private String ags;public void setName(String name) {this.name name;}public String getName() {return name;}public void setAgs(String ags) {this.ags ags;}public String getAgs() {return ags;} }测试类 import java.…

centos7默认网卡配置文件_CentOS7中网络配置详解

一、前言本来准备写一些网络配置工具的使用&#xff0c;后来想了想(其实还是为了偷懒)&#xff0c;网上关于nmcli工具的使用文章不多&#xff0c;所以写一下CentOS7中的nmcli工具的简单使用和CentOS7中一些网络属性配置的相关变化。二、CentOS7网络方面的变化很多朋友刚接触到C…

Golang 学习资料

资料 1.How to Write Go Code https://golang.org/doc/code.html 2.A Tour of Go https://tour.golang.org/list 3.Effective Go https://golang.org/doc/effective_go.html 4.Visit the documentation page for a set of in-depth articles about the Go language and its lib…

【软考】[信息安全工程师]

【背景】 有一定的基础&#xff0c;于2019年5月的考试上岸&#xff0c;复习了两周左右。奥里给&#xff01; 【备考资料】 【参考网站】 信管网 http://www.cnitpm.com/aq/ 月梦工作室 https://www.moondream.cn/ 含历年试题以及参考答案 【参考教材】 信息安全工程师五天…

java学习(134):泛型通配符的使用

import java.util.ArrayList; import java.util.List;//泛型通配符的使用 public class test73 {public static void main(String[] args){List<Integer> intListnew ArrayList<Integer>();intList.add(new Integer(100));intList.add(new Integer(200));List<?…

java dictionary遍历_遍历 Dictionary,你会几种方式?

一&#xff1a;背景1. 讲故事昨天在 StackOverflow 上看到一个很有趣的问题&#xff0c;说: 你会几种遍历字典的方式&#xff0c;然后跟帖就是各种奇葩的回答&#xff0c;挺有意思&#xff0c;马上就要国庆了&#xff0c;娱乐娱乐吧&#xff0c;说说这种挺无聊的问题&#x1f6…

【SP26073】DIVCNT1 - Counting Divisors 题解

题目描述 定义 \(d(n)\) 为 \(n\) 的正因数的个数&#xff0c;比如 \(d(2) 2, d(6) 4\)。 令 $ S_1(n) \sum_{i1}^n d(i) $ 给定 \(n\)&#xff0c;求 \(S_1(n)\)。 输入格式 第一行包含一个正整数 \(T\) (\(T \leq 10^5\))&#xff0c;表示数据组数。 接下来的 \(T\) 行&am…

密码系统的安全性

1&#xff0c;评估密码系统安全性主要有三种方法&#xff1a; &#xff08;1&#xff09;无条件安全性 这种评价方法考虑的是假定攻击者拥有无限的计算资源&#xff0c;但仍然无法破译该密码系统。 &#xff08;2&#xff09;计算安全性 这种方法是指使用目前最好的方法攻破…

java学习(135):map中泛型使用

定义一个员工类 public class Employee {private String name;private String ags;public void setName(String name) {this.name name;}public String getName() {return name;}public void setAgs(String ags) {this.ags ags;}public String getAgs() {return ags;} }定义…

java两种绑定方式_Javascript绑定事件的两种方式的区别

命名函数function check(){//code}匿名函数window.onload function(){//先获取元素对象&#xff0c;再绑定事件&#xff0c;绑定的是匿名函数不可重用var btn document.getElementById("btn");btn.onclick function(){//code}}以前一直以为两种方式的区别不大&…

前端基础_认识前端.md

前端学习 前端学习路线学习网站 菜鸟驿站慕课网freeCOdeCampw3schooltry8在线编辑 codepenjsfiddlethecodeplayer其他网站 cssfilterscssstats极客学院搭建个人博客wordpress博客园网站检查规范How to learn webTobe continue... 学习准备 查看浏览器占有的市场份额 查看浏览器…

[剑指offer][JAVA][第62题][约瑟夫环][LinkedList vs ArrayList]

【问题描述】 面试题62. 圆圈中最后剩下的数字 0,1,,n-1这n个数字排成一个圆圈&#xff0c;从数字0开始&#xff0c;每次从这个圆圈里删除第m个数字。求出这个圆圈里剩下的最后一个数字。例如&#xff0c;0、1、2、3、4这5个数字组成一个圆圈&#xff0c;从数字0开始每次删除第…

java创建两个foo方法_Java类实例化原理 - osc_foo7glsg的个人空间 - OSCHINA - 中文开源技术交流社区...

Java对象的创建过程包括类初始化(类实例化两个阶段。一、Java对象创建时机(1)使用new关键字创建对象(2)反射创建对象使用Class类的newInstance方法Student student2 (Student)Class.forName("Student类全限定名").newInstance()&#xff1b;使用Constructor类的newI…

java学习(136):带泛型的类

SuppressWarnings("all") public class GJClass<T> {public String getClassName(T t){return t.getClass().getName();} } 测试类 public class test76 {public static void main(String[] args){GJClass gjClassnew GJClass();String classNamegjClass.get…

如何往eclipse中导入maven项目

现在公司中大部分项目可能都是使用maven来构建&#xff0c;假如现在摆在你面前有一个maven的项目&#xff0c;如果你要学习它&#xff0c;如何将它导入到像eclipse这样的集成开发工具中呢&#xff0c;以项目public_class_1为例&#xff1a; 1.在eclipse的工作界面的最左侧&…

[Leetcode][JAVA][第912题][排序算法]

【问题描述】 给你一个整数数组 nums&#xff0c;将该数组升序排列。 示例 1&#xff1a; 输入&#xff1a;nums [5,2,3,1] 输出&#xff1a;[1,2,3,5]【解答思路】 1.插入排序&#xff08;熟悉&#xff09; 每次将一个数字插入一个有序的数组里&#xff0c;成为一个长度更…

java 调用r语言包传参数_Java与R语言的配置,调用

我是最近才接触到了R语言&#xff0c;所以用起来有很多的问题&#xff0c;之前只是想单纯想用java调用到R语言中的一些东西&#xff0c;没有想到这个事情并不是想象的那么简单的。好了&#xff0c;闲话不多说&#xff0c;下面我来说说我在运用R的时候遇上的问题吧。第一步&…