算法答案: public class Find { public static void main(String[] args) { int data[] = {1,2,3,4,5,6,8,10}; int k = 2; int m = 7; List<List<Integer>> data1 = findData(data, k, m); System.out.println(1); } private static List<List<Integer>> findData(int[] data, int k, int m) { List<List<Integer>> res = new ArrayList<>(); findRes(data, k, m, 0, new ArrayList<>(), res); return res; } private static void findRes(int[] data, int k, int m, int start, List<Integer> currentCom, List<List<Integer>> res){ // 判断不为0 if(k == 0 && m == 0){ res.add(new ArrayList<>(currentCom)); return; } if(k<=0 || m<=0 || start >= data.length){ return; } for(int i= start; i<data.length; i++){ currentCom.add(data[i]); findRes(data,k-1,m-data[i],i+1,currentCom,res); currentCom.remove(currentCom.size() -1); } } }
点赞 评论

相关推荐

牛客网
牛客企业服务