算法Day27|39. 组合总和 ● 40.组合总和II ● 131.分割回文串

44 阅读1分钟
题目:39. 组合总和 - 力扣(LeetCode)
代码实现:
class Solution {
    public List<List<Integer>> combinationSum(int[] candidates, int target) {
        List<List<Integer>> ans = new ArrayList<>();
        List<Integer> path = new ArrayList<>();
        backtracking(ans, path, candidates, target, 0, 0);
        return ans;
    }
    void backtracking(List<List<Integer>> ans, List<Integer> path, int[] candidates, int target, int sum, int startIndex) {
        if (sum > target) return;
        if (sum == target) {
            ans.add(new ArrayList<>(path));
            return;
        }
        for (int i = startIndex; i < candidates.length; i++) {
            path.add(candidates[i]);
            backtracking(ans, path, candidates, target, sum + candidates[i], i);
            path.remove(path.size() - 1);
        }
    }
}
题目:40. 组合总和 II - 力扣(LeetCode)
代码实现:
class Solution {
    List<List<Integer>> ans = new ArrayList<>();
    List<Integer> path = new ArrayList<>();
    int sum = 0;
    public List<List<Integer>> combinationSum2(int[] candidates, int target) {
        Arrays.sort(candidates);
        backtracking(ans, path, candidates, target, 0, 0);
        return ans;
    }
    void backtracking(List<List<Integer>> ans, List<Integer> path, int[] candidates, int target, int sum, int startIndex) {
        if (sum > target) return;
        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(ans, path, candidates, target, sum, i + 1);
            int temp = path.get(path.size() - 1); // path.getLast()编译错误
            sum -= temp;
            path.remove(path.size() - 1);
        }
    }
}
// 这可能是因为在编译时,编译器无法确定path对象是否有getLast()方法。
            // 而调用path.get(path.size() - 1)则是直接使用了已知的方法和属性,因此可以通过编译。可能的原因是getLast()方法没有被正确地定义或导入到代码中。
题目:
代码实现:
class Solution {
    List<List<String>> ans = new ArrayList<>();
    List<String> path = new ArrayList<>();

    public List<List<String>> partition(String s) {
        backtracking(s, 0);
        return ans;
    }
    void backtracking(String s, int startIndex) {
        if (startIndex >= s.length()) {
            ans.add(new ArrayList<>(path));
            return;
        }

        for (int i = startIndex; i < s.length(); i++) {
            if (isValid(s, startIndex, i)) {
                String str = s.substring(startIndex, i + 1);
                path.add(str);
            } else {
                continue;
            }
            backtracking(s, i + 1);
            path.remove(path.size() - 1);
        }

    }
    boolean isValid(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;
    }
}