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

Commit 0b681a0

Browse files
authored
735 solved. (#67)
1 parent 8333945 commit 0b681a0

File tree

4 files changed

+78
-0
lines changed

4 files changed

+78
-0
lines changed

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ continually updating 😃.
4343

4444
### Stack
4545
* [155. Min Stack](src/0155_min_stack/min_stack.go)
46+
* [735. Asteroid Collision](src/0735_asteroid_collision/ac.go)
4647

4748
### String
4849
* [3. Longest Substring Without Repeating Characters](./src/0003_longest_substring_without_repeating_characters/longest_substring_without_repeating_characters.go)   *`sliding window;`*  *`hash table`*

‎src/0735_asteroid_collision/ac.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
735. Asteroid Collision
3+
https://leetcode.com/problems/asteroid-collision/
4+
5+
We are given an array asteroids of integers representing asteroids in a row.
6+
7+
For each asteroid, the absolute value represents its size,
8+
and the sign represents its direction (positive meaning right, negative meaning left).
9+
Each asteroid moves at the same speed.
10+
11+
Find out the state of the asteroids after all collisions.
12+
If two asteroids meet, the smaller one will explode. If both are the same size, both will explode.
13+
Two asteroids moving in the same direction will never meet.
14+
15+
Note:
16+
The length of asteroids will be at most 10000.
17+
Each asteroid will be a non-zero integer in the range [-1000, 1000]..
18+
*/
19+
// time: 2019年01月14日
20+
21+
package ac
22+
23+
// stack
24+
// time complexity: O(N), where NN is the number of asteroids. Our stack pushes and pops each asteroid at most once.
25+
// space complexity: O(N), the size of stack.
26+
func asteroidCollision(asteroids []int) []int {
27+
stack := make([]int, 0)
28+
29+
for _, asteroid := range asteroids {
30+
flag := true
31+
for len(stack) > 0 && asteroid < 0 && stack[len(stack)-1] > 0 {
32+
if stack[len(stack)-1] == -asteroid {
33+
stack = stack[:len(stack)-1]
34+
} else if stack[len(stack)-1] < -asteroid {
35+
stack = stack[:len(stack)-1]
36+
continue
37+
}
38+
flag = false
39+
break
40+
}
41+
if flag {
42+
stack = append(stack, asteroid)
43+
}
44+
}
45+
return stack
46+
}

‎src/0735_asteroid_collision/ac_test.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package ac
2+
3+
import (
4+
"reflect"
5+
"testing"
6+
)
7+
8+
func TestAsteroidCollision(t *testing.T) {
9+
testCases := [][]int{
10+
{5, 10, -5},
11+
{-2, 2, -1, -2},
12+
{-2, -1, 1, 2},
13+
{8, -8},
14+
{10, 2, -5},
15+
}
16+
17+
expected := [][]int{
18+
{5, 10},
19+
{-2},
20+
{-2, -1, 1, 2},
21+
{},
22+
{10},
23+
}
24+
25+
for index, asteroids := range testCases {
26+
if res := asteroidCollision(asteroids); !reflect.DeepEqual(res, expected[index]) {
27+
t.Errorf("expected %v, got %v", expected[index], res)
28+
}
29+
}
30+
}

‎src/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,5 +102,6 @@
102102
|0713|[713. Subarray Product Less Than K](0713_subarray_product_less_than_k/spltk.go)|Medium|*`sliding window`*|
103103
|0717|[717. 1-bit and 2-bit Characters](0717_1_bit_and_2_bit_characters/1bitand2bitc.go)|Easy||
104104
|0728|[Self Dividing Numbers](./0728_self_dividing_numbers/self_dividing_numbers.go)|Easy||
105+
|0735|[735. Asteroid Collision](0735_asteroid_collision/ac.go)|Medium|*`stack`*|
105106
|0747|[Largest Number At Least Twice of Others](./0747_largest_number_at_least_twice_of_others/largest_number_at_least_twice_of_others.go)|Easy||
106107
|0872|[872. Leaf-Similar Trees](0872_leaf_similar_trees/leaf_similar_trees.go)|Easy|*`binary tree`*|

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /