当前位置:网站首页>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;
}
};
边栏推荐
- R语言patchwork包将多个可视化结果组合起来、使用plot_annotation函数以及tag_level参数将组合图用大写字母进行顺序编码、为组合图的标签添加自定义前缀信息
- 台风生成,广州公交站场积极开展台风防御安全隐患排查
- 孙正义亏掉1500亿:当初投贵了
- 6 rules to sanitize your code
- Under the NVM node installation;The node environment variable configuration
- 重装系统后新建文本文档打不开怎么办
- 国内手机厂商曾为它大打出手,如今它却最先垮台……
- R语言ggplot2可视化:使用ggplot2可视化散点图、使用labs参数自定义Y轴的轴标签文本(customize Y axis labels)
- JSON 基本使用
- 在“企业通讯录”的盲区,融云的边界与分寸
猜你喜欢

Blender程序化建模简明教程【PCG】

FileZilla搭建FTP服务器图解教程

反射机制篇
6 rules to sanitize your code
2.1.5 大纲显示问题

leetcode brush questions diary Calculate the number of elements on the right that is less than the current element

Postgresql源码(68)virtualxid锁的原理和应用场景

Swift 需求 如何防止把view重复添加到win里面

力扣 1413. 逐步求和得到正数的最小值

17-GuliMall 搭建虚拟域名访问环境
随机推荐
JSON 基本使用
Flask's routing (app.route) detailed
Basic operations of openGauss database (super detailed)
阿里云架构师金云龙:基于云XR平台的视觉计算应用部署
任务流执行器是如何工作的?
mysql 找不到或无法加载已注册的 .Net Framework Data Provider。
How to Make Your Company Content GDPR Compliant
FileZilla搭建FTP服务器图解教程
A1. Prefix Flip (Easy Version)
Converting angles to radians
unit test
laravel table migration error [easy to understand]
C. Binary String Reconstruction
leetcode 刷题日记 计算右侧小于当前元素的个数
五星控股汪建国:以“植物精神”深耕赛道,用“动物精神”推动成长
p5.js实现的炫酷星体旋转动画
Flutter 绘制美不胜收的光影流动效果
[Implementation of the interface for adding, deleting, checking, and modifying a double-linked list]
Quotefancy ,提供鼓舞人心语录的壁纸网站 - 倾城之链
BulkInsert方法实现批量导入