当前位置:网站首页>Force deduction question 3
Force deduction question 3
2022-07-25 03:27:00 【Xiao Tang Xuejie】
Medium difficulty 305 Switch to English to receive dynamic feedback
Given an array of integers asteroids, Represents planets on the same line .
For each element in the array , Its absolute value represents the size of the planet , Positive and negative indicate the direction of movement of the planet ( Positive means moving to the right , Negative means moving to the left ). Every planet moves at the same speed .
Find all the planets left after the collision . Collision rules : The two planets collide with each other , Smaller planets will explode . If two planets are the same size , Then both planets will explode . Two planets moving in the same direction , There will never be a collision .
Example 1:
Input :asteroids = [5,10,-5] Output :[5,10] explain :10 and -5 After the collision, only 10 . 5 and 10 There will never be a collision .
Example 2:
Input :asteroids = [8,-8] Output :[] explain :8 and -8 After the collision , Both exploded .
Example 3:
Input :asteroids = [10,2,-5] Output :[10] explain :2 and -5 After the collision, there are -5 .10 and -5 After the collision, there are 10 .
Tips :
2 <= asteroids.length <= 104-1000 <= asteroids[i] <= 1000asteroids[i] != 0
class Solution {
public int[] asteroidCollision(int[] asteroids) {
// Utilization stack , Positive numbers are stored inside , Encountered a negative number stack
Stack<Integer> st = new Stack<>();
for(int i = 0; i < asteroids.length; i++){
if(asteroids[i] > 0) st.push(asteroids[i]);
else if((asteroids[i] < 0 && st.isEmpty()) || st.peek() < 0) st.push(asteroids[i]);
else {
int pop = st.pop();
if((pop + asteroids[i]) > 0)st.push(pop);
else if((pop + asteroids[i]) < 0){
i--;
}
}
}
Object[] ob = st.toArray();
int[] nums = new int[st.size()];
for(int i = 0; i < ob.length; i++){
nums[i] = (int)ob[i];
}
return nums;
}
}边栏推荐
- B. Almost Ternary Matrix
- Merge sort / quick sort
- Network security - information hiding - use steganography to prevent sensitive data from being stolen
- Unity: test rotation function
- mysql_ Create temporary table
- Solve mysql5.6 database specified key was too long; Max key length is 767 bytes
- 04 -- two ways of writing el and data
- [Flink] transform operator filter
- Analysis of cascading relation operation examples of cascade
- Secondary vocational network security skills competition P100 web penetration test
猜你喜欢

Force the resumption of game 302 of the week

kettle_ Configure database connection_ report errors

Network security - information hiding - use steganography to prevent sensitive data from being stolen

How chemical enterprises choose digital service providers with dual prevention mechanism
![[kaggle] how to effectively avoid oom and the long process of alchemy](/img/d1/cf6ecdeea9aa97d0eb93aa963687a5.png)
[kaggle] how to effectively avoid oom and the long process of alchemy

Li Kou 343 integer partition dynamic programming

Chrome process architecture

Unity: test rotation function

Brief understanding of operational amplifier

Bgy development small example
随机推荐
55k is stable, and the recommendation system will always drop God!
A. Subtle Substring Subtraction
Uni app configuration
JS method encapsulation summary
B. All Distinct
Openlayers draw circles and ellipses
VMware installation
144. Preorder traversal of binary tree
05 - MVVM model
Electronic bidding procurement mall system: optimize traditional procurement business and speed up enterprise digital upgrading
TypeScript
Flowlayout in compose
Use go language to import Doris data through stream load
[brother hero July training] day 19: binary tree
Leetcode.745. prefix and suffix search____ Double dictionary tree + double pointer
Unity: text input box for numerical judgment
Moveit2 - 7. Scenario planning ROS API
Force deduction problem 238. product of arrays other than itself
Force deduction brush question 26. Delete duplicates in the ordered array
What should testers do if they encounter a bug that is difficult to reproduce?