Skip to content

Instantly share code, notes, and snippets.

@lynas
Created April 7, 2016 19:33
Show Gist options
  • Save lynas/2dd64e33a416398f346c5f9a0d96eb0c to your computer and use it in GitHub Desktop.
Save lynas/2dd64e33a416398f346c5f9a0d96eb0c to your computer and use it in GitHub Desktop.
private int fold(List<Integer> input) {
Integer res;
if (input.size() == 1) {
return input.get(0);
}else {
try {
res = input.get(0);
for (int i = 1; i < input.size(); i++) {
input.set(i, input.get(i) + res);
}
input.remove(0);
return fold(input);
} catch (UnsupportedOperationException ex) {
input = new ArrayList<>(input);
input.remove(0);
return fold(input);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment