Skip to content

Commit

Permalink
Added sum exercise
Browse files Browse the repository at this point in the history
  • Loading branch information
Rolf committed Sep 4, 2018
1 parent 24786ae commit 441c849
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/main/java/tudelft/caesarshift/CaesarShiftCipher.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package tudelft.partition.exercise.caesarshift;
package tudelft.caesarshift;

public class CaesarShiftCipher {

Expand Down
31 changes: 31 additions & 0 deletions src/main/java/tudelft/sum/TwoNumbersSum.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package tudelft.sum;

import java.util.ArrayList;
import java.util.Collections;

// Source: https://leetcode.com/problems/add-two-numbers/description/
class TwoNumbersSum {

public ArrayList<Integer> addTwoNumbers(ArrayList<Integer> first, ArrayList<Integer> second) {
Collections.reverse(first);
Collections.reverse(second);

int complement = 0;
ArrayList<Integer> result = new ArrayList<>();

for(int i = 0; i < Math.max(first.size(), second.size()); i++){
int firstVal = i < first.size() ? first.get(i) : 0;
int secondVal = i < second.size() ? second.get(i) : 0;
int total = firstVal + secondVal + complement;
complement = 0;
if (total >= 10){
complement = 1;
total -= 10;
}
result.add(i, total);
}

Collections.reverse(result);
return result;
}
}
5 changes: 5 additions & 0 deletions src/test/java/tudelft/sum/TwoNumbersSumTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package tudelft.sum;

public class TwoNumbersSumTest {

}

0 comments on commit 441c849

Please sign in to comment.