From 54175da93bb20f94093f7b1a2ff8f4a44d3cf306 Mon Sep 17 00:00:00 2001 From: lxz <2425220864@qq.com> Date: Sat, 31 Aug 2024 14:33:55 +0800 Subject: [PATCH] refactor: change List to LinkedList and remove 'n < 0' --- .../com/thealgorithms/backtracking/ArrayCombination.java | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/thealgorithms/backtracking/ArrayCombination.java b/src/main/java/com/thealgorithms/backtracking/ArrayCombination.java index a064decc0eb7..bb5a537d20b9 100644 --- a/src/main/java/com/thealgorithms/backtracking/ArrayCombination.java +++ b/src/main/java/com/thealgorithms/backtracking/ArrayCombination.java @@ -1,6 +1,7 @@ package com.thealgorithms.backtracking; import java.util.ArrayList; +import java.util.LinkedList; import java.util.List; /** @@ -18,16 +19,16 @@ private ArrayCombination() { * @return A list of all combinations of length k. */ public static List> combination(int n, int k) { - if (n < 0 || k < 0 || k > n) { + if (k < 0 || k > n) { throw new IllegalArgumentException("Wrong input."); } List> combinations = new ArrayList<>(); - combine(combinations, new ArrayList<>(), 0, n, k); + combine(combinations, new LinkedList<>(), 0, n, k); return combinations; } - private static void combine(List> combinations, List current, int start, int n, int k) { + private static void combine(List> combinations, LinkedList current, int start, int n, int k) { if (current.size() == k) { // Base case: combination found combinations.add(new ArrayList<>(current)); // Copy to avoid modification return;