BAEL-2391 Two pointer technique

* two pointer technique

* Corrected two sum

* Changed asserts in unit test
This commit is contained in:
Dhananjay Singh 2019-01-04 05:10:26 +05:30 committed by Emily Cheyne
parent 1695857b91
commit cd3f0fcb1f
7 changed files with 215 additions and 0 deletions

View File

@ -0,0 +1,16 @@
package com.baeldung.algorithms.twopointertechnique;
public class LinkedListFindMiddle {
public <T> T findMiddle(MyNode<T> head) {
MyNode<T> slowPointer = head;
MyNode<T> fastPointer = head;
while (fastPointer.next != null && fastPointer.next.next != null) {
fastPointer = fastPointer.next.next;
slowPointer = slowPointer.next;
}
return slowPointer.data;
}
}

View File

@ -0,0 +1,20 @@
package com.baeldung.algorithms.twopointertechnique;
public class MyNode<E> {
MyNode<E> next;
E data;
public MyNode(E value) {
data = value;
next = null;
}
public MyNode(E value, MyNode<E> n) {
data = value;
next = n;
}
public void setNext(MyNode<E> n) {
next = n;
}
}

View File

@ -0,0 +1,22 @@
package com.baeldung.algorithms.twopointertechnique;
public class RotateArray {
public void rotate(int[] input, int step) {
step %= input.length;
reverse(input, 0, input.length - 1);
reverse(input, 0, step - 1);
reverse(input, step, input.length - 1);
}
private void reverse(int[] input, int start, int end) {
while (start < end) {
int temp = input[start];
input[start] = input[end];
input[end] = temp;
start++;
end--;
}
}
}

View File

@ -0,0 +1,38 @@
package com.baeldung.algorithms.twopointertechnique;
public class TwoSum {
public boolean twoSum(int[] input, int targetValue) {
int pointerOne = 0;
int pointerTwo = input.length - 1;
while (pointerOne < pointerTwo) {
int sum = input[pointerOne] + input[pointerTwo];
if (sum == targetValue) {
return true;
} else if (sum < targetValue) {
pointerOne++;
} else {
pointerTwo--;
}
}
return false;
}
public boolean twoSumSlow(int[] input, int targetValue) {
for (int i = 0; i < input.length; i++) {
for (int j = 1; j < input.length; j++) {
if (input[i] + input[j] == targetValue) {
return true;
}
}
}
return false;
}
}

View File

@ -0,0 +1,37 @@
package com.baeldung.algorithms.twopointertechnique;
import static org.assertj.core.api.Assertions.assertThat;
import org.junit.Test;
public class LinkedListFindMiddleUnitTest {
LinkedListFindMiddle linkedListFindMiddle = new LinkedListFindMiddle();
@Test
public void givenLinkedListOfMyNodes_whenLinkedListFindMiddle_thenCorrect() {
MyNode<String> head = createNodesList(8);
assertThat(linkedListFindMiddle.findMiddle(head)).isEqualTo("4");
head = createNodesList(9);
assertThat(linkedListFindMiddle.findMiddle(head)).isEqualTo("5");
}
private static MyNode<String> createNodesList(int n) {
MyNode<String> head = new MyNode<String>("1");
MyNode<String> current = head;
for (int i = 2; i <= n; i++) {
MyNode<String> newNode = new MyNode<String>(String.valueOf(i));
current.setNext(newNode);
current = newNode;
}
return head;
}
}

View File

@ -0,0 +1,26 @@
package com.baeldung.algorithms.twopointertechnique;
import static org.assertj.core.api.Assertions.assertThat;
import org.junit.Test;
public class RotateArrayUnitTest {
private RotateArray rotateArray = new RotateArray();
private int[] inputArray;
private int step;
@Test
public void givenAnArrayOfIntegers_whenRotateKsteps_thenCorrect() {
inputArray = new int[] { 1, 2, 3, 4, 5, 6, 7 };
step = 4;
rotateArray.rotate(inputArray, step);
assertThat(inputArray).containsExactly(new int[] { 4, 5, 6, 7, 1, 2, 3 });
}
}

View File

@ -0,0 +1,56 @@
package com.baeldung.algorithms.twopointertechnique;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import org.junit.Test;
public class TwoSumUnitTest {
private TwoSum twoSum = new TwoSum();
private int[] sortedArray;
private int targetValue;
@Test
public void givenASortedArrayOfIntegers_whenTwoSumSlow_thenPairExists() {
sortedArray = new int[] { 0, 1, 2, 3, 4, 5, 5, 6, 7, 8, 9, 9 };
targetValue = 12;
assertTrue(twoSum.twoSumSlow(sortedArray, targetValue));
}
@Test
public void givenASortedArrayOfIntegers_whenTwoSumSlow_thenPairDoesNotExists() {
sortedArray = new int[] { 0, 1, 2, 3, 4, 5, 5, 6, 7, 8, 9, 9 };
targetValue = 20;
assertFalse(twoSum.twoSumSlow(sortedArray, targetValue));
}
@Test
public void givenASortedArrayOfIntegers_whenTwoSum_thenPairExists() {
sortedArray = new int[] { 0, 1, 2, 3, 4, 5, 5, 6, 7, 8, 9, 9 };
targetValue = 12;
assertTrue(twoSum.twoSum(sortedArray, targetValue));
}
@Test
public void givenASortedArrayOfIntegers_whenTwoSum_thenPairDoesNotExists() {
sortedArray = new int[] { 0, 1, 2, 3, 4, 5, 5, 6, 7, 8, 9, 9 };
targetValue = 20;
assertFalse(twoSum.twoSum(sortedArray, targetValue));
}
}