[LeetCode]Generalized Abbreviation

感觉我的方法逻辑不够清晰

public class Solution {
    private List<String> result;
    public List<String> generateAbbreviations(String word) {
        result = new ArrayList<String>();
        helper(word, -2, 0);
        result.add(word);
        return result;
    }
    public void helper(String word, int index, int l) {
        int length = word.length();
        for (int i = index + 2 + l; i < length; i++) {
            for (int j = 1; j + i <= length; j++) {
                String str = word.substring(0, i) + String.valueOf(j) + word.substring(i + j);
                result.add(str);
                helper(str, i, String.valueOf(j).length() - 1);
            }
        }
    }
}

 

posted @ 2016-01-03 23:08  Weizheng_Love_Coding  阅读(160)  评论(0编辑  收藏  举报