320. Generalized Abbreviation
2016-03-14 13:41
453 查看
Problem
Write a function to generate the generalized abbreviations of a word.Example:
Given word =
"word", return the following list (order does not matter):
["word", "1ord", "w1rd", "wo1d", "wor1", "2rd", "w2d", "wo2", "1o1d", "1or1", "w1r1", "1o2", "2r1", "3d", "w3", "4"]
Solution
典型的 backtracking, 唯一要注意的是例子给的单词长度是各位数,要考虑单词很长的时候可以是两位数,这样就不能用 num + '0' 来表示当前的数字字符了。class Solution { void helper(int begin, string& oneRst, vector<string>& rst, const string& word ){ if(begin == word.size()) { rst.push_back(oneRst); return; } oneRst.push_back(word[begin]); helper(begin + 1, oneRst, rst, word); oneRst.pop_back(); if( oneRst.empty() || oneRst.back() < '0' || oneRst.back() > '9' ){ for( int numLen = 1; numLen <= word.size() - begin; numLen++) { string str = to_string(numLen); oneRst.append(str); helper(begin + numLen, oneRst, rst, word); oneRst.erase(oneRst.end() - str.size(), oneRst.end()); } } } public: vector<string> generateAbbreviations(string word) { string oneRst; vector<string> rst; helper(0, oneRst, rst, word); return rst; } };
相关文章推荐
- Android中home键和back键区别实例分析
- Android Back键点击两次退出应用详解及实现方法总结
- Android实现长按back键退出应用程序的方法
- Android解决dialog弹出时无法捕捉Activity的back事件的方法
- Android监听Home键和Back键的区别介绍
- Android实现Back功能代码片段总结
- android 按两次返回键退出
- 模拟按键
- android 硬件上的back键处理
- Android重写返回按钮
- 有对话框时,偶尔会捕捉不到返回键
- Android按键拦截处理最佳实践范例(以Back事件为例)
- 点击回退键时出现连续返回的情况
- Tracking-Learning-Detection TLD解析一 - 前言
- Tracking-Learning-Detection TLD解析二 - 跟踪器及检测器设计
- Tracking-Learning-Detection TLD解析三 - Learning学习(跟踪与检测的协调与更新)
- Tracking-Learning-Detection TLD解析四 - 扩展及局限
- 提高增量备份的速度
- 跑Tracking-Learning-Detection (TLD)是遇到的问题及解决方法
- STL 中的back()方法(12)