代码随想录算法训练营第二七天 | 回溯 组合 分割

目录

  • 组合总和
  • 组合总和II
  • 分割回文串

LeetCode 39. 组合总和
LeetCode 40.组合总和II
LeetCode 131.分割回文串

组合总和

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

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

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

2 <= candidates[i] <= 40

class Solution {
    List<List<Integer>> result = new ArrayList<>();
    List<Integer> list = new ArrayList<>();
    public List<List<Integer>> combinationSum(int[] candidates, int target) {
        // Arrays.sort(candidates); // 先进行排序
        backtracking(candidates,target,0);
        return result;
    }

    private void backtracking(int[] candidates, int target, int index) {  // index控制for循环的起始位置
    // 如果是一个集合来求组合的话,就需要startIndex
    // 如果是多个集合取组合,各个集合之间相互不影响,那么就不用startIndex
        if (target == 0) {
            result.add(new ArrayList<>(list));
            return;
        }
        if (target < 0) return;  // 终止条件记得写完

        for (int i = index; i < candidates.length; i++) {
            // if (target - candidates[i] < 0) break;  // 终止条件记得写完,放这里是剪枝
            list.add(candidates[i]);
            target -= candidates[i];
            backtracking(candidates, target, i); // 不用i+1了,表示可以重复读取当前的数
            list.removeLast();
            target += candidates[i];
        }
    }
}

组合总和II

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

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

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

代码随想录算法训练营第二七天 | 回溯 组合 分割_第1张图片

class Solution {

    List<List<Integer>> result = new ArrayList<>();
    LinkedList<Integer> path = new LinkedList<>();
    int sum = 0;

    public List<List<Integer>> combinationSum2(int[] candidates, int target) {
        Arrays.sort( candidates );
        backTracking( candidates, target, 0 );
        return result;
    }

    private void backTracking(int[] candidates, int target, int start) {
        if ( sum == target ) {
            result.add( new ArrayList<>(path) );
            return;
        }

        for ( int i = start; i < candidates.length && sum + candidates[i] <= target; i++ ) {
            // if (target - candidates[i] < 0) break;
            if ( i > start && candidates[i] == candidates[i - 1] ) {
                continue;
            }
            sum += candidates[i];
            path.add( candidates[i] );
            // i+1 代表当前组内元素只选取一次
            backTracking( candidates, target, i + 1 );
            int temp = path.getLast();
            sum -= temp;
            path.removeLast();   
        }
    }
}

分割回文串

切割问题,有不同的切割方式
判断回文

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

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

class Solution {
    List<List<String>> result = new ArrayList<>();
    Deque<String> deque = new LinkedList<>();
    public List<List<String>> partition(String s) {
        backTracking(s, 0);
        return result;
    }

    private void backTracking(String s, int startIndex) {
        // 起始位置大于 s 的大小, 说明找到了一组分割方案
        if (startIndex >= s.length()) {
            result.add(new ArrayList(deque));
            return;
        }
        for (int i = startIndex; i < s.length(); i++) {
            // 回文子串
            // 获取[startIndex,i]在s中的子串
            if (isPalindrome(s, startIndex, i)) {
                String str = s.substring(startIndex, i+1);  // 起始位置,截取长度
                deque.addLast(str);
            } else { // 不是回文,跳过
                continue;
            }
            // 起始位置后移, 保证不重复
            backTracking(s, i + 1);
            deque.removeLast();
        }
    }

    private boolean isPalindrome(String s, int startIndex, int end) {
        for (int i = startIndex, j = end; i < j; i++, j--) {
            if (s.charAt(i) != s.charAt(j)) {
                return false;
            }
        }
        return true;
    }
}

你可能感兴趣的:(算法,windows,linux)