Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8575e8c
commit d7dd40c
Showing
3 changed files
with
62 additions
and
1 deletion.
There are no files selected for viewing
2 changes: 1 addition & 1 deletion
2
...com/shekhargulati/leetcode/Problem01.java → ...gulati/leetcode/algorithms/Problem01.java
This file contains 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
60 changes: 60 additions & 0 deletions
60
java8/src/main/java/com/shekhargulati/leetcode/algorithms/Problem02.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package com.shekhargulati.leetcode.algorithms; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
/** | ||
* You are given two linked lists representing two non-negative numbers. | ||
* The digits are stored in reverse order and each of their nodes contain a single digit. | ||
* Add the two numbers and return it as a linked list. | ||
* <p> | ||
* Input: (2 -> 4 -> 3) + (5 -> 6 -> 4) | ||
* Output: 7 -> 0 -> 8 | ||
*/ | ||
public class Problem02 { | ||
|
||
public static void main(String[] args) { | ||
/* | ||
1. Iterate over two lists together | ||
2. Add the two numbers at ith position | ||
3. If numbers add upto greater than equal to 10 then store the remainder at current position and store 1 as next | ||
4. When you are adding up next number add one | ||
*/ | ||
|
||
long start = System.currentTimeMillis(); | ||
List<Integer> first = Arrays.asList(2, 4, 3); | ||
List<Integer> second = Arrays.asList(5, 6, 4); | ||
|
||
List<Integer> result = new ArrayList<>(); | ||
|
||
int rem = 0; | ||
for (int i = 0; i < first.size(); i++) { | ||
int a = first.get(i); | ||
int b = second.get(i); | ||
int sum = a + b; | ||
if (a + b >= 10) { | ||
result.add(rem + sum % 10); | ||
rem = 1; | ||
} else { | ||
result.add(rem + sum); | ||
rem = 0; | ||
} | ||
} | ||
|
||
System.out.println(result); | ||
long end = System.currentTimeMillis(); | ||
System.out.println(String.format("Total time taken %d millis", (end - start))); | ||
|
||
} | ||
|
||
private class ListNode { | ||
int val; | ||
ListNode next; | ||
|
||
public ListNode(int val) { | ||
this.val = val; | ||
} | ||
} | ||
|
||
} |
1 change: 1 addition & 0 deletions
1
java8/src/test/java/com/shekhargulati/leetcode/Problem01Test.java
This file contains 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