题目地址: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表示在遍历到当前字符时前面已经省略掉了多少个字符。

对于每个位置,我们有两个选择:

  1. 这个位置如果不使用,则累加现在已有的省略掉的数字num
  2. 这个位置如果使用,则拼接前面已经累加的数字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++)的更多相关文章

  1. LeetCode 320. Generalized Abbreviation

    原题链接在这里:https://leetcode.com/problems/generalized-abbreviation/ 题目: Write a function to generate the ...

  2. LeetCode 1 Two Sum 解题报告

    LeetCode 1 Two Sum 解题报告 偶然间听见leetcode这个平台,这里面题量也不是很多200多题,打算平时有空在研究生期间就刷完,跟跟多的练习算法的人进行交流思想,一定的ACM算法积 ...

  3. 【LeetCode】Permutations II 解题报告

    [题目] Given a collection of numbers that might contain duplicates, return all possible unique permuta ...

  4. 【LeetCode】Island Perimeter 解题报告

    [LeetCode]Island Perimeter 解题报告 [LeetCode] https://leetcode.com/problems/island-perimeter/ Total Acc ...

  5. 【LeetCode】01 Matrix 解题报告

    [LeetCode]01 Matrix 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/01-matrix/#/descripti ...

  6. 【LeetCode】Largest Number 解题报告

    [LeetCode]Largest Number 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/largest-number/# ...

  7. 【LeetCode】Gas Station 解题报告

    [LeetCode]Gas Station 解题报告 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/gas-station/#/descr ...

  8. 【LeetCode】120. Triangle 解题报告(Python)

    [LeetCode]120. Triangle 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址htt ...

  9. LeetCode: Unique Paths II 解题报告

    Unique Paths II Total Accepted: 31019 Total Submissions: 110866My Submissions Question Solution  Fol ...

随机推荐

  1. 解决 VS Code 无法使用Ctrl+C等快捷键

    背景 VScode 安装 Vim扩展后,无法使用Ctrl+C,Ctrl+X和 Ctrl+V等热键 解决方案 方案一 停用Vim 热键覆盖 # 原因: vim 扩展默认启用Vim ctrl键覆盖常见的V ...

  2. Matlab指针

    Matlab指针 第一印象貌似是Matlab中不存在指针,所有变量与函数的赋值都是按值传递的,不会对参数进行修改.其实Matlab提供了handle类作为指针代替品.只要我们利用handle子类,就可 ...

  3. 【R】调整ggplot图例大小

    图例太多时,会挤压正图,显得正图展示区域很小,这时有必要缩小图例. ################# # 减小ggplot图例 ################# library(ggplot2) ...

  4. 【Python小试】计算目录下所有DNA序列的Kmer并过滤

    背景 Kmer是基因组组装算法中经常接触到的概念,简单来说,Kmer就是长度为k的核苷酸序列.一般长短为m的reads可以分成m-k+1个Kmer.Kmer的长度和阈值直接影响到组装的效果. Deno ...

  5. 短序列组装Sequence Assembly(转载)

    转载:http://blog.sina.com.cn/s/blog_4af3f0d20100fq5i.html 短序列组装(Sequence assembly)几乎是近年来next-generatio ...

  6. 2.MaxSubArray-Leetcode

    题目:最大连续子序列和 思路:动态规划 状态转移方程 f[j]=max{f[j-1]+s[j],s[j]}, 其中1<=j<=n target = max{f[j]}, 其中1<=j ...

  7. nrf 51802 和 nrf51822 的区别于联系

    51802QFAA与51822QFAA在FLASH 跟RAM的容量没有差别:区别在于:a,接收灵敏度 51802是-91dBm;51822是-93dBm,这个差异导致接收距离有差异:b,Tx Powe ...

  8. JavaScript | 新手村(一)变量,运算和变量方法

    资料来自:JavaScript 第一步 1. 向 html 页面添加 JavaScript 1.1 内部 JavaScript 在 html 文件中的 </body> 标签前插入代码: & ...

  9. day14搭建博客系统项目

    day14搭建博客系统项目 1.下载代码包 [root@web02 opt]# git clone https://gitee.com/lylinux/DjangoBlog.git 2.使用pid安装 ...

  10. org.apache.hadoop.hive.ql.metadata.HiveException: Internal Error: cannot generate all output rows for a Partition解决

    自己在路径访问明细表开发时,写的sql如下 SELECT guid, sessionid, event['url'] as page, `timestamp` as ts, row_number() ...