zoukankan      html  css  js  c++  java
  • Combination Sum II

    Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T.

    Each number in C may only be used once in the combination.

    Note:

    • All numbers (including target) will be positive integers.
    • Elements in a combination (a1a2, … , ak) must be in non-descending order. (ie, a1 ≤ a2 ≤ … ≤ ak).
    • The solution set must not contain duplicate combinations.

    For example, given candidate set 10,1,2,7,6,1,5 and target 8
    A solution set is: 
    [1, 7] 
    [1, 2, 5] 
    [2, 6] 
    [1, 1, 6]

     1 public class Solution {
     2     ArrayList<ArrayList<Integer>> result = null;
     3     public ArrayList<ArrayList<Integer>> combinationSum2(int[] num, int target) {
     4         // Note: The Solution object is instantiated only once and is reused by each test case.
     5         Arrays.sort(num);
     6         result = new ArrayList<ArrayList<Integer>>();
     7         getSolution(new ArrayList<Integer>(), target, num, 0);
     8         return result;
     9     }
    10     public void getSolution(ArrayList<Integer> row, int target, int[] num, int pos){
    11         int i = pos;
    12         int per = -1;
    13         if(target == 0){
    14             result.add(row);
    15         }
    16         while(i < num.length && num[i] <= target){
    17             if(per != num[i]){
    18                 per = num[i];
    19                 row.add(num[i]);
    20                 target -= num[i];
    21                 int tmp = num[i];
    23 getSolution(new ArrayList<Integer>(row), target, num, i + 1); 24 row.remove(row.size() - 1);
    26 target += num[i]; 27 } 28 i ++; 29 } 30 } 31 }

     第三遍:

    test case:

    Input: [1,1], 1
    Output: [[1],[1]]
    Expected: [[1]]

    结果相同如何处理。

     1 public class Solution {
     2     public List<List<Integer>> combinationSum2(int[] candidates, int target) {
     3         Arrays.sort(candidates);
     4         List<List<Integer>> result = new ArrayList<List<Integer>>();
     5         findElement(candidates, 0, candidates.length, result, new ArrayList<Integer>(), target);
     6         return result;
     7     }
     8     
     9     public void findElement(int[] arr, int start, int end, List<List<Integer>> result, ArrayList<Integer> row, int tar){
    10         if(tar == 0) result.add(row);
    11         for(int i = start; i < end; i ++){
    12             if((i == start || arr[i] != arr[i - 1]) && arr[i] <= tar){
    13                 ArrayList<Integer> rown = new ArrayList<Integer> (row);
    14                 rown.add(arr[i]);
    15                 findElement(arr, i + 1, end, result, rown, tar - arr[i]);
    16             }
    17         }
    18     }
    19 }
    (i == start || arr[i] != arr[i - 1]) 保证了在同一层里不会选取一样的数字。
  • 相关阅读:
    solrCloud设置Tomcat jvm内存解决内存溢出的问题
    JAVA 将图片转换为Base64编码
    希望这是一个新的开始,也是一个好的开端
    最全华为鸿蒙 HarmonyOS 开发资料汇总
    iPhone X适配方案
    前端程序员经常忽视的一个JavaScript面试题
    vs for Mac 升级后编译原项目提示找不到“.NETFramework,Version=v5.0”的引用程序集
    使用FastReport的BarCode2D控件生成含中文的PDF417条形码
    vs for Mac中的启用Entity Framework Core .NET命令行工具
    64位Windows7升级IE11后无法启动的解决办法
  • 原文地址:https://www.cnblogs.com/reynold-lei/p/3371410.html
Copyright © 2011-2022 走看看