资讯专栏INFORMATION COLUMN

320. Generalized Abbreviation and 22. Generate Par

lanffy / 2636人阅读

320 Generalized Abbreviation

public class Solution {
    public List generateAbbreviations(String word) {
        List res = new ArrayList();
        backtrack(res, word, 0, "", 0);
        return res;
    }
    
    public void backtrack(List res, String word, int pos, String abbr, int count){
        if(pos == word.length()){
            if(count > 0) abbr += count;
            res.add(abbr);
        } else {
            backtrack(res, word, pos+1, abbr, count+1);   // 变成数字
            backtrack(res, word, pos+1, abbr + (count > 0 ? count : "") + word.charAt(pos), 0);  // 保留
        }
    }
}

22 Generate Parentheses

public class Solution {
    public List generateParenthesis(int n) {
        List res = new ArrayList();
        dfs(res, "", n, 0);
        return res;
    }
    
    // n represent "(",  m represent ")"
    public void dfs(List res, String path, int n, int m){
        if(n == 0 && m == 0){
            res.add(path);
            return;
        }
        
        if(n > 0){
            dfs(res, path + "(", n-1, m+1);
        }
        
        if(m > 0){
            dfs(res, path  + ")", n, m-1);
        }
    }
}

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/66923.html

相关文章

  • 320. Generalized Abbreviation

    摘要:题目链接要输出所有的结果,标准思路。也可以做,保留为,改为数字的为,然后结果就是这么多,每个数学遍历一遍求对应的即可。 320. Generalized Abbreviation 题目链接:https://leetcode.com/problems... 要输出所有的结果,backtracking标准思路。 public class Solution { public List...

    yangrd 评论0 收藏0
  • [LeetCode]Generalized Abbreviation

    摘要:分析这道题第一步一定要理解题意,首先要考虑的是会有多少种结果。仔细观察会发现,最终会有种结果。然后就很显然应该用每次存下当前结果,然后继续。 Generalized Abbreviation Write a function to generate the generalized abbreviations of a word. Example:Given word = word, ...

    ZoomQuiet 评论0 收藏0
  • [LeetCode] 408. Valid Word Abbreviation

    Problem Given a non-empty string s and an abbreviation abbr, return whether the string matches with the given abbreviation. A string such as word contains only the following valid abbreviations: [word...

    zone 评论0 收藏0
  • Unique Word Abbreviation LC解题记录

    摘要:题目内容这题也是锁住的,通过率只有左右。另外,字典里面只有两个的时候,也是返回。最后再说两句距离上一篇文章过了一段时间了,这段时间搬家再适应新环境,解决心理问题。 题目内容 An abbreviation of a word follows the form . Below are some examples of word abbreviations: a) it ...

    curried 评论0 收藏0
  • 在手机web中播放视频(使用js,不使用video标签,支持直播)

    摘要:主要原理是使用链接。是中解析视频,并把内容画在画布上。目前发现的不足无法播放声音,只能播放视频。视频文件只支持格式的视频目前版本支持视频格式,似乎是不支持了,官方建议用来转格式。 主要原理是使用 jsmpeg(Github链接) 。 jsmpeg是js中解析mpeg视频,并把内容画在画布上。 这篇文章是记录jsmpeg怎么用的。 目前发现jsmpeg的不足 无法播放声音,只能播放视...

    raise_yang 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<