k Sum II

Given n unique integers, number k

Example

Given [1,2,3,4], k = 2, target = 5. Return:

[
  [1,4],
  [2,3]
]

public class Solution {
    /**
     * @param A: an integer array.
     * @param k: a positive integer (k <= length(A))
     * @param target: a integer
     * @return a list of lists of integer
     */ 

    ArrayList<ArrayList<Integer>> res=new ArrayList<ArrayList<Integer>>();
    public ArrayList<ArrayList<Integer>> kSumII(int[] A, int k, int target) {
        // write your code here

        ArrayList<Integer> list=new ArrayList<Integer>();

        if(A.length==0 || A==null || k>A.length || k<0) return res;

        kSum(A,0,k,target,list);

        return res;

    }

    public void kSum(int[] A,int begin,int k, int target,ArrayList<Integer> preList)
    {

        if (k == 0)
        {
            if (target == 0)
                res.add(new ArrayList<Integer>(preList));
            return;
        }

        for(int i=begin;i<A.length;i++)
        {
                preList.add(A[i]);
                kSum(A,i+1,k - 1,target-A[i],preList);
                preList.remove(preList.size()-1);
        }
    }
}


 
时间: 2024-10-05 18:51:00

k Sum II的相关文章

Lintcode: k Sum II

Given n unique integers, number k (1<=k<=n) and target. Find all possible k integers where their sum is target. Example Given [1,2,3,4], k=2, target=5, [1,4] and [2,3] are possible solutions. 这道题同Combination Sum II 1 public class Solution { 2 /** 3

k Sum | &amp; ||

k Sum Given n distinct positive integers, integer k (k <= n) and a number target. Find k numbers where sum is target. Calculate how many solutions there are? Example Given [1,2,3,4], k = 2, target = 5. There are 2 solutions: [1,4] and [2,3]. Return 2

hdoj 1977 Consecutive sum II

Consecutive sum II Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2523    Accepted Submission(s): 1219 Problem Description Consecutive sum come again. Are you ready? Go ~~1    = 0 + 12+3+4    =

HDU1977 Consecutive sum II【水题】

Consecutive sum II Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2237    Accepted Submission(s): 1093 Problem Description Consecutive sum come again. Are you ready? Go ~~ 1    = 0 + 1 2+3+4  

Combination Sum,Combination Sum II,Combination Sum III

39. Combination Sum Given a set of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T. The same repeated number may be chosen from C unlimited number of times. Note: All numbers (inc

LeetCode_39combinationSum2 [Combination Sum II]

#pragma warning(disable:4996) #include <Windows.h> #include <tchar.h> #include <cstdio> #include <vector> using namespace std; /* submit time : 3 1. Runtime Error Last executed input: [5,3], 5 2. Wrong Answer Input: [2,2,2], 4 Outp

【LeetCode】167. Two Sum II - Input array is sorted

Two Sum II - Input array is sorted Given an array of integers that is already sorted in ascending order, find two numbers such that they add up to a specific target number. The function twoSum should return indices of the two numbers such that they a

【Leetcode】Path Sum II

Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given sum. For example: Given the below binary tree and sum = 22, 5 / 4 8 / / 11 13 4 / \ / 7 2 5 1 return [ [5,4,11,2], [5,8,4,5] ] 思路:与[Leetcode]Path Sum 不同

leetcode_113题——Path Sum II(深度优先搜索)

Path Sum II Total Accepted: 41402 Total Submissions: 155034My Submissions Question Solution Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given sum. For example:Given the below binary tree and sum = 22, 5