Created
April 7, 2016 19:33
-
-
Save lynas/2dd64e33a416398f346c5f9a0d96eb0c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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