2024-09-15 15:38
南京大学 信息技术岗 0 点赞 评论 收藏
分享
0 点赞 评论 收藏
分享
進撃のNiko: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);
}
}

0 点赞 评论 收藏
分享

0 点赞 评论 收藏
分享

0 点赞 评论 收藏
分享
创作者周榜
更多
关注他的用户也关注了: