Skip to content

Commit 66fb6b9

Browse files
refactor 283
1 parent fdf6038 commit 66fb6b9

File tree

2 files changed

+68
-4
lines changed

2 files changed

+68
-4
lines changed

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,11 @@ public static class Solution2 {
3131
public void moveZeroes(int[] nums) {
3232
//this solutoin is the most optimal since it minimizes the number of operations
3333
//the idea is to swap the non-zero element to the first zero number position
34-
for (int i = 0, j = 0; i < nums.length && j < nums.length; i++) {
35-
if (nums[i] != 0) {
34+
for (int i = 0, j = 0; i < nums.length && j < nums.length; j++) {
35+
if (nums[j] != 0) {
3636
int temp = nums[i];
37-
nums[i] = nums[j];
37+
nums[i++] = nums[j];
3838
nums[j] = temp;
39-
j++;
4039
}
4140
}
4241
}

src/test/java/com/fishercoder/_283Test.java

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
import org.junit.BeforeClass;
66
import org.junit.Test;
77

8+
import static org.junit.Assert.assertArrayEquals;
9+
810
public class _283Test {
911
private static _283.Solution1 solution1;
1012
private static _283.Solution2 solution2;
@@ -38,4 +40,67 @@ public void test3() {
3840
solution3.moveZeroes(nums);
3941
CommonUtils.printArray(nums);
4042
}
43+
44+
@Test
45+
public void test4() {
46+
nums = new int[]{1, 0};
47+
solution1.moveZeroes(nums);
48+
CommonUtils.printArray(nums);
49+
}
50+
51+
@Test
52+
public void test5() {
53+
nums = new int[]{0, 1, 0, 3, 12};
54+
solution1.moveZeroes(nums);
55+
assertArrayEquals(new int[]{1, 3, 12, 0, 0}, nums);
56+
}
57+
58+
@Test
59+
public void test6() {
60+
nums = new int[]{1, 0, 0};
61+
solution1.moveZeroes(nums);
62+
assertArrayEquals(new int[]{1, 0, 0}, nums);
63+
}
64+
65+
@Test
66+
public void test7() {
67+
nums = new int[]{1, 0};
68+
solution2.moveZeroes(nums);
69+
CommonUtils.printArray(nums);
70+
}
71+
72+
@Test
73+
public void test8() {
74+
nums = new int[]{0, 1, 0, 3, 12};
75+
solution2.moveZeroes(nums);
76+
assertArrayEquals(new int[]{1, 3, 12, 0, 0}, nums);
77+
}
78+
79+
@Test
80+
public void test9() {
81+
nums = new int[]{1, 0, 0};
82+
solution2.moveZeroes(nums);
83+
assertArrayEquals(new int[]{1, 0, 0}, nums);
84+
}
85+
86+
@Test
87+
public void test10() {
88+
nums = new int[]{1, 0};
89+
solution3.moveZeroes(nums);
90+
CommonUtils.printArray(nums);
91+
}
92+
93+
@Test
94+
public void test11() {
95+
nums = new int[]{0, 1, 0, 3, 12};
96+
solution3.moveZeroes(nums);
97+
assertArrayEquals(new int[]{1, 3, 12, 0, 0}, nums);
98+
}
99+
100+
@Test
101+
public void test12() {
102+
nums = new int[]{1, 0, 0};
103+
solution3.moveZeroes(nums);
104+
assertArrayEquals(new int[]{1, 0, 0}, nums);
105+
}
41106
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy