zoukankan      html  css  js  c++  java
  • leetcode40

     1 class Solution {
     2     public List<List<Integer>> combinationSum2(int[] candidates, int target) {
     3         List<List<Integer>> combinations = new ArrayList<>();
     4         Arrays.sort(candidates);
     5         List<Integer> tempCombination = new ArrayList<>();
     6         backtracking(tempCombination, combinations, new boolean[candidates.length], 0, target, candidates);
     7         return combinations;
     8     }
     9 
    10     private void backtracking(List<Integer> tempCombination, List<List<Integer>> combinations,
    11                               boolean[] hasVisited, int start, int target, final int[] candidates) {
    12 
    13         if (target == 0) {
    14             combinations.add(new ArrayList<>(tempCombination));
    15             return;
    16         }
    17         for (int i = start; i < candidates.length; i++) {
    18             if (i != 0 && candidates[i] == candidates[i - 1] && !hasVisited[i - 1]) {
    19                 continue;
    20             }
    21             if (candidates[i] <= target) {
    22                 tempCombination.add(candidates[i]);
    23                 hasVisited[i] = true;
    24                 backtracking(tempCombination, combinations, hasVisited, i + 1, target - candidates[i], candidates);
    25                 hasVisited[i] = false;
    26                 tempCombination.remove(tempCombination.size() - 1);
    27             }
    28         }
    29     }
    30 }

    对比leetcode39

     1 public class LEET_39 {
     2     public List<List<Integer>> combinationSum(int[] candidates, int target) {
     3         List<List<Integer>> combinations = new ArrayList<>();
     4         List<Integer> tempCombination = new ArrayList<>();
     5         backtracking(tempCombination, combinations, 0, target, candidates);
     6         return combinations;
     7     }
     8 
     9     private void backtracking(List<Integer> tempCombination, List<List<Integer>> combinations,
    10                               int start, int target, final int[] candidates) {
    11 
    12         if (target == 0) {
    13             combinations.add(new ArrayList<>(tempCombination));
    14             return;
    15         }
    16         for (int i = start; i < candidates.length; i++) {
    17             if (candidates[i] <= target) {
    18                 tempCombination.add(candidates[i]);
    19                 backtracking(tempCombination, combinations, i, target - candidates[i], candidates);
    20                 tempCombination.remove(tempCombination.size() - 1);
    21             }
    22         }
    23     }
    24 }
  • 相关阅读:
    python通过帐号和密码访问mysql
    pyqt5简单登陆界面
    PyQt5点击按钮产生新窗体
    ubuntu访问win10下的磁盘
    python平均值和加权平均值
    牛顿迭代法求高次方程的根
    运用模逆运算(同余方程)来解决Matlab课上的一道思考题
    线程实现方式与临界区保护
    线程调度的问题:Lock Convoy(锁封护)与Priority Inversion(优先级反转)
    C++ lvalue,prvalue,xvalue,glvalue和rvalue详解(from cppreference)
  • 原文地址:https://www.cnblogs.com/asenyang/p/10998015.html
Copyright © 2011-2022 走看看