代码随想录二刷day25

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档

文章目录

  • 前言
  • 一、力扣216. 组合总和 III
  • 二、力扣17. 电话号码的字母组合


前言


一、力扣216. 组合总和 III

class Solution {
    List<List<Integer>> res = new ArrayList<>();
    List<Integer> group = new ArrayList<>();
    public List<List<Integer>> combinationSum3(int k, int n) {
        fun(1, k, n, 0);
        return res;
    }
    public void fun(int start, int k, int n, int count){
        if(group.size() == k && count == n){
            res.add(new ArrayList<>(group));
            return ;
        }
        for(int i = start; i <= 9; i ++){
            count += i;
            group.add(i);
            fun(i+1, k, n, count);
            count -= i;
            group.remove(group.size()-1);
        }
    }
}

二、力扣17. 电话号码的字母组合

class Solution {
    String[] str = new String[]{"", "", "abc", "def", "ghi", "jkl", "mno", "qprs", "tuv", "wxyz"};
    List<String> res = new ArrayList<>();
    StringBuilder sb = new StringBuilder();
    public List<String> letterCombinations(String digits) {
        if(digits.length() == 0){
            return res;
        }
        fun(digits, 0);
        return res;
    }
    public void fun(String digits, int startInt){
        if(startInt == digits.length()){
            res.add(sb.toString());
            return;
        }
        String s = str[digits.charAt(startInt) - '0'];
        for(int i = 0; i < s.length(); i ++){
            sb.append(s.charAt(i));
            fun(digits, startInt+1);
            sb.deleteCharAt(sb.length()-1);
        }
    }
}

你可能感兴趣的:(windows,leetcode,算法,数据结构,java)