代码随想录算法训练营29期|day 25 任务以及具体安排

  •  216.组合总和III
  • 代码随想录算法训练营29期|day 25 任务以及具体安排_第1张图片
    class Solution {
    	List> result = new ArrayList<>();
    	LinkedList path = new LinkedList<>();
    
    	public List> combinationSum3(int k, int n) {
    		backTracking(n, k, 1, 0);
    		return result;
    	}
    
    	private void backTracking(int targetSum, int k, int startIndex, int sum) {
    		// 减枝
    		if (sum > targetSum) {
    			return;
    		}
    
    		if (path.size() == k) {
    			if (sum == targetSum) result.add(new ArrayList<>(path));
    			return;
    		}
    
    		// 减枝 9 - (k - path.size()) + 1
    		for (int i = startIndex; i <= 9 - (k - path.size()) + 1; i++) {
    			path.add(i);
    			sum += i;
    			backTracking(targetSum, k, i + 1, sum);
    			//回溯
    			path.removeLast();
    			//回溯
    			sum -= i;
    		}
    	}
    }

    思路:回溯的基本模板,关键是剪枝的操作,if(sum > targetSum)进行剪枝,9-(k - path.size())+1进行剪枝。

  •  17.电话号码的字母组合
  • 代码随想录算法训练营29期|day 25 任务以及具体安排_第2张图片
    class Solution {
    
        //设置全局列表存储最后的结果
        List list = new ArrayList<>();
    
        public List letterCombinations(String digits) {
            if (digits == null || digits.length() == 0) {
                return list;
            }
            //初始对应所有的数字,为了直接对应2-9,新增了两个无效的字符串""
            String[] numString = {"", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
            //迭代处理
            backTracking(digits, numString, 0);
            return list;
    
        }
    
        //每次迭代获取一个字符串,所以会设计大量的字符串拼接,所以这里选择更为高效的 StringBuild
        StringBuilder temp = new StringBuilder();
    
        //比如digits如果为"23",num 为0,则str表示2对应的 abc
        public void backTracking(String digits, String[] numString, int num) {
            //遍历全部一次记录一次得到的字符串
            if (num == digits.length()) {
                list.add(temp.toString());
                return;
            }
            //str 表示当前num对应的字符串
            String str = numString[digits.charAt(num) - '0'];
            for (int i = 0; i < str.length(); i++) {
                temp.append(str.charAt(i));
                //c
                backTracking(digits, numString, num + 1);
                //剔除末尾的继续尝试
                temp.deleteCharAt(temp.length() - 1);
            }
        }
    }
    class Solution {
        Listresult = new ArrayList<>();
        StringBuilder path = new StringBuilder();
        String[] numString = {"","","abc", "def", "ghi", "jkl","mno","pqrs","tuv","wxyz"};
        public List letterCombinations(String digits) {
            if(digits == null || digits.length() == 0) {
                return result;
            }
            backTracking(digits, 0);
            return result;
        }
    
        public void backTracking(String digits, int index){
            if(index == digits.length()){
                result.add(path.toString());
                return;
            }
    
            int temp = digits.charAt(index)-'0';
            String s = numString[temp];
            for(int i = 0 ; i < s.length() ; i++){
                path.append(s.charAt(i)) ;
                backTracking(digits,index+1);
                path.deleteCharAt(path.length()-1);
            }
        }
    }

    思路:要把数字对应为字符串,将num作为遍历第几个数字的标识,递归num++,往深遍历,for循环是往宽遍历,遍历一个数字对应的所有字符

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