leetcode:216. 组合总和 III - 力扣(LeetCode)
class Solution { List<List<Integer>> res = new ArrayList<>(); LinkedList<Integer> link = new LinkedList<>(); public List<List<Integer>> combinationSum3(int k, int n) { backtracking(k,n,0,1); return res; } public void backtracking(int k , int n , int sum , int strartIndex){ if( sum > n) return; if(link.size() > k) return; //和等于目标值,存入二维数组 if (link.size() == k){ if(sum == n){ res.add(new ArrayList<>(link)); return; } } for(int i = strartIndex;i <= 9; i++){ link.add(i); sum += i; backtracking(k,n,sum,i+1); sum -= i; link.removeLast(); } } }
leetcode:17. 电话号码的字母组合 - 力扣(LeetCode)
class Solution { List<String> list = new ArrayList<>(); public List<String> letterCombinations(String digits) { if(digits == null || digits.length() == 0){ return list; } String []numString={"","","abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"}; backtracking(digits,numString,0); return list; } StringBuilder temp = new StringBuilder(); public void backtracking(String dights,String[] numString,int num ){ if(num == dights.length()){ list.add(temp.toString()); return; } String str = numString[dights.charAt(num) - '0']; for (int i =0 ; i < str.length(); i++){ temp.append(str.charAt(i)); backtracking(dights,numString,num+1); temp.deleteCharAt(temp.length() - 1); } } }
标签:216,return,String,int,随想录,字母组合,new,numString,backtracking From: https://www.cnblogs.com/lengbo/p/18091303