标签:tab ack ref targe add port 更新 ati public
package leetcode_50; import java.util.ArrayList; import java.util.Arrays; import java.util.List; /*** * * @author pengfei_zheng * 求解满足加和等于target的所有数字组合 */ public class Solution39 { public List<List<Integer>> combinationSum(int[] nums, int target) { List<List<Integer>> list = new ArrayList<>(); Arrays.sort(nums); backtrack(list, new ArrayList<>(), nums, target, 0); return list; } private void backtrack(List<List<Integer>> list, List<Integer> tempList, int [] nums, int remain, int start){ if(remain < 0) return; else if(remain == 0) list.add(new ArrayList<>(tempList)); else{ for(int i = start; i < nums.length; i++){ tempList.add(nums[i]); backtrack(list, tempList, nums, remain - nums[i], i); // not i + 1 because we can reuse same elements tempList.remove(tempList.size() - 1); } } } }
LeetCode 39 Combination Sum(满足求和等于target的所有组合)
标签:tab ack ref targe add port 更新 ati public
原文地址:http://www.cnblogs.com/zpfbuaa/p/6537720.html