标签:font get tin continue util public script add return
package leetcode_50; import java.util.ArrayList; import java.util.Arrays; import java.util.List; /*** * * @author pengfei_zheng * 数组中求和等于target的所有组合 */ public class Solution40 { public List<List<Integer>> combinationSum2(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++){ if(i > start && nums[i] == nums[i-1]) continue; // skip duplicates tempList.add(nums[i]); backtrack(list, tempList, nums, remain - nums[i], i + 1); tempList.remove(tempList.size() - 1); } } } }
LeetCode 40 Combination Sum II(数组中求和等于target的所有组合)
标签:font get tin continue util public script add return
原文地址:http://www.cnblogs.com/zpfbuaa/p/6537725.html