当前位置:网站首页>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;
}
}边栏推荐
- C language_ Defining structures and using variables
- B. All Distinct
- Use reflection to convert RDD to dataframe
- Time formatting
- Force button brushing question 61. rotating linked list
- Easyexcel sets the style of the last row [which can be expanded to each row]
- Direct insert sort / Hill sort
- Secondary vocational network security skills competition P100 dcore (light CMS system) SQL injection
- Imeta | ggclusternet microbial network analysis and visualization nanny level tutorial
- 04 -- two ways of writing el and data
猜你喜欢

04 -- two ways of writing el and data

Select sort / cardinality sort

Openlayers ol ext: Transform object, rotate, stretch, zoom in

Force the resumption of game 302 of the week

Machine learning notes - building a recommendation system (4) matrix decomposition for collaborative filtering

Lombok detailed introduction

Secondary vocational network security skills competition P100 dcore (light CMS system) SQL injection

From input URL to page presentation

Chrome process architecture

mysql_ Record the executed SQL
随机推荐
From input URL to page presentation
C language_ Structure introduction
Matplotlib tutorial (I) [getting to know Matplotlib first]
292. Nim game
Skywalking distributed link tracking, related graphics, DLJD, cat
How is the virtual DOM different from the actual DOM?
Use reflection to convert RDD to dataframe
How chemical enterprises choose digital service providers with dual prevention mechanism
Import and export using poi
Idea configuration
C language writes a circular advertising lantern or changes it to a confession system
List type to string type
Day 9 (capture traffic and routing strategy)
Print the common part of two ordered linked lists
Bubble sort / heap sort
Imeta | ggclusternet microbial network analysis and visualization nanny level tutorial
Swagger key configuration items
Moveit2 - 6. Planning scene monitor
What should testers do if they encounter a bug that is difficult to reproduce?
Machine learning exercise 8 - anomaly detection and recommendation system (collaborative filtering)