赞
踩
1.题目
2.解法
①一维数组+dfs
public class Solution { public List<List<Integer>> kSumII(int[] A, int k, int target) { List<List<Integer>> res = new ArrayList<>(); List<Integer> temp = new ArrayList<>(); int n = A.length; dfs(0, k, n, A, target, res, temp); return res; } private void dfs(int start, int k, int n, int[] A, int target, List<List<Integer>> res, List<Integer> temp){ if(k == 0 && target == 0){ res.add(new ArrayList<>(temp)); return; } for(int i = start; i < n; i++){ temp.add(A[i]); dfs(i + 1, k - 1, n, A, target - A[i], res, temp); temp.remove(temp.size() - 1); } } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。