题目地址: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. Jvarkit : Java utilities for Bioinformatics

    Jvarkit : Java utilities for Bioinformatics :一个java写的生物信息工具包:http://lindenb.github.io/jvarkit/

  2. C 语言do while 循环

    do while 循环小练习 1 #include <stdio.h> 2 #include <stdlib.h> 3 4 int main(void) 5 { 6 int a ...

  3. mysql-日期时间函数大全

    DAYOFWEEK(date)  返回日期date是星期几(1=星期天,2=星期一,--7=星期六,ODBC标准)mysql> select DAYOFWEEK('1998-02-03');  ...

  4. Swift-技巧(十一)重写运算符

    摘要 基础数据的运算可以直接使用四则运算符.在 Swift 中也可以通过重写四则运算符的方式,让 struct 或者 class 创建的结构体或者对象也能像基础数据那样直接使用四则运算符. Swift ...

  5. Hive(三)【DDL 数据定义】

    目录 一.DDL数据定义 1.库的DDL 1.1创建数据库 1.2查询数据库 1.3查看数据库详情 1.4切换数据库 1.5修改数据库 1.6删除数据库 2.表的DDL 2.1创建表 2.2管理表(内 ...

  6. Shell学习(二)——变量和基本数据类型

    参考博客: [1]LinuxShell脚本--变量和数据类型 [2]shell只读变量删除 一.变量 定义变量的语法 定义变量时,变量名和变量值之间使用"="分隔,并且等号两边不能 ...

  7. vue SCSS

        C:\eclipse\wks\vue\esql-ui>node -v v12.18.1 C:\eclipse\wks\vue\esql-ui>npm -v 6.14.5 直接修改p ...

  8. 【Java 8】Optional 使用

    一.前言 如果要给 Java 所有异常弄个榜单,我会选择将 NullPointerException 放在榜首.这个异常潜伏在代码中,就像个遥控炸弹,不知道什么时候这个按钮会被突然按下(传入 null ...

  9. Groovy获取Bean两种方式(奇淫技巧操作)

    前言:请各大网友尊重本人原创知识分享,谨记本人博客:南国以南i 背景: 在Java代码中当我们需要一个Bean对象,通常会使用spring中@Autowired注解,用来自动装配对象. 在Groovy ...

  10. Tableau如何绘制凹凸图

    一.把订单日期拖拽至列,把销售额拖拽至行,类别拖拽至标记,并把订单日期拖拽至筛选器选择2017年 二.创建计算字段销售排名 三.将刚刚创建的销售排名拖拽至行,计算依据-类别 四.销量排名拖拽成两个,图 ...