全球优质服务器购买

为用户应用推荐适合的服务器,针对需求定制,将质量做到更好

香港服务器

香港CN2优化带宽,国内访问快

CPU:I3-2120(2核心4线程)

内存:4G DDR3内存

硬盘:1T HDD

带宽:10M优化、10M国际

IP数:1个

价格:699/月

美国服务器

美国洛杉矶高性价比服务器

CPU:I3-2120(2核心4线程)

内存:4G DDR3内存

硬盘:1T HDD

带宽:30M优化/100M普通

IP数:1个(10G防护)

价格:499/月

香港站群服务器

香港多IP站群服务器租用

CPU:E3-1230V2(4核

内存:8G DDR3内存

硬盘:240G SSD/1T SATA

带宽:10M优化

IP数:125个IP(1/2C)

价格:1099/月

美国站群服务器

美国多IP站群服务器租用

CPU:E3-1230V2(4核)

内存:16G DDR3内存

硬盘:1T HDD/240G SSD

带宽:30M优化/100M普通

IP数:125个IP(1/2C)

价格:999/月

b'

\n
\n

【题目描述】

单词数组 ​​words​​ 的 有效编码 由任意助记字符串 ​​s​​​ 和下标数组 ​​indices​​ 组成,且满足:

  • ​words.length == indices.length​
  • 助记字符串 ​​s​​ 以 ​​\'#\'​​ 字符结尾
  • 对于每个下标 ​​indices[i]​​ ,​​s​​ 的一个从 ​​indices[i]​​ 开始、到下一个 ​​\'#\'​​ 字符结束(但不包括 ​​\'#\'​​)的 子字符串 恰好与 ​​words[i]​​ 相等

给你一个单词数组 ​​words​​ ,返回成功对 ​​words​​ 进行编码的最小助记字符串 ​​s​​ 的长度 。

​​​​https://leetcode.cn/problems/short-encoding-of-words/​​

 

【示例】

【LeeCode】820. 单词的压缩编码_System

 

【代码】​​存储后缀​​

很巧妙的思路

package com.company;
import java.util.*;

// 2023-03-01
class Solution {
public int minimumLengthEncoding(String[] words) {
List<String> list = Arrays.asList(words);
Set<String> set = new HashSet<>(list);

System.out.println("去重前: " + set);
// 删除words中可能重复的字符
for (String x: words){
for (int i = 1; i < x.length(); i++){
set.remove(x.substring(i));
}
}
System.out.println("去重后: " + set);
int res = 0;
for (String x : set){
res += x.length() + 1;
}
System.out.println("res: " + res);
return res;
}
}


public class Test {
public static void main(String[] args) {
new Solution().minimumLengthEncoding(new String[]{"time", "me", "bell"} ); // 输出:10
new Solution().minimumLengthEncoding(new String[]{"t"} ); // 输出:2
}
}

 

【代码】​​优解​​

package com.company;
import java.util.*;

// 2023-03-01
class Solution {
public int minimumLengthEncoding(String[] words) {
// 先按字符串长度进行排序
Arrays.sort(words, (w1, w2) -> w2.length() - w1.length());

// 然后在进行匹配的计算
String sb = new String();
for (String word : words) {
if (!sb.contains(word + "#")) {
sb = sb.concat(word + "#");
}
}
System.out.println(sb);
return sb.length();
}
}
public class Test {
public static void main(String[] args) {
new Solution().minimumLengthEncoding(new String[]{"time", "me", "bell"} ); // 输出:10
new Solution().minimumLengthEncoding(new String[]{"t"} ); // 输出:2
}
}
\n
\n \n
\n
\n\n \n \n \n\n
\n

\n '

内容来源于网络如有侵权请私信删除

推荐文章