Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 4cb151c

Browse files
refactor 658
1 parent 1b7a1fd commit 4cb151c

File tree

2 files changed

+43
-41
lines changed

2 files changed

+43
-41
lines changed

src/main/java/com/fishercoder/solutions/_658.java

Lines changed: 38 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -25,50 +25,52 @@
2525
Absolute value of elements in the array and x will not exceed 104
2626
*/
2727
public class _658 {
28-
public List<Integer> findClosestElements(List<Integer> arr, int k, int x) {
29-
List<Integer> result = new ArrayList<>();
30-
int index = findInsertPosition(arr, x);
31-
int right = 0;
32-
if (arr.get(index) == x) {
33-
result.add(arr.get(index));
34-
right = index + 1;
35-
k--;
36-
} else {
37-
right = index;
38-
}
39-
int left = index - 1;
40-
while (k-- > 0) {
41-
if (left >= 0 && right < arr.size()) {
42-
if (x - arr.get(left) <= arr.get(right) - x) {
28+
public static class Solution1 {
29+
public List<Integer> findClosestElements(List<Integer> arr, int k, int x) {
30+
List<Integer> result = new ArrayList<>();
31+
int index = findInsertPosition(arr, x);
32+
int right = 0;
33+
if (arr.get(index) == x) {
34+
result.add(arr.get(index));
35+
right = index + 1;
36+
k--;
37+
} else {
38+
right = index;
39+
}
40+
int left = index - 1;
41+
while (k-- > 0) {
42+
if (left >= 0 && right < arr.size()) {
43+
if (x - arr.get(left) <= arr.get(right) - x) {
44+
result.add(0, arr.get(left--));
45+
} else {
46+
result.add(result.size(), arr.get(right++));
47+
}
48+
} else if (left >= 0) {
4349
result.add(0, arr.get(left--));
44-
} else {
50+
} else if (right < arr.size()) {
4551
result.add(result.size(), arr.get(right++));
4652
}
47-
} else if (left >= 0) {
48-
result.add(0, arr.get(left--));
49-
} else if (right < arr.size()) {
50-
result.add(result.size(), arr.get(right++));
5153
}
54+
return result;
5255
}
53-
return result;
54-
}
5556

56-
private int findInsertPosition(List<Integer> arr, int x) {
57-
if (arr == null || arr.size() == 0) {
58-
return 0;
59-
}
60-
int len = arr.size();
61-
for (int i = 0; i < len; i++) {
62-
if (arr.get(0) > x) {
57+
private int findInsertPosition(List<Integer> arr, int x) {
58+
if (arr == null || arr.size() == 0) {
6359
return 0;
64-
} else if (arr.get(len - 1) < x) {
65-
return len;
66-
} else if (arr.get(i) < x && arr.get(i + 1) > x) {
67-
return i + 1;
68-
} else if (arr.get(i) == x) {
69-
return i;
7060
}
61+
int len = arr.size();
62+
for (int i = 0; i < len; i++) {
63+
if (arr.get(0) > x) {
64+
return 0;
65+
} else if (arr.get(len - 1) < x) {
66+
return len;
67+
} else if (arr.get(i) < x && arr.get(i + 1) > x) {
68+
return i + 1;
69+
} else if (arr.get(i) == x) {
70+
return i;
71+
}
72+
}
73+
return len;
7174
}
72-
return len;
7375
}
7476
}

src/test/java/com/fishercoder/_658Test.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,15 @@
1111
import static junit.framework.Assert.assertEquals;
1212

1313
public class _658Test {
14-
private static _658 test;
14+
private static _658.Solution1 solution1;
1515
private static List<Integer> arr;
1616
private static List<Integer> expected;
1717
private static int k;
1818
private static int x;
1919

2020
@BeforeClass
2121
public static void setup() {
22-
test = new _658();
22+
solution1 = new _658.Solution1();
2323
}
2424

2525
@Test
@@ -28,7 +28,7 @@ public void test1() {
2828
x = 3;
2929
expected = new ArrayList<>(Arrays.asList(1, 2, 3, 4));
3030
arr = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5));
31-
assertEquals(expected, test.findClosestElements(arr, k, x));
31+
assertEquals(expected, solution1.findClosestElements(arr, k, x));
3232
}
3333

3434
@Test
@@ -37,7 +37,7 @@ public void test2() {
3737
x = -1;
3838
expected = new ArrayList<>(Arrays.asList(1, 2, 3, 4));
3939
arr = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5));
40-
assertEquals(expected, test.findClosestElements(arr, k, x));
40+
assertEquals(expected, solution1.findClosestElements(arr, k, x));
4141
}
4242

4343
@Test
@@ -46,6 +46,6 @@ public void test3() {
4646
x = 5;
4747
arr = new ArrayList<>(Arrays.asList(0, 0, 1, 2, 3, 3, 4, 7, 7, 8));
4848
expected = new ArrayList<>(Arrays.asList(3, 3, 4));
49-
assertEquals(expected, test.findClosestElements(arr, k, x));
49+
assertEquals(expected, solution1.findClosestElements(arr, k, x));
5050
}
5151
}

0 commit comments

Comments
 (0)