当前位置:网站首页>leetcode:320.列举单词的全部缩写
leetcode:320.列举单词的全部缩写
2022-08-09 22:01:00 【OceanStar的学习笔记】
题目来源
题目描述
请你写出一个能够举单词全部缩写的函数。
注意:输出的顺序并不重要。
示例:
输入: "word"
输出:
["word", "1ord", "w1rd", "wo1d", "wor1", "2rd", "w2d",
"wo2", "1o1d", "1or1", "w1r1", "1o2", "2r1", "3d", "w3", "4"]
题目解析
仔细观察示例,发现word有4个字母时,会生成16个缩写,如果把0到15的二进制写出来,每一个可以对应一种情况,如下所示:
0000 word
0001 wor1
0010 wo1d
0011 wo2
0100 w1rd
0101 w1r1
0110 w2d
0111 w3
1000 1ord
1001 1or1
1010 1o1d
1011 1o2
1100 2rd
1101 2r1
1110 3d
1111 4
那么我们就可以观察出规律,凡是0的地方都是原来的字母,单独的1还是1,如果是若干个1连在一起的话,就要求出1的个数,用这个数字来替换对应的字母
class Solution {
void helper(string word, int pos, vector<string> &res) {
for (int i = pos; i < word.size(); ++i) {
// 前缀[0, i)
for (int j = 1; i + j <= word.size(); ++j) {
string t = word.substr(0, i);
// "" + 1 + ord
// "" + 2 + rd
// "" + 3 + d
// "" + 4
// w + 1 + od
// w + 2 + d
// w + 3
t += to_string(j) + word.substr(i + j);
res.push_back(t);
helper(t, i + 1 + to_string(j).size(), res);
}
}
}
public:
vector<string> generateAbbreviations(string word) {
vector<string> res{
word};
helper(word, 0, res);
return res;
}
};
实现二:
class Solution {
// 当前正在对word[idx]字符做决定, [0...idx)已经决定完了
// 在来到word[idx]位置之前,一共有多少个1连起来,
void helper(string word, int idx, int cnt, std::string path, vector<string> &ans) {
if(idx == word.size()){
if (cnt > 0) path += to_string(cnt);
ans.push_back(path);
}else{
helper(word, idx + 1, cnt + 1, path, ans); //当前位置用1替代
helper(word, idx + 1, 0, path + (cnt > 0 ? to_string(cnt) : "") + word[idx], ans); // 当前位置还是取原来的字符
}
}
public:
vector<string> generateAbbreviations(string word) {
vector<string> res;
helper(word, 0, 0, "", res);
return res;
}
};
边栏推荐
- Presto Event Listener开发
- Under the NVM node installation;The node environment variable configuration
- 【EF】 更新条目时出错。有关详细信息,请参见内部异常。[通俗易懂]
- Flask之路由(app.route)详解
- 十步以内,用小程序快速生成App!
- Flask入门学习教程
- 聊天尬死名场面,你遇到过吗?教你一键获取斗图表情包,晋升聊天达人
- “稚晖君”为2022昇腾AI创新大赛打call&nbsp;期待广大开发者加入
- 你的 Link Button 能让用户选择新页面打开吗?
- 面试官:Redis 大 key 要如何处理?
猜你喜欢
shell学习
一文让你快速了解隐式类型转换【整型提升】!
JS Deobfuscation - AST Restoration Case
十步以内,用小程序快速生成App!
金山云地震,震源在字节?
Socket发送缓冲区接收缓冲区快问快答
关于ETL的两种架构(ETL架构和ELT架构)
【服务器数据恢复】SAN LUN映射出错导致文件系统数据丢失的数据恢复案例
Chatting embarrassing scenes, have you encountered it?Teach you to get the Doutu emoticon package with one click, and become a chat expert
【微服务~Nacos】Nacos之配置中心
随机推荐
【LaTex】 Font “FandolSong-Regular“ does not contain requested(fontspec)Script “CJK“.如何抑制此种警告?
Presto Event Listener开发
金山云地震,震源在字节?
json case
Flask's routing (app.route) detailed
The overall construction process of the Tensorflow model
FileZilla搭建FTP服务器图解教程
shell学习
A1. Prefix Flip (Easy Version)
6 rules to sanitize your code
18-GuliMall 压力测试与性能监控
Liver all night to write a thirty thousand - word all the commands the SQL database, function, speaks clearly explain operators, content is rich, proposal collection + 3 even high praise!
开发者必备:一文快速熟记【数据库系统】和【软件开发模型】常用知识点
How do task flow executors work?
Redis
Evolution of MLOps
【软考 系统架构设计师】案例分析⑤ 质量属性和架构评估
Jinshanyun earthquake, the epicenter is in bytes?
【EF】 更新条目时出错。有关详细信息,请参见内部异常。[通俗易懂]
UML类图五种关系的代码实现[通俗易懂]