Skip to content

Commit ad2abfc

Browse files
committed
Remove duplicate solution of SumOfTwo task
1 parent 09c3e84 commit ad2abfc

File tree

4 files changed

+83
-134
lines changed

4 files changed

+83
-134
lines changed

src/main/java/by/andd3dfx/numeric/SumOfTwoInArray.java

Lines changed: 0 additions & 62 deletions
This file was deleted.

src/main/java/by/andd3dfx/search/FindSumOfTwoInArray.java

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,28 @@
44
import java.util.Map;
55

66
/**
7-
* Find in array of integers two items which have sum equals to definite number.
7+
* <pre>
8+
* <a href="https://leetcode.com/problems/two-sum/">Task description</a>
9+
*
10+
* Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target.
11+
* You may assume that each input would have exactly one solution, and you may not use the same element twice.
12+
* You can return the answer in any order.
13+
*
14+
* Example 1:
15+
* Input: nums = [2,7,11,15], target = 9
16+
* Output: [0,1]
17+
* Explanation: Because nums[0] + nums[1] == 9, we return [0, 1].
18+
*
19+
* Example 2:
20+
* Input: nums = [3,2,4], target = 6
21+
* Output: [1,2]
22+
*
23+
* Example 3:
24+
* Input: nums = [3,3], target = 6
25+
* Output: [0,1]
26+
*
827
* Try to solve task using algorithm with O(n) complexity.
9-
* <p>
10-
* Есть массив целых чисел и число K.
11-
* Найти два таких (не обязательно различных) числа в массиве, сумма которых равна K, либо вывести, что такого нет.
28+
* </pre>
1229
*
1330
* @see <a href="https://youtu.be/CrQdpjsr26w">Video solution</a>
1431
*/

src/test/java/by/andd3dfx/numeric/SumOfTwoInArrayTest.java

Lines changed: 0 additions & 66 deletions
This file was deleted.

src/test/java/by/andd3dfx/search/FindSumOfTwoInArrayTest.java

Lines changed: 62 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
package by.andd3dfx.search;
22

3-
import org.junit.Test;
4-
53
import static org.assertj.core.api.Assertions.assertThat;
64

5+
import org.junit.Test;
6+
77
public class FindSumOfTwoInArrayTest {
88

99
@Test
@@ -25,4 +25,64 @@ public void find_On() {
2525
assertThat(FindSumOfTwoInArray.find_On(new int[]{1, 4, 7, 2, 90}, 19))
2626
.isNull();
2727
}
28+
29+
/// Cases from deleted implementation copy
30+
31+
@Test
32+
public void testFind_ON() {
33+
var result = FindSumOfTwoInArray.find_On(new int[]{1, 3, 5, 7, 9}, 12);
34+
35+
assertThat(result).satisfiesAnyOf(
36+
items -> assertThat(items).isEqualTo(new int[]{1, 4}),
37+
items -> assertThat(items).isEqualTo(new int[]{2, 3}),
38+
items -> assertThat(items).isEqualTo(new int[]{3, 2}),
39+
items -> assertThat(items).isEqualTo(new int[]{4, 1})
40+
);
41+
}
42+
43+
@Test
44+
public void testFind_ON2() {
45+
var result = FindSumOfTwoInArray.find_On2(new int[]{1, 3, 5, 7, 9}, 12);
46+
47+
assertThat(result).satisfiesAnyOf(
48+
items -> assertThat(items).isEqualTo(new int[]{1, 4}),
49+
items -> assertThat(items).isEqualTo(new int[]{2, 3}),
50+
items -> assertThat(items).isEqualTo(new int[]{3, 2}),
51+
items -> assertThat(items).isEqualTo(new int[]{4, 1})
52+
);
53+
}
54+
55+
@Test
56+
public void testFind_ONForSameNumbers() {
57+
var result = FindSumOfTwoInArray.find_On(new int[]{1, 1, 6, 6, 1}, 12);
58+
59+
assertThat(result).satisfiesAnyOf(
60+
items -> assertThat(items).isEqualTo(new int[]{2, 3}),
61+
items -> assertThat(items).isEqualTo(new int[]{3, 2})
62+
);
63+
}
64+
65+
@Test
66+
public void testFind_ON2ForSameNumbers() {
67+
var result = FindSumOfTwoInArray.find_On2(new int[]{1, 1, 6, 6, 1}, 12);
68+
69+
assertThat(result).satisfiesAnyOf(
70+
items -> assertThat(items).isEqualTo(new int[]{2, 3}),
71+
items -> assertThat(items).isEqualTo(new int[]{3, 2})
72+
);
73+
}
74+
75+
@Test
76+
public void testFind_ONForAbsentSolution() {
77+
var result = FindSumOfTwoInArray.find_On(new int[]{1, 1, 6, 3, 1}, 12);
78+
79+
assertThat(result).isNull();
80+
}
81+
82+
@Test
83+
public void testFind_ON2ForAbsentSolution() {
84+
var result = FindSumOfTwoInArray.find_On2(new int[]{1, 1, 6, 3, 1}, 12);
85+
86+
assertThat(result).isNull();
87+
}
2888
}

0 commit comments

Comments
 (0)