day27回溯

day27 回溯

组合总数

题目链接: 组合总数

题目描述

给你一个 无重复元素 的整数数组 candidates 和一个目标整数 target ,找出 candidates 中可以使数字和为目标数 target 的 所有 不同组合 ,并以列表形式返回。你可以按 任意顺序 返回这些组合。

candidates 中的 同一个 数字可以 无限制重复被选取 。如果至少一个数字的被选数量不同,则两种组合是不同的。

对于给定的输入,保证和为 target 的不同组合数少于 150 个。

解答

class Solution {
   
    public List<List<Integer>> combinationSum(int[] candidates, int target) {
   
        List<List<Integer>> res = new ArrayList<>();
        Arrays.sort(candidates);
        backTracking(res , new ArrayList<>() , candidates , target , 0 ,0 );
        return res;
    }
    void backTracking (List<List<Integer>> res , List<Integer> path , int[] candidates , int target , int sum , int index  ){
   
        if (sum == target) {
   
            res.add(new ArrayList<>(path));
            return;
        }

        for (int i = index ; i < candidates.length; i++) {
   
            if (sum + candidates[i] > target) break;
            path.add(candidates[i]);
            backTracking(res,path,candidates,target , sum+candidates[i] , i);
            path.remove(path.size() - 1);
        }
    }
}

组合总数

题目链接: 组合总数

题目描述

给定一个候选人编号的集合 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合。

candidates 中的每个数字在每个组合中只能使用 一次 。

注意:解集不能包含重复的组合。

解答

class Solution {
   
    LinkedList<Integer> path = new LinkedList<>();
    List<List<Integer>> ans = new ArrayList<>();
    boolean[] used;
    int sum = 0;
    public List<List<Integer>> combinationSum2(int[] candidates, int target) {
   
        used = new boolean[candidates.length];
        Arrays.fill(used , false);
        Arrays.sort(candidates);
        backTracking(candidates , target , 0);
        return ans;
    }
    void backTracking(int[] candidates , int target , int startIndex) {
   
        if (sum == target) {
   
            ans.add(new ArrayList<>(path));
        }
        for (int i = startIndex ; i < candidates.length  ; i++ ) {
   
            if (sum + candidates[i] > target) break;

            if (i > 0 && candidates[i] == candidates[i - 1] && !used[i - 1]) {
   
                continue;
            }
            used[i] = true;
            sum += candidates[i];
            path.add(candidates[i]);
            backTracking(candidates,target,i + 1);
            used[i] = false;
            sum -= candidates[i];
            path.removeLast();
        }
    }
}
class Solution {
   
    LinkedList<Integer> path = new LinkedList<>();
    List<List<Integer>> ans = new ArrayList<>();
    int sum = 0;
    public List<List<Integer>> combinationSum2(int[] candidates, int target) {
   
      Arrays.sort(candidates);
      backTracking(candidates , target , 0);
      return ans;
    }
    void backTracking(int[] candidates , int target , int startIndex) {
   
        if (sum == target) {
   
            ans.add(new ArrayList<>(path));
            return;
        }
        for (int i = startIndex ; i < candidates.length && sum + candidates[i] <= target ; i++) {
   
            if ( i > startIndex && candidates[i] == candidates[i -1]) {
   
                continue;
            }
            sum += candidates[i];
            path.add(candidates[i]);
            backTracking(candidates , target , i + 1);
            int temp = path.getLast();
            sum -= temp;
            path.removeLast();
        }
        
        
    }
}

分割回文串

题目链接: 分割回文串

题目描述

给你一个字符串 s,请你将 s 分割成一些子串,使每个子串都是 回文串 。返回 s 所有可能的分割方案。

回文串 是正着读和反着读都一样的字符串。

解题

class Solution {
   
    List<List<String>> lists = new ArrayList<>();
    Deque<String> deque = new LinkedList<>();
    public List<List<String>> partition(String s) {
   
        backTracking(s , 0);
        return lists;
    }
    void backTracking(String s , int index) {
   
        if (index >= s.length()) {
   
            lists.add(new ArrayList(deque));
            return;
        }
        for (int i = index ; i < s.length() ; i++) {
   
            if (isPalindrome(s , index , i)) {
   
                String temp = s.substring(index , i + 1);
                deque.addLast(temp);
            } else {
   
                continue;
            }
            backTracking( s , i +1);
            deque.removeLast();
        }
    }
    boolean isPalindrome(String s , int startIndex , int endIndex) {
   
        for (int i = startIndex , j = endIndex ; i < j ; i++ , j--) {
   
            if (s.charAt(i) != s.charAt(j)) {
   
                return false;
            }
        }
        return true;
    }
}

相关推荐

  1. day27 回溯(03)

    2024-02-06 07:54:01       70 阅读
  2. day27回溯

    2024-02-06 07:54:01       57 阅读
  3. Day 29 回溯05

    2024-02-06 07:54:01       43 阅读
  4. Day 24 回溯算法 1

    2024-02-06 07:54:01       62 阅读
  5. Day 24 回溯算法01

    2024-02-06 07:54:01       36 阅读
  6. day 27 第七章 回溯算法part03

    2024-02-06 07:54:01       35 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-02-06 07:54:01       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-02-06 07:54:01       106 阅读
  3. 在Django里面运行非项目文件

    2024-02-06 07:54:01       87 阅读
  4. Python语言-面向对象

    2024-02-06 07:54:01       96 阅读

热门阅读

  1. iOS面试题

    2024-02-06 07:54:01       52 阅读
  2. zk集群--集群同步

    2024-02-06 07:54:01       41 阅读
  3. 设计模式(创建型模式)原型模式

    2024-02-06 07:54:01       45 阅读
  4. 系统架构设计师-22年-上午答案

    2024-02-06 07:54:01       38 阅读
  5. spring-boot-actuator 服务监控

    2024-02-06 07:54:01       44 阅读