public static ArrayList<ArrayList<Integer>> combinationSum (int[] prices, int m) { // write code here ArrayList<ArrayList<Integer>> list = new ArrayList<>(); boolean[] flag = new boolean[prices.length]; dfs(list,prices,0,m,flag); return list; } public static void dfs(ArrayList<ArrayList<Integer>> list,int[] prices,int index,int sum,boolean[] flag){ if(sum==0){ ArrayList<Integer> l = new ArrayList<>(); for(int i=0;i<prices.length;i++){ if(flag[i]) l.add(prices[i]); } if(!list.contains(l)){ list.add(l); } } if(sum<0||index==prices.length) return; flag[index] = true; dfs(list,prices,index+1,sum-prices[index],flag); flag[index] = false; dfs(list,prices,index+1,sum,flag); } 自己造了几个测试用例都没问题,但a了0.....是因为list没排序的关系吗。。
点赞 6
牛客网
牛客企业服务