Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commite338d1d

Browse files
add a solution for 735
1 parentb82c7f8 commite338d1d

File tree

2 files changed

+49
-5
lines changed

2 files changed

+49
-5
lines changed

‎src/main/java/com/fishercoder/solutions/_735.java

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
packagecom.fishercoder.solutions;
22

3-
importjava.util.Stack;
3+
importjava.util.Deque;
4+
importjava.util.LinkedList;
45

56
publicclass_735 {
67
publicstaticclassSolution1 {
78
publicint[]asteroidCollision(int[]asteroids) {
8-
Stack<Integer>stack =newStack();
9+
Deque<Integer>stack =newLinkedList<>();
910
for (inti =0;i <asteroids.length;i++) {
1011
if (!stack.isEmpty() &&stack.peek() >0 &&asteroids[i] <0) {
1112
if (Math.abs(stack.peek()) <Math.abs(asteroids[i])) {
@@ -27,7 +28,7 @@ public int[] asteroidCollision(int[] asteroids) {
2728
returnresult;
2829
}
2930

30-
privatevoidcollide(Stack<Integer>stack) {
31+
privatevoidcollide(Deque<Integer>stack) {
3132
do {
3233
Integertop =stack.pop();
3334
if (!stack.isEmpty() &&stack.peek() *top <0) {
@@ -47,4 +48,43 @@ private void collide(Stack<Integer> stack) {
4748
}while (!stack.isEmpty());
4849
}
4950
}
51+
52+
publicstaticclassSolution2 {
53+
/**
54+
* My completely original solution on 11/5/2021.
55+
*/
56+
publicint[]asteroidCollision(int[]asteroids) {
57+
Deque<Integer>stack =newLinkedList<>();
58+
for (inta :asteroids) {
59+
if (a >0) {
60+
stack.addLast(a);
61+
}else {
62+
if (!stack.isEmpty() &&stack.peekLast() >0) {
63+
if (stack.peekLast() >Math.abs(a)) {
64+
continue;
65+
}elseif (stack.peekLast() ==Math.abs(a)) {
66+
stack.pollLast();
67+
}else {
68+
while (!stack.isEmpty() &&stack.peekLast() >0 &&stack.peekLast() <Math.abs(a)) {
69+
stack.pollLast();
70+
}
71+
if (!stack.isEmpty() &&stack.peekLast() >0 &&stack.peekLast() ==Math.abs(a)) {
72+
stack.pollLast();
73+
continue;
74+
}elseif (stack.isEmpty() ||stack.peekLast() <0) {
75+
stack.addLast(a);
76+
}
77+
}
78+
}else {
79+
stack.addLast(a);
80+
}
81+
}
82+
}
83+
int[]ans =newint[stack.size()];
84+
for (inti =stack.size() -1;i >=0;i--) {
85+
ans[i] =stack.pollLast();
86+
}
87+
returnans;
88+
}
89+
}
5090
}

‎src/test/java/com/fishercoder/_735Test.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,22 @@
88

99
publicclass_735Test {
1010
privatestatic_735.Solution1solution1;
11+
privatestatic_735.Solution2solution2;
1112
privatestaticint[]asteroids;
13+
privatestaticint[]expected;
1214

1315
@BeforeClass
1416
publicstaticvoidsetup() {
1517
solution1 =new_735.Solution1();
18+
solution2 =new_735.Solution2();
1619
}
1720

1821
@Test
1922
publicvoidtest1() {
2023
asteroids =newint[]{5,10, -5};
21-
asteroids =solution1.asteroidCollision(asteroids);
22-
assertArrayEquals(newint[]{5,10},asteroids);
24+
expected =newint[]{5,10};
25+
assertArrayEquals(expected,solution1.asteroidCollision(asteroids));
26+
assertArrayEquals(expected,solution2.asteroidCollision(asteroids));
2327
}
2428

2529
@Test

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp