From 0a1fe93f38a534ac63b839a7cb9d410db18d05d7 Mon Sep 17 00:00:00 2001
From: yanglbme <szuyanglb@outlook.com>
Date: Tue, 19 Sep 2023 14:57:16 +0800
Subject: [PATCH] feat: add solutions to lc/lcof2 problems

* lc No.0735 & lcof2 No.037.Asteroid Collision
---
 .../README.md"                                | 150 ++++++++-----
 .../Solution.cpp"                             |  42 ++--
 .../Solution.go"                              |  23 +-
 .../Solution.java"                            |  40 ++--
 .../Solution.py"                              |  28 +--
 .../Solution.rs"                              |  21 ++
 .../Solution.ts"                              |  18 ++
 .../0735.Asteroid Collision/README.md         | 204 ++++++++----------
 .../0735.Asteroid Collision/README_EN.md      | 202 ++++++++---------
 .../0735.Asteroid Collision/Solution.cpp      |  42 ++--
 .../0735.Asteroid Collision/Solution.go       |  23 +-
 .../0735.Asteroid Collision/Solution.java     |  40 ++--
 .../0735.Asteroid Collision/Solution.py       |  28 +--
 .../0735.Asteroid Collision/Solution.rs       |  67 ++----
 .../0735.Asteroid Collision/Solution.ts       |  22 +-
 15 files changed, 481 insertions(+), 469 deletions(-)
 create mode 100644 "lcof2/\345\211\221\346\214\207 Offer II 037. \345\260\217\350\241\214\346\230\237\347\242\260\346\222\236/Solution.rs"
 create mode 100644 "lcof2/\345\211\221\346\214\207 Offer II 037. \345\260\217\350\241\214\346\230\237\347\242\260\346\222\236/Solution.ts"

diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 037. \345\260\217\350\241\214\346\230\237\347\242\260\346\222\236/README.md" "b/lcof2/\345\211\221\346\214\207 Offer II 037. \345\260\217\350\241\214\346\230\237\347\242\260\346\222\236/README.md"
index bbff5a8263163..c343705b2aca2 100644
--- "a/lcof2/\345\211\221\346\214\207 Offer II 037. \345\260\217\350\241\214\346\230\237\347\242\260\346\222\236/README.md"	
+++ "b/lcof2/\345\211\221\346\214\207 Offer II 037. \345\260\217\350\241\214\346\230\237\347\242\260\346\222\236/README.md"	
@@ -58,12 +58,16 @@
 
 <!-- 这里可写通用的实现逻辑 -->
 
-可以类比成左右括号匹配:
+**方法一:栈**
 
--   向右移动的小行星(左括号):不会引发碰撞,直接入栈
--   向左移动的小行星(右括号):可能会和之前向右移动的小行星发生碰撞,特殊处理
+我们从左到右遍历每个小行星 $x$,由于每个小行星可能与之前的多个小行星发生碰撞,考虑用栈来存储。
 
-因为答案需要碰撞后剩下的所有小行星,相当于栈里最后剩下的元素,所以可以直接用数组表示栈
+-   对于当前小行星,如果 $x \gt 0$,那么它一定不会跟前面的小行星发生碰撞,我们可以直接将 $x$ 入栈。
+-   否则,如果栈不为空并且栈顶元素大于 $0$,且栈顶元素小于 $-x$,那么栈顶元素对应的小行星会发生爆炸,我们循环将栈顶元素出栈,直到不满足条件。此时如果栈顶元素等于 $-x$,那么两个小行星会发生爆炸,只需要将栈顶元素出栈即可;如果栈为空,或者栈顶元素小于 $0$,那么当前小行星不会发生碰撞,我们将 $x$ 入栈。
+
+最后我们返回栈中的元素即为答案。
+
+时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 是数组 $asteroids$ 的长度。
 
 <!-- tabs:start -->
 
@@ -74,18 +78,18 @@
 ```python
 class Solution:
     def asteroidCollision(self, asteroids: List[int]) -> List[int]:
-        ans = []
-        for a in asteroids:
-            if a > 0:
-                ans.append(a)
+        stk = []
+        for x in asteroids:
+            if x > 0:
+                stk.append(x)
             else:
-                while len(ans) > 0 and ans[-1] > 0 and ans[-1] < -a:
-                    ans.pop()
-                if len(ans) > 0 and ans[-1] == -a:
-                    ans.pop()
-                elif len(ans) == 0 or ans[-1] < -a:
-                    ans.append(a)
-        return ans
+                while stk and stk[-1] > 0 and stk[-1] < -x:
+                    stk.pop()
+                if stk and stk[-1] == -x:
+                    stk.pop()
+                elif not stk or stk[-1] < 0:
+                    stk.append(x)
+        return stk
 ```
 
 ### **Java**
@@ -95,22 +99,22 @@ class Solution:
 ```java
 class Solution {
     public int[] asteroidCollision(int[] asteroids) {
-        Deque<Integer> d = new ArrayDeque<>();
-        for (int a : asteroids) {
-            if (a > 0) {
-                d.offerLast(a);
+        Deque<Integer> stk = new ArrayDeque<>();
+        for (int x : asteroids) {
+            if (x > 0) {
+                stk.offerLast(x);
             } else {
-                while (!d.isEmpty() && d.peekLast() > 0 && d.peekLast() < -a) {
-                    d.pollLast();
+                while (!stk.isEmpty() && stk.peekLast() > 0 && stk.peekLast() < -x) {
+                    stk.pollLast();
                 }
-                if (!d.isEmpty() && d.peekLast() == -a) {
-                    d.pollLast();
-                } else if (d.isEmpty() || d.peekLast() < -a) {
-                    d.offerLast(a);
+                if (!stk.isEmpty() && stk.peekLast() == -x) {
+                    stk.pollLast();
+                } else if (stk.isEmpty() || stk.peekLast() < 0) {
+                    stk.offerLast(x);
                 }
             }
         }
-        return d.stream().mapToInt(Integer::valueOf).toArray();
+        return stk.stream().mapToInt(Integer::valueOf).toArray();
     }
 }
 ```
@@ -121,22 +125,22 @@ class Solution {
 class Solution {
 public:
     vector<int> asteroidCollision(vector<int>& asteroids) {
-        vector<int> ans;
-        for (int a : asteroids) {
-            if (a > 0) {
-                ans.push_back(a);
+        vector<int> stk;
+        for (int x : asteroids) {
+            if (x > 0) {
+                stk.push_back(x);
             } else {
-                while (!ans.empty() && ans.back() > 0 && ans.back() < -a) {
-                    ans.pop_back();
+                while (stk.size() && stk.back() > 0 && stk.back() < -x) {
+                    stk.pop_back();
                 }
-                if (!ans.empty() && ans.back() == -a) {
-                    ans.pop_back();
-                } else if (ans.empty() || ans.back() < -a) {
-                    ans.push_back(a);
+                if (stk.size() && stk.back() == -x) {
+                    stk.pop_back();
+                } else if (stk.empty() || stk.back() < 0) {
+                    stk.push_back(x);
                 }
             }
         }
-        return ans;
+        return stk;
     }
 };
 ```
@@ -144,23 +148,71 @@ public:
 ### **Go**
 
 ```go
-func asteroidCollision(asteroids []int) []int {
-	var ans []int
-	for _, a := range asteroids {
-		if a > 0 {
-			ans = append(ans, a)
+func asteroidCollision(asteroids []int) (stk []int) {
+	for _, x := range asteroids {
+		if x > 0 {
+			stk = append(stk, x)
 		} else {
-			for len(ans) > 0 && ans[len(ans)-1] > 0 && ans[len(ans)-1] < -a {
-				ans = ans[:len(ans)-1]
+			for len(stk) > 0 && stk[len(stk)-1] > 0 && stk[len(stk)-1] < -x {
+				stk = stk[:len(stk)-1]
 			}
-			if len(ans) > 0 && ans[len(ans)-1] == -a {
-				ans = ans[:len(ans)-1]
-			} else if len(ans) == 0 || ans[len(ans)-1] < -a {
-				ans = append(ans, a)
+			if len(stk) > 0 && stk[len(stk)-1] == -x {
+				stk = stk[:len(stk)-1]
+			} else if len(stk) == 0 || stk[len(stk)-1] < 0 {
+				stk = append(stk, x)
 			}
 		}
 	}
-	return ans
+	return
+}
+```
+
+### **TypeScript**
+
+```ts
+function asteroidCollision(asteroids: number[]): number[] {
+    const stk: number[] = [];
+    for (const x of asteroids) {
+        if (x > 0) {
+            stk.push(x);
+        } else {
+            while (stk.length && stk.at(-1) > 0 && stk.at(-1) < -x) {
+                stk.pop();
+            }
+            if (stk.length && stk.at(-1) === -x) {
+                stk.pop();
+            } else if (!stk.length || stk.at(-1) < 0) {
+                stk.push(x);
+            }
+        }
+    }
+    return stk;
+}
+```
+
+### **Rust**
+
+```rust
+impl Solution {
+    #[allow(dead_code)]
+    pub fn asteroid_collision(asteroids: Vec<i32>) -> Vec<i32> {
+        let mut stk = Vec::new();
+        for &x in &asteroids {
+            if x > 0 {
+                stk.push(x);
+            } else {
+                while !stk.is_empty() && *stk.last().unwrap() > 0 && *stk.last().unwrap() < -x {
+                    stk.pop();
+                }
+                if !stk.is_empty() && *stk.last().unwrap() == -x {
+                    stk.pop();
+                } else if stk.is_empty() || *stk.last().unwrap() < 0 {
+                    stk.push(x);
+                }
+            }
+        }
+        stk
+    }
 }
 ```
 
diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 037. \345\260\217\350\241\214\346\230\237\347\242\260\346\222\236/Solution.cpp" "b/lcof2/\345\211\221\346\214\207 Offer II 037. \345\260\217\350\241\214\346\230\237\347\242\260\346\222\236/Solution.cpp"
index e4d96bd3ed708..768f03fc6a28c 100644
--- "a/lcof2/\345\211\221\346\214\207 Offer II 037. \345\260\217\350\241\214\346\230\237\347\242\260\346\222\236/Solution.cpp"	
+++ "b/lcof2/\345\211\221\346\214\207 Offer II 037. \345\260\217\350\241\214\346\230\237\347\242\260\346\222\236/Solution.cpp"	
@@ -1,21 +1,21 @@
-class Solution {
-public:
-    vector<int> asteroidCollision(vector<int>& asteroids) {
-        vector<int> ans;
-        for (int a : asteroids) {
-            if (a > 0) {
-                ans.push_back(a);
-            } else {
-                while (!ans.empty() && ans.back() > 0 && ans.back() < -a) {
-                    ans.pop_back();
-                }
-                if (!ans.empty() && ans.back() == -a) {
-                    ans.pop_back();
-                } else if (ans.empty() || ans.back() < -a) {
-                    ans.push_back(a);
-                }
-            }
-        }
-        return ans;
-    }
-};
+class Solution {
+public:
+    vector<int> asteroidCollision(vector<int>& asteroids) {
+        vector<int> stk;
+        for (int x : asteroids) {
+            if (x > 0) {
+                stk.push_back(x);
+            } else {
+                while (stk.size() && stk.back() > 0 && stk.back() < -x) {
+                    stk.pop_back();
+                }
+                if (stk.size() && stk.back() == -x) {
+                    stk.pop_back();
+                } else if (stk.empty() || stk.back() < 0) {
+                    stk.push_back(x);
+                }
+            }
+        }
+        return stk;
+    }
+};
\ No newline at end of file
diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 037. \345\260\217\350\241\214\346\230\237\347\242\260\346\222\236/Solution.go" "b/lcof2/\345\211\221\346\214\207 Offer II 037. \345\260\217\350\241\214\346\230\237\347\242\260\346\222\236/Solution.go"
index ca38f4feb3624..26d1420d98ba8 100644
--- "a/lcof2/\345\211\221\346\214\207 Offer II 037. \345\260\217\350\241\214\346\230\237\347\242\260\346\222\236/Solution.go"	
+++ "b/lcof2/\345\211\221\346\214\207 Offer II 037. \345\260\217\350\241\214\346\230\237\347\242\260\346\222\236/Solution.go"	
@@ -1,18 +1,17 @@
-func asteroidCollision(asteroids []int) []int {
-	var ans []int
-	for _, a := range asteroids {
-		if a > 0 {
-			ans = append(ans, a)
+func asteroidCollision(asteroids []int) (stk []int) {
+	for _, x := range asteroids {
+		if x > 0 {
+			stk = append(stk, x)
 		} else {
-			for len(ans) > 0 && ans[len(ans)-1] > 0 && ans[len(ans)-1] < -a {
-				ans = ans[:len(ans)-1]
+			for len(stk) > 0 && stk[len(stk)-1] > 0 && stk[len(stk)-1] < -x {
+				stk = stk[:len(stk)-1]
 			}
-			if len(ans) > 0 && ans[len(ans)-1] == -a {
-				ans = ans[:len(ans)-1]
-			} else if len(ans) == 0 || ans[len(ans)-1] < -a {
-				ans = append(ans, a)
+			if len(stk) > 0 && stk[len(stk)-1] == -x {
+				stk = stk[:len(stk)-1]
+			} else if len(stk) == 0 || stk[len(stk)-1] < 0 {
+				stk = append(stk, x)
 			}
 		}
 	}
-	return ans
+	return
 }
\ No newline at end of file
diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 037. \345\260\217\350\241\214\346\230\237\347\242\260\346\222\236/Solution.java" "b/lcof2/\345\211\221\346\214\207 Offer II 037. \345\260\217\350\241\214\346\230\237\347\242\260\346\222\236/Solution.java"
index 787b2e17f051b..2482fab648a88 100644
--- "a/lcof2/\345\211\221\346\214\207 Offer II 037. \345\260\217\350\241\214\346\230\237\347\242\260\346\222\236/Solution.java"	
+++ "b/lcof2/\345\211\221\346\214\207 Offer II 037. \345\260\217\350\241\214\346\230\237\347\242\260\346\222\236/Solution.java"	
@@ -1,20 +1,20 @@
-class Solution {
-    public int[] asteroidCollision(int[] asteroids) {
-        Deque<Integer> d = new ArrayDeque<>();
-        for (int a : asteroids) {
-            if (a > 0) {
-                d.offerLast(a);
-            } else {
-                while (!d.isEmpty() && d.peekLast() > 0 && d.peekLast() < -a) {
-                    d.pollLast();
-                }
-                if (!d.isEmpty() && d.peekLast() == -a) {
-                    d.pollLast();
-                } else if (d.isEmpty() || d.peekLast() < -a) {
-                    d.offerLast(a);
-                }
-            }
-        }
-        return d.stream().mapToInt(Integer::valueOf).toArray();
-    }
-}
+class Solution {
+    public int[] asteroidCollision(int[] asteroids) {
+        Deque<Integer> stk = new ArrayDeque<>();
+        for (int x : asteroids) {
+            if (x > 0) {
+                stk.offerLast(x);
+            } else {
+                while (!stk.isEmpty() && stk.peekLast() > 0 && stk.peekLast() < -x) {
+                    stk.pollLast();
+                }
+                if (!stk.isEmpty() && stk.peekLast() == -x) {
+                    stk.pollLast();
+                } else if (stk.isEmpty() || stk.peekLast() < 0) {
+                    stk.offerLast(x);
+                }
+            }
+        }
+        return stk.stream().mapToInt(Integer::valueOf).toArray();
+    }
+}
\ No newline at end of file
diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 037. \345\260\217\350\241\214\346\230\237\347\242\260\346\222\236/Solution.py" "b/lcof2/\345\211\221\346\214\207 Offer II 037. \345\260\217\350\241\214\346\230\237\347\242\260\346\222\236/Solution.py"
index baf64ead7e268..01660c5098ec8 100644
--- "a/lcof2/\345\211\221\346\214\207 Offer II 037. \345\260\217\350\241\214\346\230\237\347\242\260\346\222\236/Solution.py"	
+++ "b/lcof2/\345\211\221\346\214\207 Offer II 037. \345\260\217\350\241\214\346\230\237\347\242\260\346\222\236/Solution.py"	
@@ -1,14 +1,14 @@
-class Solution:
-    def asteroidCollision(self, asteroids: List[int]) -> List[int]:
-        ans = []
-        for a in asteroids:
-            if a > 0:
-                ans.append(a)
-            else:
-                while len(ans) > 0 and ans[-1] > 0 and ans[-1] < -a:
-                    ans.pop()
-                if len(ans) > 0 and ans[-1] == -a:
-                    ans.pop()
-                elif len(ans) == 0 or ans[-1] < -a:
-                    ans.append(a)
-        return ans
+class Solution:
+    def asteroidCollision(self, asteroids: List[int]) -> List[int]:
+        stk = []
+        for x in asteroids:
+            if x > 0:
+                stk.append(x)
+            else:
+                while stk and stk[-1] > 0 and stk[-1] < -x:
+                    stk.pop()
+                if stk and stk[-1] == -x:
+                    stk.pop()
+                elif not stk or stk[-1] < 0:
+                    stk.append(x)
+        return stk
diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 037. \345\260\217\350\241\214\346\230\237\347\242\260\346\222\236/Solution.rs" "b/lcof2/\345\211\221\346\214\207 Offer II 037. \345\260\217\350\241\214\346\230\237\347\242\260\346\222\236/Solution.rs"
new file mode 100644
index 0000000000000..bd3c7e88457d9
--- /dev/null
+++ "b/lcof2/\345\211\221\346\214\207 Offer II 037. \345\260\217\350\241\214\346\230\237\347\242\260\346\222\236/Solution.rs"	
@@ -0,0 +1,21 @@
+impl Solution {
+    #[allow(dead_code)]
+    pub fn asteroid_collision(asteroids: Vec<i32>) -> Vec<i32> {
+        let mut stk = Vec::new();
+        for &x in &asteroids {
+            if x > 0 {
+                stk.push(x);
+            } else {
+                while !stk.is_empty() && *stk.last().unwrap() > 0 && *stk.last().unwrap() < -x {
+                    stk.pop();
+                }
+                if !stk.is_empty() && *stk.last().unwrap() == -x {
+                    stk.pop();
+                } else if stk.is_empty() || *stk.last().unwrap() < 0 {
+                    stk.push(x);
+                }
+            }
+        }
+        stk
+    }
+}
\ No newline at end of file
diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 037. \345\260\217\350\241\214\346\230\237\347\242\260\346\222\236/Solution.ts" "b/lcof2/\345\211\221\346\214\207 Offer II 037. \345\260\217\350\241\214\346\230\237\347\242\260\346\222\236/Solution.ts"
new file mode 100644
index 0000000000000..edbebda4b9016
--- /dev/null
+++ "b/lcof2/\345\211\221\346\214\207 Offer II 037. \345\260\217\350\241\214\346\230\237\347\242\260\346\222\236/Solution.ts"	
@@ -0,0 +1,18 @@
+function asteroidCollision(asteroids: number[]): number[] {
+    const stk: number[] = [];
+    for (const x of asteroids) {
+        if (x > 0) {
+            stk.push(x);
+        } else {
+            while (stk.length && stk.at(-1) > 0 && stk.at(-1) < -x) {
+                stk.pop();
+            }
+            if (stk.length && stk.at(-1) === -x) {
+                stk.pop();
+            } else if (!stk.length || stk.at(-1) < 0) {
+                stk.push(x);
+            }
+        }
+    }
+    return stk;
+}
diff --git a/solution/0700-0799/0735.Asteroid Collision/README.md b/solution/0700-0799/0735.Asteroid Collision/README.md
index 5cf24eff10b2f..5c80e78afd7ac 100644
--- a/solution/0700-0799/0735.Asteroid Collision/README.md	
+++ b/solution/0700-0799/0735.Asteroid Collision/README.md	
@@ -49,14 +49,16 @@
 
 <!-- 这里可写通用的实现逻辑 -->
 
-**方法一:栈模拟**
+**方法一:栈**
 
-可以类比成左右括号匹配:
+我们从左到右遍历每个小行星 $x$,由于每个小行星可能与之前的多个小行星发生碰撞,考虑用栈来存储。
 
--   向右移动的小行星(左括号):不会引发碰撞,直接入栈
--   向左移动的小行星(右括号):可能会和之前向右移动的小行星发生碰撞,特殊处理
+-   对于当前小行星,如果 $x \gt 0$,那么它一定不会跟前面的小行星发生碰撞,我们可以直接将 $x$ 入栈。
+-   否则,如果栈不为空并且栈顶元素大于 $0$,且栈顶元素小于 $-x$,那么栈顶元素对应的小行星会发生爆炸,我们循环将栈顶元素出栈,直到不满足条件。此时如果栈顶元素等于 $-x$,那么两个小行星会发生爆炸,只需要将栈顶元素出栈即可;如果栈为空,或者栈顶元素小于 $0$,那么当前小行星不会发生碰撞,我们将 $x$ 入栈。
 
-因为答案需要碰撞后剩下的所有小行星,相当于栈里最后剩下的元素,所以可以直接用数组表示栈
+最后我们返回栈中的元素即为答案。
+
+时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 是数组 $asteroids$ 的长度。
 
 <!-- tabs:start -->
 
@@ -67,18 +69,18 @@
 ```python
 class Solution:
     def asteroidCollision(self, asteroids: List[int]) -> List[int]:
-        ans = []
-        for a in asteroids:
-            if a > 0:
-                ans.append(a)
+        stk = []
+        for x in asteroids:
+            if x > 0:
+                stk.append(x)
             else:
-                while ans and 0 < ans[-1] < -a:
-                    ans.pop()
-                if ans and ans[-1] == -a:
-                    ans.pop()
-                elif not ans or ans[-1] < -a:
-                    ans.append(a)
-        return ans
+                while stk and stk[-1] > 0 and stk[-1] < -x:
+                    stk.pop()
+                if stk and stk[-1] == -x:
+                    stk.pop()
+                elif not stk or stk[-1] < 0:
+                    stk.append(x)
+        return stk
 ```
 
 ### **Java**
@@ -88,22 +90,22 @@ class Solution:
 ```java
 class Solution {
     public int[] asteroidCollision(int[] asteroids) {
-        Deque<Integer> d = new ArrayDeque<>();
-        for (int a : asteroids) {
-            if (a > 0) {
-                d.offerLast(a);
+        Deque<Integer> stk = new ArrayDeque<>();
+        for (int x : asteroids) {
+            if (x > 0) {
+                stk.offerLast(x);
             } else {
-                while (!d.isEmpty() && d.peekLast() > 0 && d.peekLast() < -a) {
-                    d.pollLast();
+                while (!stk.isEmpty() && stk.peekLast() > 0 && stk.peekLast() < -x) {
+                    stk.pollLast();
                 }
-                if (!d.isEmpty() && d.peekLast() == -a) {
-                    d.pollLast();
-                } else if (d.isEmpty() || d.peekLast() < -a) {
-                    d.offerLast(a);
+                if (!stk.isEmpty() && stk.peekLast() == -x) {
+                    stk.pollLast();
+                } else if (stk.isEmpty() || stk.peekLast() < 0) {
+                    stk.offerLast(x);
                 }
             }
         }
-        return d.stream().mapToInt(Integer::valueOf).toArray();
+        return stk.stream().mapToInt(Integer::valueOf).toArray();
     }
 }
 ```
@@ -114,122 +116,94 @@ class Solution {
 class Solution {
 public:
     vector<int> asteroidCollision(vector<int>& asteroids) {
-        vector<int> ans;
-        for (int a : asteroids) {
-            if (a > 0) {
-                ans.push_back(a);
+        vector<int> stk;
+        for (int x : asteroids) {
+            if (x > 0) {
+                stk.push_back(x);
             } else {
-                while (!ans.empty() && ans.back() > 0 && ans.back() < -a) {
-                    ans.pop_back();
+                while (stk.size() && stk.back() > 0 && stk.back() < -x) {
+                    stk.pop_back();
                 }
-                if (!ans.empty() && ans.back() == -a) {
-                    ans.pop_back();
-                } else if (ans.empty() || ans.back() < -a) {
-                    ans.push_back(a);
+                if (stk.size() && stk.back() == -x) {
+                    stk.pop_back();
+                } else if (stk.empty() || stk.back() < 0) {
+                    stk.push_back(x);
                 }
             }
         }
-        return ans;
+        return stk;
     }
 };
 ```
 
-### **Rust**
-
-```rust
-impl Solution {
-    #[allow(dead_code)]
-    pub fn asteroid_collision(asteroids: Vec<i32>) -> Vec<i32> {
-        let mut ret_stack = Vec::new();
-
-        for &a in &asteroids {
-            if ret_stack.is_empty() {
-                ret_stack.push(a);
-                continue;
-            }
-            if a > 0 {
-                ret_stack.push(a);
-                continue;
-            }
-            // Otherwise, peek the top element in the current stack
-            if a < 0 {
-                if *ret_stack.last().unwrap() < 0 {
-                    ret_stack.push(a);
-                    continue;
-                }
-                let mut explode_flag = false;
-                while !ret_stack.is_empty() && *ret_stack.last().unwrap() > 0 {
-                    let cur_res = *ret_stack.last().unwrap() + a;
-                    if cur_res < 0 {
-                        // |a| > |top()|
-                        assert_ne!(ret_stack.pop(), None);
-                    } else if cur_res == 0 {
-                        // |a| == |top()|
-                        explode_flag = true;
-                        assert_ne!(ret_stack.pop(), None);
-                        break;
-                    } else {
-                        // |a| < |top()|
-                        explode_flag = true;
-                        break;
-                    }
-                }
-                if !explode_flag {
-                    ret_stack.push(a);
-                }
-                continue;
-            }
-            assert!(false); // This is impossible
-        }
-
-        ret_stack
-    }
-}
-```
-
 ### **Go**
 
 ```go
-func asteroidCollision(asteroids []int) []int {
-	var ans []int
-	for _, a := range asteroids {
-		if a > 0 {
-			ans = append(ans, a)
+func asteroidCollision(asteroids []int) (stk []int) {
+	for _, x := range asteroids {
+		if x > 0 {
+			stk = append(stk, x)
 		} else {
-			for len(ans) > 0 && ans[len(ans)-1] > 0 && ans[len(ans)-1] < -a {
-				ans = ans[:len(ans)-1]
+			for len(stk) > 0 && stk[len(stk)-1] > 0 && stk[len(stk)-1] < -x {
+				stk = stk[:len(stk)-1]
 			}
-			if len(ans) > 0 && ans[len(ans)-1] == -a {
-				ans = ans[:len(ans)-1]
-			} else if len(ans) == 0 || ans[len(ans)-1] < -a {
-				ans = append(ans, a)
+			if len(stk) > 0 && stk[len(stk)-1] == -x {
+				stk = stk[:len(stk)-1]
+			} else if len(stk) == 0 || stk[len(stk)-1] < 0 {
+				stk = append(stk, x)
 			}
 		}
 	}
-	return ans
+	return
 }
 ```
 
-### TypeScript
+### **TypeScript**
 
 ```ts
 function asteroidCollision(asteroids: number[]): number[] {
-    const ans: number[] = [];
-    for (const a of asteroids) {
-        if (a > 0) {
-            ans.push(a);
+    const stk: number[] = [];
+    for (const x of asteroids) {
+        if (x > 0) {
+            stk.push(x);
         } else {
-            while (ans.length && 0 < ans[ans.length - 1] && ans[ans.length - 1] < -a) {
-                ans.pop();
+            while (stk.length && stk.at(-1) > 0 && stk.at(-1) < -x) {
+                stk.pop();
+            }
+            if (stk.length && stk.at(-1) === -x) {
+                stk.pop();
+            } else if (!stk.length || stk.at(-1) < 0) {
+                stk.push(x);
             }
-            if (ans.length && ans[ans.length - 1] === -a) {
-                ans.pop();
-            } else if (!ans.length || ans[ans.length - 1] < -a) {
-                ans.push(a);
+        }
+    }
+    return stk;
+}
+```
+
+### **Rust**
+
+```rust
+impl Solution {
+    #[allow(dead_code)]
+    pub fn asteroid_collision(asteroids: Vec<i32>) -> Vec<i32> {
+        let mut stk = Vec::new();
+        for &x in &asteroids {
+            if x > 0 {
+                stk.push(x);
+            } else {
+                while !stk.is_empty() && *stk.last().unwrap() > 0 && *stk.last().unwrap() < -x {
+                    stk.pop();
+                }
+                if !stk.is_empty() && *stk.last().unwrap() == -x {
+                    stk.pop();
+                } else if stk.is_empty() || *stk.last().unwrap() < 0 {
+                    stk.push(x);
+                }
             }
         }
+        stk
     }
-    return ans;
 }
 ```
 
diff --git a/solution/0700-0799/0735.Asteroid Collision/README_EN.md b/solution/0700-0799/0735.Asteroid Collision/README_EN.md
index 3f0c317b669cb..6b1f830c99a3c 100644
--- a/solution/0700-0799/0735.Asteroid Collision/README_EN.md	
+++ b/solution/0700-0799/0735.Asteroid Collision/README_EN.md	
@@ -46,12 +46,16 @@
 
 ## Solutions
 
-this can be analogous to matching parentheses:
+**Solution 1: Stack**
 
--   right-moving asteroid (left parenthesis): will not cause a collision, will be pushed directly
--   left-moving asteroid (right parenthesis): may collision with the right-moving asteroid, special treatment
+We traverse each asteroid $x$ from left to right. Since each asteroid may collide with multiple asteroids before it, we consider using a stack to store.
 
-because the answer requires all the asteroids left after the collision, which is element left in the stack, you can simply represent the stack in an array
+-   For the current asteroid, if $x>0$, it will definitely not collide with the previous asteroid, and we can directly push $x$ into the stack.
+-   Otherwise, if the stack is not empty and the top element of the stack is greater than $0$, and the top element of the stack is less than $-x$, then the top element of the stack corresponds to the asteroid will explode, we loop to the top element of the stack Pop out until the condition is not satisfied. At this time, if the top element of the stack is equal to $-x$, then the two asteroids will explode, and we only need to pop the top element of the stack; if the stack is empty, or the top element of the stack is less than $0$, then the current asteroid will not collide, we will push $x$ into the stack.
+
+Finally, we return the elements in the stack as the answer.
+
+The time complexity is $O(n)$, and the space complexity is $O(n)$. Where $n$ is the length of the array $asteroids$.
 
 <!-- tabs:start -->
 
@@ -60,18 +64,18 @@ because the answer requires all the asteroids left after the collision, which is
 ```python
 class Solution:
     def asteroidCollision(self, asteroids: List[int]) -> List[int]:
-        ans = []
-        for a in asteroids:
-            if a > 0:
-                ans.append(a)
+        stk = []
+        for x in asteroids:
+            if x > 0:
+                stk.append(x)
             else:
-                while ans and 0 < ans[-1] < -a:
-                    ans.pop()
-                if ans and ans[-1] == -a:
-                    ans.pop()
-                elif not ans or ans[-1] < -a:
-                    ans.append(a)
-        return ans
+                while stk and stk[-1] > 0 and stk[-1] < -x:
+                    stk.pop()
+                if stk and stk[-1] == -x:
+                    stk.pop()
+                elif not stk or stk[-1] < 0:
+                    stk.append(x)
+        return stk
 ```
 
 ### **Java**
@@ -79,22 +83,22 @@ class Solution:
 ```java
 class Solution {
     public int[] asteroidCollision(int[] asteroids) {
-        Deque<Integer> d = new ArrayDeque<>();
-        for (int a : asteroids) {
-            if (a > 0) {
-                d.offerLast(a);
+        Deque<Integer> stk = new ArrayDeque<>();
+        for (int x : asteroids) {
+            if (x > 0) {
+                stk.offerLast(x);
             } else {
-                while (!d.isEmpty() && d.peekLast() > 0 && d.peekLast() < -a) {
-                    d.pollLast();
+                while (!stk.isEmpty() && stk.peekLast() > 0 && stk.peekLast() < -x) {
+                    stk.pollLast();
                 }
-                if (!d.isEmpty() && d.peekLast() == -a) {
-                    d.pollLast();
-                } else if (d.isEmpty() || d.peekLast() < -a) {
-                    d.offerLast(a);
+                if (!stk.isEmpty() && stk.peekLast() == -x) {
+                    stk.pollLast();
+                } else if (stk.isEmpty() || stk.peekLast() < 0) {
+                    stk.offerLast(x);
                 }
             }
         }
-        return d.stream().mapToInt(Integer::valueOf).toArray();
+        return stk.stream().mapToInt(Integer::valueOf).toArray();
     }
 }
 ```
@@ -105,99 +109,45 @@ class Solution {
 class Solution {
 public:
     vector<int> asteroidCollision(vector<int>& asteroids) {
-        vector<int> ans;
-        for (int a : asteroids) {
-            if (a > 0) {
-                ans.push_back(a);
+        vector<int> stk;
+        for (int x : asteroids) {
+            if (x > 0) {
+                stk.push_back(x);
             } else {
-                while (!ans.empty() && ans.back() > 0 && ans.back() < -a) {
-                    ans.pop_back();
+                while (stk.size() && stk.back() > 0 && stk.back() < -x) {
+                    stk.pop_back();
                 }
-                if (!ans.empty() && ans.back() == -a) {
-                    ans.pop_back();
-                } else if (ans.empty() || ans.back() < -a) {
-                    ans.push_back(a);
+                if (stk.size() && stk.back() == -x) {
+                    stk.pop_back();
+                } else if (stk.empty() || stk.back() < 0) {
+                    stk.push_back(x);
                 }
             }
         }
-        return ans;
+        return stk;
     }
 };
 ```
 
-### **Rust**
-
-```rust
-impl Solution {
-    #[allow(dead_code)]
-    pub fn asteroid_collision(asteroids: Vec<i32>) -> Vec<i32> {
-        let mut ret_stack = Vec::new();
-
-        for &a in &asteroids {
-            if ret_stack.is_empty() {
-                ret_stack.push(a);
-                continue;
-            }
-            if a > 0 {
-                ret_stack.push(a);
-                continue;
-            }
-            // Otherwise, peek the top element in the current stack
-            if a < 0 {
-                if *ret_stack.last().unwrap() < 0 {
-                    ret_stack.push(a);
-                    continue;
-                }
-                let mut explode_flag = false;
-                while !ret_stack.is_empty() && *ret_stack.last().unwrap() > 0 {
-                    let cur_res = *ret_stack.last().unwrap() + a;
-                    if cur_res < 0 {
-                        // |a| > |top()|
-                        assert_ne!(ret_stack.pop(), None);
-                    } else if cur_res == 0 {
-                        // |a| == |top()|
-                        explode_flag = true;
-                        assert_ne!(ret_stack.pop(), None);
-                        break;
-                    } else {
-                        // |a| < |top()|
-                        explode_flag = true;
-                        break;
-                    }
-                }
-                if !explode_flag {
-                    ret_stack.push(a);
-                }
-                continue;
-            }
-            assert!(false); // This is impossible
-        }
-
-        ret_stack
-    }
-}
-```
-
 ### **Go**
 
 ```go
-func asteroidCollision(asteroids []int) []int {
-	var ans []int
-	for _, a := range asteroids {
-		if a > 0 {
-			ans = append(ans, a)
+func asteroidCollision(asteroids []int) (stk []int) {
+	for _, x := range asteroids {
+		if x > 0 {
+			stk = append(stk, x)
 		} else {
-			for len(ans) > 0 && ans[len(ans)-1] > 0 && ans[len(ans)-1] < -a {
-				ans = ans[:len(ans)-1]
+			for len(stk) > 0 && stk[len(stk)-1] > 0 && stk[len(stk)-1] < -x {
+				stk = stk[:len(stk)-1]
 			}
-			if len(ans) > 0 && ans[len(ans)-1] == -a {
-				ans = ans[:len(ans)-1]
-			} else if len(ans) == 0 || ans[len(ans)-1] < -a {
-				ans = append(ans, a)
+			if len(stk) > 0 && stk[len(stk)-1] == -x {
+				stk = stk[:len(stk)-1]
+			} else if len(stk) == 0 || stk[len(stk)-1] < 0 {
+				stk = append(stk, x)
 			}
 		}
 	}
-	return ans
+	return
 }
 ```
 
@@ -205,22 +155,48 @@ func asteroidCollision(asteroids []int) []int {
 
 ```ts
 function asteroidCollision(asteroids: number[]): number[] {
-    const ans: number[] = [];
-    for (const a of asteroids) {
-        if (a > 0) {
-            ans.push(a);
+    const stk: number[] = [];
+    for (const x of asteroids) {
+        if (x > 0) {
+            stk.push(x);
         } else {
-            while (ans.length && 0 < ans[ans.length - 1] && ans[ans.length - 1] < -a) {
-                ans.pop();
+            while (stk.length && stk.at(-1) > 0 && stk.at(-1) < -x) {
+                stk.pop();
             }
-            if (ans.length && ans[ans.length - 1] === -a) {
-                ans.pop();
-            } else if (!ans.length || ans[ans.length - 1] < -a) {
-                ans.push(a);
+            if (stk.length && stk.at(-1) === -x) {
+                stk.pop();
+            } else if (!stk.length || stk.at(-1) < 0) {
+                stk.push(x);
+            }
+        }
+    }
+    return stk;
+}
+```
+
+### **Rust**
+
+```rust
+impl Solution {
+    #[allow(dead_code)]
+    pub fn asteroid_collision(asteroids: Vec<i32>) -> Vec<i32> {
+        let mut stk = Vec::new();
+        for &x in &asteroids {
+            if x > 0 {
+                stk.push(x);
+            } else {
+                while !stk.is_empty() && *stk.last().unwrap() > 0 && *stk.last().unwrap() < -x {
+                    stk.pop();
+                }
+                if !stk.is_empty() && *stk.last().unwrap() == -x {
+                    stk.pop();
+                } else if stk.is_empty() || *stk.last().unwrap() < 0 {
+                    stk.push(x);
+                }
             }
         }
+        stk
     }
-    return ans;
 }
 ```
 
diff --git a/solution/0700-0799/0735.Asteroid Collision/Solution.cpp b/solution/0700-0799/0735.Asteroid Collision/Solution.cpp
index e4d96bd3ed708..768f03fc6a28c 100644
--- a/solution/0700-0799/0735.Asteroid Collision/Solution.cpp	
+++ b/solution/0700-0799/0735.Asteroid Collision/Solution.cpp	
@@ -1,21 +1,21 @@
-class Solution {
-public:
-    vector<int> asteroidCollision(vector<int>& asteroids) {
-        vector<int> ans;
-        for (int a : asteroids) {
-            if (a > 0) {
-                ans.push_back(a);
-            } else {
-                while (!ans.empty() && ans.back() > 0 && ans.back() < -a) {
-                    ans.pop_back();
-                }
-                if (!ans.empty() && ans.back() == -a) {
-                    ans.pop_back();
-                } else if (ans.empty() || ans.back() < -a) {
-                    ans.push_back(a);
-                }
-            }
-        }
-        return ans;
-    }
-};
+class Solution {
+public:
+    vector<int> asteroidCollision(vector<int>& asteroids) {
+        vector<int> stk;
+        for (int x : asteroids) {
+            if (x > 0) {
+                stk.push_back(x);
+            } else {
+                while (stk.size() && stk.back() > 0 && stk.back() < -x) {
+                    stk.pop_back();
+                }
+                if (stk.size() && stk.back() == -x) {
+                    stk.pop_back();
+                } else if (stk.empty() || stk.back() < 0) {
+                    stk.push_back(x);
+                }
+            }
+        }
+        return stk;
+    }
+};
\ No newline at end of file
diff --git a/solution/0700-0799/0735.Asteroid Collision/Solution.go b/solution/0700-0799/0735.Asteroid Collision/Solution.go
index ca38f4feb3624..26d1420d98ba8 100644
--- a/solution/0700-0799/0735.Asteroid Collision/Solution.go	
+++ b/solution/0700-0799/0735.Asteroid Collision/Solution.go	
@@ -1,18 +1,17 @@
-func asteroidCollision(asteroids []int) []int {
-	var ans []int
-	for _, a := range asteroids {
-		if a > 0 {
-			ans = append(ans, a)
+func asteroidCollision(asteroids []int) (stk []int) {
+	for _, x := range asteroids {
+		if x > 0 {
+			stk = append(stk, x)
 		} else {
-			for len(ans) > 0 && ans[len(ans)-1] > 0 && ans[len(ans)-1] < -a {
-				ans = ans[:len(ans)-1]
+			for len(stk) > 0 && stk[len(stk)-1] > 0 && stk[len(stk)-1] < -x {
+				stk = stk[:len(stk)-1]
 			}
-			if len(ans) > 0 && ans[len(ans)-1] == -a {
-				ans = ans[:len(ans)-1]
-			} else if len(ans) == 0 || ans[len(ans)-1] < -a {
-				ans = append(ans, a)
+			if len(stk) > 0 && stk[len(stk)-1] == -x {
+				stk = stk[:len(stk)-1]
+			} else if len(stk) == 0 || stk[len(stk)-1] < 0 {
+				stk = append(stk, x)
 			}
 		}
 	}
-	return ans
+	return
 }
\ No newline at end of file
diff --git a/solution/0700-0799/0735.Asteroid Collision/Solution.java b/solution/0700-0799/0735.Asteroid Collision/Solution.java
index 787b2e17f051b..2482fab648a88 100644
--- a/solution/0700-0799/0735.Asteroid Collision/Solution.java	
+++ b/solution/0700-0799/0735.Asteroid Collision/Solution.java	
@@ -1,20 +1,20 @@
-class Solution {
-    public int[] asteroidCollision(int[] asteroids) {
-        Deque<Integer> d = new ArrayDeque<>();
-        for (int a : asteroids) {
-            if (a > 0) {
-                d.offerLast(a);
-            } else {
-                while (!d.isEmpty() && d.peekLast() > 0 && d.peekLast() < -a) {
-                    d.pollLast();
-                }
-                if (!d.isEmpty() && d.peekLast() == -a) {
-                    d.pollLast();
-                } else if (d.isEmpty() || d.peekLast() < -a) {
-                    d.offerLast(a);
-                }
-            }
-        }
-        return d.stream().mapToInt(Integer::valueOf).toArray();
-    }
-}
+class Solution {
+    public int[] asteroidCollision(int[] asteroids) {
+        Deque<Integer> stk = new ArrayDeque<>();
+        for (int x : asteroids) {
+            if (x > 0) {
+                stk.offerLast(x);
+            } else {
+                while (!stk.isEmpty() && stk.peekLast() > 0 && stk.peekLast() < -x) {
+                    stk.pollLast();
+                }
+                if (!stk.isEmpty() && stk.peekLast() == -x) {
+                    stk.pollLast();
+                } else if (stk.isEmpty() || stk.peekLast() < 0) {
+                    stk.offerLast(x);
+                }
+            }
+        }
+        return stk.stream().mapToInt(Integer::valueOf).toArray();
+    }
+}
\ No newline at end of file
diff --git a/solution/0700-0799/0735.Asteroid Collision/Solution.py b/solution/0700-0799/0735.Asteroid Collision/Solution.py
index f073fb9ccc97d..01660c5098ec8 100644
--- a/solution/0700-0799/0735.Asteroid Collision/Solution.py	
+++ b/solution/0700-0799/0735.Asteroid Collision/Solution.py	
@@ -1,14 +1,14 @@
-class Solution:
-    def asteroidCollision(self, asteroids: List[int]) -> List[int]:
-        ans = []
-        for a in asteroids:
-            if a > 0:
-                ans.append(a)
-            else:
-                while ans and 0 < ans[-1] < -a:
-                    ans.pop()
-                if ans and ans[-1] == -a:
-                    ans.pop()
-                elif not ans or ans[-1] < -a:
-                    ans.append(a)
-        return ans
+class Solution:
+    def asteroidCollision(self, asteroids: List[int]) -> List[int]:
+        stk = []
+        for x in asteroids:
+            if x > 0:
+                stk.append(x)
+            else:
+                while stk and stk[-1] > 0 and stk[-1] < -x:
+                    stk.pop()
+                if stk and stk[-1] == -x:
+                    stk.pop()
+                elif not stk or stk[-1] < 0:
+                    stk.append(x)
+        return stk
diff --git a/solution/0700-0799/0735.Asteroid Collision/Solution.rs b/solution/0700-0799/0735.Asteroid Collision/Solution.rs
index 13df14d529c91..bd3c7e88457d9 100644
--- a/solution/0700-0799/0735.Asteroid Collision/Solution.rs	
+++ b/solution/0700-0799/0735.Asteroid Collision/Solution.rs	
@@ -1,48 +1,21 @@
-impl Solution {
-    #[allow(dead_code)]
-    pub fn asteroid_collision(asteroids: Vec<i32>) -> Vec<i32> {
-        let mut ret_stack = Vec::new();
-
-        for &a in &asteroids {
-            if ret_stack.is_empty() {
-                ret_stack.push(a);
-                continue;
-            }
-            if a > 0 {
-                ret_stack.push(a);
-                continue;
-            }
-            // Otherwise, peek the top element in the current stack
-            if a < 0 {
-                if *ret_stack.last().unwrap() < 0 {
-                    ret_stack.push(a);
-                    continue;
-                }
-                let mut explode_flag = false;
-                while !ret_stack.is_empty() && *ret_stack.last().unwrap() > 0 {
-                    let cur_res = *ret_stack.last().unwrap() + a;
-                    if cur_res < 0 {
-                        // |a| > |top()|
-                        assert_ne!(ret_stack.pop(), None);
-                    } else if cur_res == 0 {
-                        // |a| == |top()|
-                        explode_flag = true;
-                        assert_ne!(ret_stack.pop(), None);
-                        break;
-                    } else {
-                        // |a| < |top()|
-                        explode_flag = true;
-                        break;
-                    }
-                }
-                if !explode_flag {
-                    ret_stack.push(a);
-                }
-                continue;
-            }
-            assert!(false); // This is impossible
-        }
-
-        ret_stack
-    }
+impl Solution {
+    #[allow(dead_code)]
+    pub fn asteroid_collision(asteroids: Vec<i32>) -> Vec<i32> {
+        let mut stk = Vec::new();
+        for &x in &asteroids {
+            if x > 0 {
+                stk.push(x);
+            } else {
+                while !stk.is_empty() && *stk.last().unwrap() > 0 && *stk.last().unwrap() < -x {
+                    stk.pop();
+                }
+                if !stk.is_empty() && *stk.last().unwrap() == -x {
+                    stk.pop();
+                } else if stk.is_empty() || *stk.last().unwrap() < 0 {
+                    stk.push(x);
+                }
+            }
+        }
+        stk
+    }
 }
\ No newline at end of file
diff --git a/solution/0700-0799/0735.Asteroid Collision/Solution.ts b/solution/0700-0799/0735.Asteroid Collision/Solution.ts
index d9497e16c8ea9..edbebda4b9016 100644
--- a/solution/0700-0799/0735.Asteroid Collision/Solution.ts	
+++ b/solution/0700-0799/0735.Asteroid Collision/Solution.ts	
@@ -1,18 +1,18 @@
 function asteroidCollision(asteroids: number[]): number[] {
-    const ans: number[] = [];
-    for (const a of asteroids) {
-        if (a > 0) {
-            ans.push(a);
+    const stk: number[] = [];
+    for (const x of asteroids) {
+        if (x > 0) {
+            stk.push(x);
         } else {
-            while (ans.length && 0 < ans[ans.length - 1] && ans[ans.length - 1] < -a) {
-                ans.pop();
+            while (stk.length && stk.at(-1) > 0 && stk.at(-1) < -x) {
+                stk.pop();
             }
-            if (ans.length && ans[ans.length - 1] === -a) {
-                ans.pop();
-            } else if (!ans.length || ans[ans.length - 1] < -a) {
-                ans.push(a);
+            if (stk.length && stk.at(-1) === -x) {
+                stk.pop();
+            } else if (!stk.length || stk.at(-1) < 0) {
+                stk.push(x);
             }
         }
     }
-    return ans;
+    return stk;
 }