Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 66 additions & 0 deletions CombinationSum.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import java.util.ArrayList;
import java.util.List;
class CombinationSum {
List<List<Integer>> result;
public List<List<Integer>> 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<Integer> path)
private void combineSumHelper(int[] candidates, int target, int pivot, List<Integer> 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);
}
}
}
57 changes: 57 additions & 0 deletions ExpressionAddOperators.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import java.util.ArrayList;
import java.util.List;
class ExpressionAddOperators {
List<String> result;
public List<String> 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);
}
}
}
}