当前位置:网站首页>LeetCode 648. 单词替换
LeetCode 648. 单词替换
2022-07-07 12:15:00 【Sasakihaise_】
【哈希+字符串处理】将dictionary中的单词放到HashSet中,在添加过程中记录单词的长度,方便对sentence中的单词取前缀。
class Solution {
// 哈希+字符串截取 12:41
public String replaceWords(List<String> dictionary, String sentence) {
Set<String> set = new HashSet();
Set<Integer> length = new HashSet();
for (var st: dictionary) {
set.add(st);
length.add(st.length());
}
List<Integer> l = new ArrayList(length);
Collections.sort(l);
String[] strs = sentence.split(" ");
int n = l.size();
StringBuilder sb = new StringBuilder();
for (var str: strs) {
sb.append(" ");
var flag = 0;
for (var i = 0; i < n; i++) {
var m = l.get(i);
if (m >= str.length()) {
break;
} else {
var st = str.substring(0, m);
if (set.contains(st)) {
flag = 1;
sb.append(st);
break;
}
}
}
if (flag == 0) {
sb.append(str);
}
}
return sb.toString().strip();
}
}
【字典树】对dictionary中的单词建树,遍历sentence中的单词,最先匹配到的前缀返回即可。
class Solution {
// 字典树 1:15 33
class Trie {
Trie[] map = new Trie[26];
boolean is_end;
}
Trie root = new Trie();
public void add(String str) {
Trie node = root;
for (var i = 0; i < str.length(); i++) {
char c = str.charAt(i);
if (node.map[c - 'a'] != null) {
node = node.map[c - 'a'];
} else {
Trie trie = new Trie();
node.map[c - 'a'] = trie;
node = trie;
}
}
node.is_end = true;
}
public String pre(StringBuilder sb, String str) {
Trie node = root;
for (var i = 0; i < str.length(); i++) {
char c = str.charAt(i);
if (node.map[c - 'a'] == null) {
return str;
} else {
sb.append(c);
node = node.map[c - 'a'];
if (node.is_end) {
return sb.toString();
}
}
}
return str;
}
public String replaceWords(List<String> dictionary, String sentence) {
for (var str: dictionary) {
add(str);
}
String[] strs = sentence.split(" ");
StringBuilder sb = new StringBuilder();
for (var str: strs) {
sb.append(pre(new StringBuilder(), str));
sb.append(" ");
}
return sb.toString().strip();
}
}
【字典树+哈希表写法】
class Solution {
// 字典树 10:05 10:16
class Trie {
Map<Character, Trie> map = new HashMap();
boolean endOfWord = false;
}
Trie root = new Trie();
public void add(String str) {
Trie node = root;
for (var i = 0; i < str.length(); i++) {
char c = str.charAt(i);
if (node.map.containsKey(c)) {
node = node.map.get(c);
} else {
Trie trie = new Trie();
node.map.put(c, trie);
node = trie;
}
}
node.endOfWord = true;
}
public String prefix(String str) {
Trie node = root;
StringBuilder sb = new StringBuilder();
for (var i = 0; i < str.length(); i++) {
char c = str.charAt(i);
if (node.map.containsKey(c)) {
sb.append(c);
node = node.map.get(c);
if (node.endOfWord) {
return sb.toString();
}
} else {
return str;
}
}
return str;
}
public String replaceWords(List<String> dictionary, String sentence) {
for (var str: dictionary) {
add(str);
}
String strs[] = sentence.split(" ");
for (var i = 0; i < strs.length; i++) {
strs[i] = prefix(strs[i]);
}
return String.join(" ", strs);
}
}
边栏推荐
- UML sequence diagram (sequence diagram)
- VSCode 配置使用 PyLint 语法检查器
- 2022-7-6 sigurg is used to receive external data. I don't know why it can't be printed out
- THINKPHP框架的优秀开源系统推荐
- 参数关键字Final,Flags,Internal,映射关键字Internal
- 请问,在使用flink sql sink数据到kafka的时候出现执行成功,但是kafka里面没有数
- SSRF vulnerability file pseudo protocol [netding Cup 2018] fakebook1
- 请问,我kafka 3个分区,flinksql 任务中 写了 join操作,,我怎么单独给join
- 通过 iValueConverter 给datagrid 的背景颜色 动态赋值
- Data flow diagram, data dictionary
猜你喜欢
Evolution of customer service hotline of dewu
Did login metamask
Vmware 与主机之间传输文件
UML sequence diagram (sequence diagram)
Verilog implementation of a simple legv8 processor [4] [explanation of basic knowledge and module design of single cycle implementation]
高等數學---第八章多元函數微分學1
Details of redis core data structure & new features of redis 6
Selenium Library
2022-7-7 Leetcode 844. Compare strings with backspace
Advanced Mathematics - Chapter 8 differential calculus of multivariate functions 1
随机推荐
"Song of ice and fire" in the eleventh issue of "open source Roundtable" -- how to balance the natural contradiction between open source and security?
Selenium Library
Horizontal of libsgm_ path_ Interpretation of aggregation program
The delivery efficiency is increased by 52 times, and the operation efficiency is increased by 10 times. See the compilation of practical cases of financial cloud native technology (with download)
PostgreSQL array type, each splice
Environment configuration of lavarel env
Common response status codes
ARM Cortex-A9,MCIMX6U7CVM08AD 处理器应用
Is the compass stock software reliable? Is it safe to trade stocks?
[fortress machine] what is the difference between cloud fortress machine and ordinary fortress machine?
2022-7-6 beginner redis (I) download, install and run redis under Linux
FCOS3D label assignment
[network security] SQL injection syntax summary
ES日志报错赏析-Limit of total fields
oracle 非自动提交解决
"New red flag Cup" desktop application creativity competition 2022
call undefined function openssl_cipher_iv_length
3D detection: fast visualization of 3D box and point cloud
Mathématiques avancées - - chapitre 8 différenciation des fonctions multivariables 1
Csma/cd carrier monitoring multipoint access / collision detection protocol