From 8c3d8d60e1d008f8f6ac3c00a3dc5e9a0d72c802 Mon Sep 17 00:00:00 2001 From: Christian R <108245334+evaunit00@users.noreply.github.com> Date: Wed, 6 Jul 2022 14:46:11 -0700 Subject: [PATCH 1/4] Add files via upload Created and finished 39. Combination Sum and 17. Letter Combinations of a Phone Number --- 17-Letter-Combinations-of-a-Phone-Number.java | 37 +++++++++++++++++++ 39-Combination-Sum.java | 24 ++++++++++++ 2 files changed, 61 insertions(+) create mode 100644 17-Letter-Combinations-of-a-Phone-Number.java create mode 100644 39-Combination-Sum.java diff --git a/17-Letter-Combinations-of-a-Phone-Number.java b/17-Letter-Combinations-of-a-Phone-Number.java new file mode 100644 index 000000000..def598a17 --- /dev/null +++ b/17-Letter-Combinations-of-a-Phone-Number.java @@ -0,0 +1,37 @@ +import java.util.ArrayList; +import java.util.List; +import java.util.Map; + +class Solution { + private Map digitToChar = Map.of( + '2', "abc", '3', "def", '4', "ghi", '5', "jkl", + '6', "mno", '7', "pqrs", '8', "tuv", '9', "wxyz"); + + public List letterCombinations(String digits) { + + if (digits.length() == 0) { + return new ArrayList(); + } + + List ans = new ArrayList(); + String cur = ""; + backtrack(digits, ans, cur, 0); + return ans; + + } + + public void backtrack(String digits, List ans, String cur, int index) { + + if (cur.length() == digits.length()) { + ans.add(cur); + return; + } else if (index >= digits.length()) { + return; + } else { + String digit = digitToChar.get(digits.charAt(index)); + for (char c : digit.toCharArray()) { + backtrack(digits, ans, cur + c, index + 1); + } + } + } +} \ No newline at end of file diff --git a/39-Combination-Sum.java b/39-Combination-Sum.java new file mode 100644 index 000000000..e3b0f4730 --- /dev/null +++ b/39-Combination-Sum.java @@ -0,0 +1,24 @@ +class Solution { + public List> combinationSum(int[] candidates, int target) { + List> ans = new ArrayList>(); + List cur = new ArrayList(); + backtrack(candidates, target, ans, cur, 0); + return ans; + + } + + public void backtrack(int[] candidates, int target, List> ans, List cur, int index) { + if (target == 0) { + ans.add(new ArrayList(cur)); + } else if (target < 0 || index >= candidates.length) { + return; + } else { + cur.add(candidates[index]); + backtrack(candidates, target - candidates[index], ans, cur, index); + + cur.remove(cur.get(cur.size() - 1)); + backtrack(candidates, target, ans, cur, index + 1); + } + + } +} \ No newline at end of file From 886d7d4d7947240e0fc1d30825823189356b50d0 Mon Sep 17 00:00:00 2001 From: Christian R <108245334+evaunit00@users.noreply.github.com> Date: Wed, 6 Jul 2022 14:48:16 -0700 Subject: [PATCH 2/4] Delete 17-Letter-Combinations-of-a-Phone-Number.java Accidentally added it to python directory instead of java --- 17-Letter-Combinations-of-a-Phone-Number.java | 37 ------------------- 1 file changed, 37 deletions(-) delete mode 100644 17-Letter-Combinations-of-a-Phone-Number.java diff --git a/17-Letter-Combinations-of-a-Phone-Number.java b/17-Letter-Combinations-of-a-Phone-Number.java deleted file mode 100644 index def598a17..000000000 --- a/17-Letter-Combinations-of-a-Phone-Number.java +++ /dev/null @@ -1,37 +0,0 @@ -import java.util.ArrayList; -import java.util.List; -import java.util.Map; - -class Solution { - private Map digitToChar = Map.of( - '2', "abc", '3', "def", '4', "ghi", '5', "jkl", - '6', "mno", '7', "pqrs", '8', "tuv", '9', "wxyz"); - - public List letterCombinations(String digits) { - - if (digits.length() == 0) { - return new ArrayList(); - } - - List ans = new ArrayList(); - String cur = ""; - backtrack(digits, ans, cur, 0); - return ans; - - } - - public void backtrack(String digits, List ans, String cur, int index) { - - if (cur.length() == digits.length()) { - ans.add(cur); - return; - } else if (index >= digits.length()) { - return; - } else { - String digit = digitToChar.get(digits.charAt(index)); - for (char c : digit.toCharArray()) { - backtrack(digits, ans, cur + c, index + 1); - } - } - } -} \ No newline at end of file From 137ca09a5ca9084a346b1ff8190fd3826327cf35 Mon Sep 17 00:00:00 2001 From: Christian R <108245334+evaunit00@users.noreply.github.com> Date: Wed, 6 Jul 2022 14:49:08 -0700 Subject: [PATCH 3/4] Delete 39-Combination-Sum.java Accidentally added to python directory instead of Java --- 39-Combination-Sum.java | 24 ------------------------ 1 file changed, 24 deletions(-) delete mode 100644 39-Combination-Sum.java diff --git a/39-Combination-Sum.java b/39-Combination-Sum.java deleted file mode 100644 index e3b0f4730..000000000 --- a/39-Combination-Sum.java +++ /dev/null @@ -1,24 +0,0 @@ -class Solution { - public List> combinationSum(int[] candidates, int target) { - List> ans = new ArrayList>(); - List cur = new ArrayList(); - backtrack(candidates, target, ans, cur, 0); - return ans; - - } - - public void backtrack(int[] candidates, int target, List> ans, List cur, int index) { - if (target == 0) { - ans.add(new ArrayList(cur)); - } else if (target < 0 || index >= candidates.length) { - return; - } else { - cur.add(candidates[index]); - backtrack(candidates, target - candidates[index], ans, cur, index); - - cur.remove(cur.get(cur.size() - 1)); - backtrack(candidates, target, ans, cur, index + 1); - } - - } -} \ No newline at end of file From 9d8b53abcb0f6f5093db4060d745a666c6087a53 Mon Sep 17 00:00:00 2001 From: Christian R <108245334+evaunit00@users.noreply.github.com> Date: Wed, 6 Jul 2022 14:51:30 -0700 Subject: [PATCH 4/4] Added questions 17 and 39 in Java Added question 17. Letter Combinations of a Phone Number and 39. Combination Sum in Java --- ...Letter-Combinations-of-a-Phone-Number.java | 37 +++++++++++++++++++ java/39-Combination-Sum.java | 24 ++++++++++++ 2 files changed, 61 insertions(+) create mode 100644 java/17-Letter-Combinations-of-a-Phone-Number.java create mode 100644 java/39-Combination-Sum.java diff --git a/java/17-Letter-Combinations-of-a-Phone-Number.java b/java/17-Letter-Combinations-of-a-Phone-Number.java new file mode 100644 index 000000000..def598a17 --- /dev/null +++ b/java/17-Letter-Combinations-of-a-Phone-Number.java @@ -0,0 +1,37 @@ +import java.util.ArrayList; +import java.util.List; +import java.util.Map; + +class Solution { + private Map digitToChar = Map.of( + '2', "abc", '3', "def", '4', "ghi", '5', "jkl", + '6', "mno", '7', "pqrs", '8', "tuv", '9', "wxyz"); + + public List letterCombinations(String digits) { + + if (digits.length() == 0) { + return new ArrayList(); + } + + List ans = new ArrayList(); + String cur = ""; + backtrack(digits, ans, cur, 0); + return ans; + + } + + public void backtrack(String digits, List ans, String cur, int index) { + + if (cur.length() == digits.length()) { + ans.add(cur); + return; + } else if (index >= digits.length()) { + return; + } else { + String digit = digitToChar.get(digits.charAt(index)); + for (char c : digit.toCharArray()) { + backtrack(digits, ans, cur + c, index + 1); + } + } + } +} \ No newline at end of file diff --git a/java/39-Combination-Sum.java b/java/39-Combination-Sum.java new file mode 100644 index 000000000..e3b0f4730 --- /dev/null +++ b/java/39-Combination-Sum.java @@ -0,0 +1,24 @@ +class Solution { + public List> combinationSum(int[] candidates, int target) { + List> ans = new ArrayList>(); + List cur = new ArrayList(); + backtrack(candidates, target, ans, cur, 0); + return ans; + + } + + public void backtrack(int[] candidates, int target, List> ans, List cur, int index) { + if (target == 0) { + ans.add(new ArrayList(cur)); + } else if (target < 0 || index >= candidates.length) { + return; + } else { + cur.add(candidates[index]); + backtrack(candidates, target - candidates[index], ans, cur, index); + + cur.remove(cur.get(cur.size() - 1)); + backtrack(candidates, target, ans, cur, index + 1); + } + + } +} \ No newline at end of file