[LeetCode] 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来写的,但是我一时半会又没想到该咋递归,后来我数了一下题目中给的例子的所有情况的个数,是16个,而word有4个字母,刚好是2的4次方,这是巧合吗,当然不是,后来我又发现如果把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 {
public:
vector<string> generateAbbreviations(string word) {
vector<string> res;
for (int i = ; i < pow(, word.size()); ++i) {
string out = "";
int cnt = , t = i;
for (int j = ; j < word.size(); ++j) {
if (t & == ) {
++cnt;
if (j == word.size() - ) out += to_string(cnt);
} else {
if (cnt != ) {
out += to_string(cnt);
cnt = ;
}
out += word[j];
}
t >>= ;
}
res.push_back(out);
}
return res;
}
};
上述方法返回结果的顺序为:
["word","1ord","w1rd","2rd","wo1d","1o1d","w2d","3d","wor1","1or1","w1r1","2r1","wo2","1o2","w3","4"]
我们可以对上面代码稍稍改写一下,变的稍微简洁一点:
解法二:
class Solution {
public:
vector<string> generateAbbreviations(string word) {
vector<string> res;
for (int i = ; i < pow(, word.size()); ++i) {
string out = "";
int cnt = ;
for (int j = ; j < word.size(); ++j) {
if ((i >> j) & ) ++cnt;
else {
if (cnt != ) {
out += to_string(cnt);
cnt = ;
}
out += word[j];
}
}
if (cnt > ) out += to_string(cnt);
res.push_back(out);
}
return res;
}
};
那么迭代的写法看完了,来考虑一些递归的写法吧,上网搜了一下,发现下面三种写法比较容易理解,
解法三:
class Solution {
public:
vector<string> generateAbbreviations(string word) {
vector<string> res{word};
helper(word, , res);
return res;
}
void helper(string word, int pos, vector<string> &res) {
for (int i = pos; i < word.size(); ++i) {
for (int j = ; i + j <= word.size(); ++j) {
string t = word.substr(, i);
t += to_string(j) + word.substr(i + j);
res.push_back(t);
helper(t, i + + to_string(j).size(), res);
}
}
}
};
上述方法返回结果的顺序为:
["word","1ord","1o1d","1o2","1or1","2rd","2r1","3d","4","w1rd","w1r1","w2d","w3","wo1d","wo2","wor1"]
解法四:
class Solution {
public:
vector<string> generateAbbreviations(string word) {
vector<string> res;
helper(word, , , "", res);
return res;
}
void helper(string word, int pos, int cnt, string out, vector<string> &res) {
if (pos == word.size()) {
if (cnt > ) out += to_string(cnt);
res.push_back(out);
} else {
helper(word, pos + , cnt + , out, res);
helper(word, pos + , , out + (cnt > ? to_string(cnt) : "") + word[pos], res);
}
}
};
上述方法返回结果的顺序为:
["4","3d","2r1","2rd","1o2","1o1d","1or1","1ord","w3","w2d","w1r1","w1rd","wo2","wo1d","wor1","word"]
解法五:
class Solution {
public:
vector<string> generateAbbreviations(string word) {
vector<string> res;
res.push_back(word.size() == ? "" : to_string(word.size()));
for (int i = ; i < word.size(); ++i) {
for (auto a : generateAbbreviations(word.substr(i + ))) {
string left = i > ? to_string(i) : "";
res.push_back(left + word.substr(i, ) + a);
}
}
return res;
}
};
上述方法返回结果的顺序为:
["4","w3","wo2","wor1","word","wo1d","w1r1","w1rd","w2d","1o2","1or1","1ord","1o1d","2r1","2rd","3d"]
参考资料:
https://leetcode.com/problems/generalized-abbreviation/
https://leetcode.com/discuss/75754/java-backtracking-solution
https://leetcode.com/discuss/77280/c-straightforward-recursive-solution
LeetCode All in One 题目讲解汇总(持续更新中...)
[LeetCode] Generalized Abbreviation 通用简写的更多相关文章
- LeetCode Generalized Abbreviation
原题链接在这里:https://leetcode.com/problems/generalized-abbreviation/ 题目: Write a function to generate the ...
- [LeetCode] Word Abbreviation 单词缩写
Given an array of n distinct non-empty strings, you need to generate minimal possible abbreviations ...
- [Swift]LeetCode320. 通用简写 $ Generalized Abbreviation
Write a function to generate the generalized abbreviations of a word. Example: Given word = "wo ...
- LeetCode 320. Generalized Abbreviation
原题链接在这里:https://leetcode.com/problems/generalized-abbreviation/ 题目: Write a function to generate the ...
- 【LeetCode】320. Generalized Abbreviation 解题报告 (C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS 日期 题目地址:https://leetcod ...
- LeetCode Word Abbreviation
原题链接在这里:https://leetcode.com/problems/word-abbreviation/description/ 题目: Given an array of n distinc ...
- 320. Generalized Abbreviation
首先想到的是DFS,对于每个单词的字母都遍历,比如 spy: 1py,s1y,sp1 然后每个遍历完的单词再DFS..左右有数字就合并比如 1py: 11y=>2py, 1p1 这样.. 但是单 ...
- [Locked] Generalized Abbreviation
Write a function to generate the generalized abbreviations of a word. Example:Given word = "wor ...
- LeetCode 527---Word Abbreviation
527. Word Abbreviation Given an array of n distinct non-empty strings, you need to generate minimal ...
随机推荐
- Android来电监听和去电监听
我觉得写文章就得写得有用一些的,必须要有自己的思想,关于来电去电监听将按照下面三个问题展开 1.监听来电去电有什么用? 2.怎么监听,来电去电监听方式一样吗? 3.实战,有什么需要特别注意地方? 监听 ...
- FFmpeg学习4:音频格式转换
前段时间,在学习试用FFmpeg播放音频的时候总是有杂音,网上的很多教程是基于之前版本的FFmpeg的,而新的FFmepg3中audio增加了平面(planar)格式,而SDL播放音频是不支持平面格式 ...
- 利用Python进行数据分析(2) 尝试处理一份JSON数据并生成条形图
一.JSON 数据准备 首先准备一份 JSON 数据,这份数据共有 3560 条内容,每条内容结构如下: 本示例主要是以 tz(timezone 时区) 这一字段的值,分析这份数据里时区的分布情况. ...
- TeamCity : Build 版本控制系统配置
VCS (版本控制系统) 是用来跟踪项目源文件版本变化的系统.它还有其它的名字,比如 SCM(源代码管理).当前 TeamCity 内置支持的 VCS 类型有:Git, Subversion, Mer ...
- 从架构层面谈web加载优化(个人整理)
最近听了阿里一位大牛的讲座,讲web架构优化对网页加载的影响,看完之后对他所讲的一些优化方法进行一些总结和整理,发现收获还是蛮多的,下面多为个人整理和个人见解,希望有说的不对的,能及时指出 1.DNS ...
- asp.net core 简单部署之FTP配置(CentOS 7.0安装配置Vsftp服务器)
配置过程原文地址:http://www.osyunwei.com/archives/9006.html 坑和结果 正确的跟着这个内容走,是靠谱的. 我自己给自己踩了个坑,请参照文章的朋友注意第七条:七 ...
- 安装MySql for Visual Studio的坑
阅读目录 问题描述 解决过程 解决方案 总结 回到顶部 问题描述 安装MySql for Visual Studio 一般来说是为了能在VS的服务器数据连接的数据源中能选择MySql类型,如下图: 但 ...
- 配置mac百度云同步盘
1. 选择同步盘在电脑中的位置,该文件夹中的内容与云端保持一致.默认位置/Users/LemonVerbena/百度云同步盘.电脑同步盘的作用与百度云网盘的主页一样,下面可以包括多个同步文件夹. 2. ...
- Java核心技术点之多线程
学习Java的同学注意了!!! 学习过程中遇到什么问题或者想获取学习资源的话,欢迎加入Java学习交流群,群号码:279558494 我们一起学Java! 本文主要从整体上介绍Java中的多线程技术, ...
- React Native 之 View使用
前言 学习本系列内容需要具备一定 HTML 开发基础,没有基础的朋友可以先转至 HTML快速入门(一) 学习 本人接触 React Native 时间并不是特别长,所以对其中的内容和性质了解可能会有所 ...