K个反转链表是头条高频题了,刷牛客的不应该陌生才是。第二题经典回溯枚举就行,简单写了下,IDE可以跑。 public class SumSequence { private static ArrayList<ArrayList<Integer>> res = new ArrayList<>(); public static ArrayList<ArrayList<Integer>> sumSeq(int m) { backtracking(1, 0, m, new ArrayList<Integer>()); return res; } private static void backtracking(int start, int sum, int max, ArrayList<Integer> temp) { if (sum > max) return; if (sum == max) res.add(new ArrayList<>(temp)); for (int j = start; j <= max; j++) { if (!temp.contains(j)) { temp.add(j); backtracking(j, sum+j, max, temp); temp.remove(temp.size()-1); } } } public static void main(String[] args) { sumSeq(6); } }