immiao0319

[抄题]:

Given a digit string excluded 01, return all possible letter combinations that the number could represent.

A mapping of digit to letters (just like on the telephone buttons) is given below.

给定 "23"

返回 ["ad", "ae", "af", "bd", "be", "bf", "cd", "ce", "cf"]

 [暴力解法]:

时间分析:

空间分析:

[思维问题]:

  1. 以为是树中分治型的DFS, 其实是图中枚举型的DFS,写法都不一样,应该是for循环,没有概念

[一句话思路]:

DFS中嵌套DFS控制总体变量的改变,图中枚举型的for循环控制局部变量的改变。

[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

[画图]:

[一刷]:

  1.  主函数里传入的字符串一开始应该是“”空串,表示dfs的开始。以前写过但是不理解
  2. digits.charAt(x) - \'0\'; 可以把输入的字符变成数字来使用,没用过
  3. dfs中不需要画蛇添足地解释l是什么,形式参数是自带的,之前不理解。//int l = digits.length();

[二刷]:

[三刷]:

[四刷]:

[五刷]:

  [五分钟肉眼debug的结果]:

[总结]:

枚举型还是DFS嵌套DFS,不过里面有循环

[复杂度]:Time complexity: O(3^n) Space complexity: O(n

新建一个链表 空间复杂度还是n, 就地使用原来的链表 空间复杂度是1

[英文数据结构或算法,为什么不用别的数据结构或算法]:

[其他解法]:

[Follow Up]:

[LC给出的题目变变变]:

22. Generate Parentheses 涉及到穷举,用dfs回溯法,忘了

 [代码风格] :

public class Solution {
    /**
     * @param digits: A digital string
     * @return: all posible letter combinations
     */
     List<String> ans = new LinkedList<>();
     
    public List<String> letterCombinations(String digits) {
        //corner case
        if (digits.length() == 0) {
            return ans;
        }
        String[] phone = {"", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
        dfs(0, digits.length(), "", digits, phone);
        return ans;
    }
    
    //bfs
    //exit and execute
    private void dfs (int x, int l, String str, String digits, String[] phone) {
        //int l = digits.length();
        if (x == l) {
            ans.add(str);
            return;
        }
        
        int d = digits.charAt(x) - \'0\';
        for (char c : phone[d].toCharArray()) {
            dfs(x + 1, l, str + c, digits, phone);
        }
    }
}
View Code

 

分类:

技术点:

相关文章:

  • 2021-05-11
  • 2021-10-03
  • 2021-05-11
  • 2021-04-20
  • 2021-09-29
  • 2021-09-29
  • 2021-09-29
  • 2021-09-29
猜你喜欢
  • 2021-10-09
  • 2021-09-29
  • 2021-04-24
  • 2022-01-11
  • 2021-10-12
相关资源
相似解决方案