diff --git a/CombinationSum.java b/CombinationSum.java new file mode 100644 index 00000000..0ba01dc4 --- /dev/null +++ b/CombinationSum.java @@ -0,0 +1,66 @@ +import java.util.ArrayList; +import java.util.List; +class CombinationSum { + List> result; + public List> combinationSum(int[] candidates, int target) { + // recursion - with backtracking + result = new ArrayList<>(); + combineSumHelper(candidates, target, 0, new ArrayList<>()); + return result; + } + + // private void combineSumHelper(int[] candidates, int target, int idx, List path) + private void combineSumHelper(int[] candidates, int target, int pivot, List path) + { + // // a. recurse + backtrack + // // base + // if(target == 0) + // { + // result.add(new ArrayList<>(path)); + // return; + // } + + // if(target < 0 || idx == candidates.length) + // { + // return; + // } + + // // logic + // // not choose + // // no action here for not choose + // //recurse + // combineSumHelper(candidates, target, idx+1, path); + + // // choose + // // action + // path.add(candidates[idx]); + // // recurse + // combineSumHelper(candidates, target - candidates[idx], idx, path); + + // //backtrack + // path.remove(path.size() - 1); + + // b. for loop + recurse + backtrack + // base case + if(target == 0) + { + result.add(new ArrayList<>(path)); + return; + } + if(target < 0 || pivot == candidates.length) + { + return; + } + + // logic + // action + for(int i = pivot; i < candidates.length; i++) + { + // recurse + path.add(candidates[i]); + combineSumHelper(candidates, target - candidates[i], i, path); + // backtrack + path.remove(path.size() - 1); + } + } +} \ No newline at end of file diff --git a/ExpressionAddOperators.java b/ExpressionAddOperators.java new file mode 100644 index 00000000..7ab51089 --- /dev/null +++ b/ExpressionAddOperators.java @@ -0,0 +1,57 @@ +import java.util.ArrayList; +import java.util.List; +class ExpressionAddOperators { + List result; + public List addOperators(String num, int target) { + result = new ArrayList<>(); + addOperatorHelper(num, target, 0, new StringBuilder(), 0l, 0l); + return result; + } + + private void addOperatorHelper(String num, int target, int pivot, StringBuilder path, long cal, long tail) + { + // base + // we have to iclude all digits + if(pivot == num.length()) + { + if(cal == target) + { + result.add(path.toString()); + } + return; + } + + // logic + // action + for(int i = pivot; i < num.length(); i++) + { + // handle leading 0 cases, and not include them, we dont need '05' + if(pivot != i && num.charAt(pivot) == '0') continue; + long curr = Long.parseLong(num.substring(pivot, i+1)); // pivot to i, include i - substring + int le = path.length(); + // recurse + if(0 == pivot) // no need of operators here - for 1, 12, 123, 1234 cases in first level + { + path.append(curr); + addOperatorHelper(num, target, i+1, path, curr, curr); + path.setLength(le); + } + // backtrack - the operations + else + { + // + + path.append("+").append(curr); + addOperatorHelper(num, target, i+1, path, cal+curr, curr); + path.setLength(le); + // - + path.append("-").append(curr); + addOperatorHelper(num, target, i+1, path, cal-curr, -curr); + path.setLength(le); + // * + path.append("*").append(curr); + addOperatorHelper(num, target, i+1, path, cal - tail + tail * curr, tail*curr); + path.setLength(le); + } + } + } +} \ No newline at end of file