力扣77组合(剪枝优化),216组合总和,17电话号码的组合,39组合总和1(可使用重复元素,但数组中不包含相同元素),40组合总和2(不可重复使用相同元素,但数组中含有相同的元素就可以使用),113分割回文串
    1. 77组合

    1. private List<List<Integer>>res=new ArrayList<>();
    2. private List<Integer>singleRes=new LinkedList<>();
    3. public List<List<Integer>> combine(int n, int k) {
    4. backTrack(n,k,1);
    5. return res;
    6. }
    7. private void backTrack(int n,int k,int index){
    8. if(singleRes.size()==k){
    9. res.add(new ArrayList<>(singleRes));
    10. return;
    11. }
    12. for(int i=index;i<=n;i++){
    13. singleRes.add(i);
    14. backTrack(n,k,i+1);
    15. singleRes.remove(singleRes.size()-1);
    16. }
    17. }
    18. }
    19. 如果剪枝的话就是
    20. for(int i=index;i<=n-(k-singleRes.size())+1;i++){
    21. 自己用n=4,k=4来试试效果。
    1. 216组合总和
    1. private void backTrack(int k,int n,int index){
    2. if(singleRes.size()==k){
    3. if(sum==n){
    4. res.add(new ArrayList<>(singleRes));
    5. return;
    6. }else{
    7. return;
    8. }
    9. }
    10. for(int i=index;i<=9;i++){
    11. singleRes.add(i);
    12. sum+=i;
    13. backTrack(k,n,i+1);
    14. singleRes.remove(singleRes.size()-1);
    15. sum-=i;
    16. }
    17. }

    14电话号码的字母组合

    1. private List<String>res=new ArrayList<>();
    2. private StringBuilder sb=new StringBuilder();
    3. private String[] reflection=new String[]{"","","abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"};
    4. public List<String> letterCombinations(String digits) {
    5. if(digits.equals("")||digits.length()==0){
    6. return res;
    7. }
    8. backTrack(0,digits.length(),digits);
    9. return res;
    10. }
    11. private void backTrack(int index,int length,String digits){
    12. if(sb.length()==length){
    13. res.add(sb.toString());
    14. return;
    15. }
    16. for(int i=index;i<digits.length();i++){
    17. int location = digits.charAt(i) - '0';
    18. String str=reflection[location];
    19. for(int j=0;j<str.length();j++){
    20. sb.append(str.charAt(j));
    21. backTrack(i+1,length,digits);
    22. sb.deleteCharAt(sb.length()-1);
    23. }
    24. }
    25. }

    39 组合总和1(可以使用相同的数组元素)

    1. private List<List<Integer>>res=new ArrayList<>();
    2. private List<Integer>singleRes=new LinkedList<>();
    3. private int sum=0;
    4. public List<List<Integer>> combinationSum(int[] candidates, int target) {
    5. Arrays.sort(candidates);
    6. backTrack(0,candidates,target);
    7. return res;
    8. }
    9. private void backTrack(int index,int[] candidates,int target){
    10. if(sum==target){
    11. res.add(new ArrayList<>(singleRes));
    12. return;
    13. }else if(sum>target){
    14. return;
    15. }
    16. for(int i=index;i<candidates.length;i++){
    17. singleRes.add(candidates[i]);
    18. sum+=candidates[i];
    19. backTrack(i,candidates,target);
    20. singleRes.remove(singleRes.size()-1);
    21. sum-=candidates[i];
    22. }
    23. }
    1. 40. 组合总和2
    1. private List<List<Integer>>res=new ArrayList<>();
    2. private List<Integer>singleRes=new LinkedList<>();
    3. private int sum=0;
    4. public List<List<Integer>> combinationSum2(int[] candidates, int target) {
    5. Arrays.sort(candidates);
    6. backTrack(0,candidates,target);
    7. return res;
    8. }
    9. private void backTrack(int index,int[]candidates,int target){
    10. if(sum==target){
    11. res.add(new ArrayList<>(singleRes));
    12. return;
    13. }else if(sum>target){
    14. return;
    15. }
    16. for(int i=index;i<candidates.length;i++){
    17. if(i>index&&candidates[i]==candidates[i-1]){
    18. continue;
    19. }
    20. singleRes.add(candidates[i]);
    21. sum+=candidates[i];
    22. backTrack(i+1,candidates,target);
    23. singleRes.remove(singleRes.size()-1);
    24. sum-=candidates[i];
    25. }
    26. }

    131 分割回文串

    1. private List<List<String>>res=new ArrayList<>();
    2. private List<String>singleRes=new LinkedList<>();
    3. public List<List<String>> partition(String s) {
    4. backTrack(0,s);
    5. return res;
    6. }
    7. private void backTrack(int index,String s){
    8. if(index>=s.length()){
    9. res.add(new ArrayList<>(singleRes));
    10. return;
    11. }
    12. for(int i=index;i<s.length();i++){
    13. if(check(s,index,i)) {
    14. singleRes.add(s.substring(index,i+1));
    15. backTrack(i+1,s);
    16. singleRes.remove(singleRes.size()-1);
    17. }
    18. }
    19. }
    20. private boolean check(String s,int start,int end){
    21. for(int i=start,j=end;i<end;i++,j--){
    22. if(s.charAt(i)!=s.charAt(j)){
    23. return false;
    24. }
    25. }
    26. return true;
    27. }