From bce33ad79ece5c69ca6d456afdea9ff3441e6730 Mon Sep 17 00:00:00 2001 From: Himanshu Singh <32442053+hemusqwerty@users.noreply.github.com> Date: Thu, 20 Jun 2019 12:01:19 +0530 Subject: [PATCH 1/2] 077 Combination.java Added Solution for 077 Combinations --- leetcode/combination.java | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 leetcode/combination.java diff --git a/leetcode/combination.java b/leetcode/combination.java new file mode 100644 index 0000000..6468d32 --- /dev/null +++ b/leetcode/combination.java @@ -0,0 +1,31 @@ +import java.util.Scanner; +class Solution { + + public static List> combine(int n, int k) { + List> result = new ArrayList<>(); + exploreCombinations(n, k, 1, new ArrayList(), result); + return result; + } + + private static void exploreCombinations(int n, int k, int offset, + List snippet, + List> result) { + if (snippet.size() == k) { + result.add(new ArrayList<>(snippet)); + return; + } + final int spaceLeftInSnippet = k - snippet.size(); + for (int i = offset; i <= n && spaceLeftInSnippet <= n - i + 1 ; ++i) { + snippet.add(i); + exploreCombinations(n, k, i + 1, snippet , result); + snippet.remove(snippet.size() - 1); + } + } + + public static void main(String[] args){ + Scanner sc=new Scanner(System.in); + int n=sc.nextInt(); + int k=sc.nextInt(); + combine(n,k); + } +} From ab42009530df58e7af24522249eebe8faacdb795 Mon Sep 17 00:00:00 2001 From: Himanshu Singh <32442053+hemusqwerty@users.noreply.github.com> Date: Thu, 20 Jun 2019 12:23:53 +0530 Subject: [PATCH 2/2] Update and rename combination.java to 077-Combination.java --- leetcode/{combination.java => 077-Combination.java} | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename leetcode/{combination.java => 077-Combination.java} (97%) diff --git a/leetcode/combination.java b/leetcode/077-Combination.java similarity index 97% rename from leetcode/combination.java rename to leetcode/077-Combination.java index 6468d32..8330036 100644 --- a/leetcode/combination.java +++ b/leetcode/077-Combination.java @@ -1,4 +1,4 @@ -import java.util.Scanner; +import java.util.*; class Solution { public static List> combine(int n, int k) {