当前位置:网站首页>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);
}
}
边栏推荐
- Interface automation test - solution of data dependency between interfaces
- 使用day.js让时间 (显示为几分钟前 几小时前 几天前 几个月前 )
- AI人才培育新思路,这场直播有你关心的
- 118. Yanghui triangle
- 最长上升子序列模型 AcWing 482. 合唱队形
- 内存溢出和内存泄漏的区别
- "New red flag Cup" desktop application creativity competition 2022
- Lavarel之环境配置 .env
- FCOS3D label assignment
- 手把手教会:XML建模
猜你喜欢
Wired network IP address of VMware shared host
Dry goods | summarize the linkage use of those vulnerability tools
PERT图(工程网络图)
用例图
Flask session forged hctf admin
一个简单LEGv8处理器的Verilog实现【四】【单周期实现基础知识及模块设计讲解】
2022-7-6 beginner redis (I) download, install and run redis under Linux
js 获取当前时间 年月日,uniapp定位 小程序打开地图选择地点
Codes de non - retour à zéro inversés, codes Manchester et codes Manchester différentiels couramment utilisés pour le codage des signaux numériques
高等数学---第八章多元函数微分学1
随机推荐
Laravel Form-builder使用
2022-7-6 Leetcode27. Remove the element - I haven't done the problem for a long time. It's such an embarrassing day for double pointers
带你掌握三层架构(建议收藏)
請問,在使用flink sql sink數據到kafka的時候出現執行成功,但是kafka裏面沒有數
Data flow diagram, data dictionary
Cargo placement problem
. Net core about redis pipeline and transactions
Vmware 与主机之间传输文件
Million data document access of course design
IP and long integer interchange
最长上升子序列模型 AcWing 482. 合唱队形
requires php ~7.1 -&gt; your PHP version (7.0.18) does not satisfy that requirement
PERT图(工程网络图)
Introduction to database system - Chapter 1 introduction [conceptual model, hierarchical model and three-level mode (external mode, mode, internal mode)]
118. Yanghui triangle
[AI practice] Application xgboost Xgbregressor builds air quality prediction model (II)
XML文件的解析操作
[daily training -- Tencent select 50] 231 Power of 2
[network security] SQL injection syntax summary
Excuse me, when using Flink SQL sink data to Kafka, the execution is successful, but there is no number in Kafka