leetcode Generalized Abbreviation
来源:互联网 发布:趣头条提现是骗局 知乎 编辑:程序博客网 时间:2024/04/30 19:21
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"]
Show Similar Problems
Have you met this question in a real interview?
Yes
No
Discuss Pick One
主要说一下递归思路,自己之前写的感觉不是很正确,网上看到比较正确的思路是这样的,用一个变量count来表示当前连续的缩写个数,那么当遇到一个新的char时,只有两种选择:1、继续缩写,count+1 2、不缩写,输出当前count,输出该字符,重置count为0,代码:public List<String> generateAbbreviations(String word) { List<String> list=new ArrayList<>(); search(word.toCharArray(),list,new StringBuffer(),0,0); return list;}public void search(char[] word,List<String> list,StringBuffer str,int count,int len){ int length=str.length(); if(len==word.length){ if(count!=0) str.append(count); list.add(str.toString()); }else{ search(word,list,str,count+1,len+1); if(count!=0){ str.append(count); } search(word,list,str.append(word[len]),0,len+1); } str.setLength(length);}
0 0
- Leetcode: Generalized Abbreviation
- [LeetCode]Generalized Abbreviation
- leetcode Generalized Abbreviation
- LeetCode 320. Generalized Abbreviation
- Leetcode 320. Generalized Abbreviation
- Generalized Abbreviation
- Generalized Abbreviation
- [leetcode] 320. Generalized Abbreviation 解题报告
- LeetCode 320. Generalized Abbreviation(产生缩写)
- [Leetcode] 320. Generalized Abbreviation 解题报告
- [leetcode320]Generalized Abbreviation
- 320. Generalized Abbreviation
- 320. Generalized Abbreviation
- *LeetCode-Unique Word Abbreviation
- Leetcode Unique Word Abbreviation
- leetcode 288: Unique Word Abbreviation
- [leetcode 288] Unique Word Abbreviation
- LeetCode 256. Unique Word Abbreviation
- Android中为什么使用Log,而不适用System.out()?
- java基础Day01
- jq实现有透明遮罩的轻量级弹窗
- Good Bye 2014
- zookeeper 安装 windows环境
- leetcode Generalized Abbreviation
- HDOJ 1286 找新朋友【欧拉函数】
- 磁条磁道信息
- String、StringBuffer、StringBuilder 性能测试
- 修改MySql密码
- POJ 2777 Count Color
- 日期计算
- Codeforces 444C DZY Loves Colors
- HDU 5029 Relief grain(恶心的树链剖分 + 线段树)