【LeetCode】320. Generalized Abbreviation 解题报告 (C++)
- 作者: 负雪明烛
- id: fuxuemingzhu
- 个人博客:http://fuxuemingzhu.cn/
题目地址:https://leetcode-cn.com/problems/generalized-abbreviation/
题目描述
Write a function to generate the generalized abbreviations of a word.
Note: The order of the output does not matter.
Example:
Input: "word"
Output:
["word", "1ord", "w1rd", "wo1d", "wor1", "2rd", "w2d", "wo2", "1o1d", "1or1", "w1r1", "1o2", "2r1", "3d", "w3", "4"]
题目大意
生成一个字符串的所有缩写。
解题方法
DFS
该题是让我们生成缩写,本质是个搜索题。
使用index表示当前遍历到哪个字符了,使用nums表示在遍历到当前字符时前面已经省略掉了多少个字符。
对于每个位置,我们有两个选择:
- 这个位置如果不使用,则累加现在已有的省略掉的数字num
- 这个位置如果使用,则拼接前面已经累加的数字num,拼接当前字符,省略掉的字符从0开始
所以这个问题本身还是简单的,需要注意的是num为0的时候不应该进行拼接。
另外,这个题为什么没有像全排列/子集一样,进行for循环呢?这是由于在构造字符缩写的时候起点、顺序都不能变的,这个不是抽取或者排列的问题。
C++代码如下:
class Solution {
public:
vector<string> generateAbbreviations(string word) {
vector<string> res;
dfs(res, word, 0, 0, "");
return res;
}
void dfs(vector<string>& res, string& word, int index, int num, string cur) {
if (index == word.size()) {
if (num != 0)
cur = cur + to_string(num);
res.push_back(cur);
return;
}
// 不用word[index]
dfs(res, word, index + 1, num + 1, cur);
// 用word[index]
dfs(res, word, index + 1, 0, cur + (num == 0 ? "" : to_string(num)) + word[index]);
}
};
日期
2019 年 9 月 27 日 —— 昨天面快手,竟然是纯刷题
【LeetCode】320. Generalized Abbreviation 解题报告 (C++)的更多相关文章
- LeetCode 320. Generalized Abbreviation
原题链接在这里:https://leetcode.com/problems/generalized-abbreviation/ 题目: Write a function to generate the ...
- LeetCode 1 Two Sum 解题报告
LeetCode 1 Two Sum 解题报告 偶然间听见leetcode这个平台,这里面题量也不是很多200多题,打算平时有空在研究生期间就刷完,跟跟多的练习算法的人进行交流思想,一定的ACM算法积 ...
- 【LeetCode】Permutations II 解题报告
[题目] Given a collection of numbers that might contain duplicates, return all possible unique permuta ...
- 【LeetCode】Island Perimeter 解题报告
[LeetCode]Island Perimeter 解题报告 [LeetCode] https://leetcode.com/problems/island-perimeter/ Total Acc ...
- 【LeetCode】01 Matrix 解题报告
[LeetCode]01 Matrix 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/01-matrix/#/descripti ...
- 【LeetCode】Largest Number 解题报告
[LeetCode]Largest Number 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/largest-number/# ...
- 【LeetCode】Gas Station 解题报告
[LeetCode]Gas Station 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/gas-station/#/descr ...
- 【LeetCode】120. Triangle 解题报告(Python)
[LeetCode]120. Triangle 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址htt ...
- LeetCode: Unique Paths II 解题报告
Unique Paths II Total Accepted: 31019 Total Submissions: 110866My Submissions Question Solution Fol ...
随机推荐
- seq生成格式化字符
[root@ansz.quan.bbs ~]$seq -s "+" 10 1+2+3+4+5+6+7+8+9+10 seq为生成数字序列 -s 分隔符
- python-django-聚合与统计
l例子1: 这里的values_list 相当于SQL语句里面的GROUP BY的功能,但是返回的是一个元组 要实现示例当中的直接用,与数据库格式有关将模型设计得更加完美 模型: 注意related_ ...
- 【R方差分析】蛋白质表达量多组比较
初始数据类似: 蛋白质组数据虽不是严格的正态分布,但目前最常用的检验方法还是T检验(两组比较)和方差分析(多组比较).这个话题值得深究,这里不展开. 主要是求多个蛋白的Pvalue值或FDR,用于差异 ...
- cpu的性能测试
#!/bin/bash #user%加上sys%是性能的评判标准 User_sys_a=`sar -u 1 3 |tail -1 |awk '{print $3"+"$5}'|bc ...
- karatsuba乘法
karatsuba乘法 Karatsuba乘法是一种快速乘法.此算法在1960年由Anatolii Alexeevitch Karatsuba 提出,并于1962年得以发表.[1] 此算法主要用于两个 ...
- GeckoDriver的安装和使用
GeckoDriver用于驱动Firefox,在这之前请确保已经正确安装好了Firefox浏览器并可以正常运行. 一.GeckoDriver的安装 GitHub:https://github.com/ ...
- Kubernetes主机间cluster ip时通时不通
1.问题现象 测试部署了一个service,包括2个pod,分别在node1和node2上. $ kubectl get svc NAME CLUSTER-IP EXTERNAL-IP PORT(S) ...
- Flink(四)【IDEA执行查看Web UI】
1.导入依赖 <!-- flink Web UI --> <dependency> <groupId>org.apache.flink</groupId> ...
- Shell学习(九)——chattr与lsattr命令详解
有时候你发现用root权限都不能修改某个文件,大部分原因是曾经用chattr命令锁定该文件了.chattr命令的作用很大,其中一些功能是由Linux内核版本来支持的,不过现在生产绝大部分跑的linux ...
- new Date()与setDate()参数
New Date()与setDate()参数 相信网上已经有很多关于日期的文章了,这里只是我自己再工作中遇到的问题然后加以总结: new Date() new Date() 一共有六种形式,五种带参数 ...