当前位置:网站首页>Force deduction solution summary 735 planetary collision
Force deduction solution summary 735 planetary collision
2022-07-28 12:04:00 【Lost summer】
Directory links :
Force buckle programming problem - The solution sums up _ Share + Record -CSDN Blog
GitHub Synchronous question brushing items :
https://github.com/September26/java-algorithms
Original link : Power button
describe :
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] <= 1000
asteroids[i] != 0
source : Power button (LeetCode)
link :https://leetcode.cn/problems/asteroid-collision
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
Their thinking :
* Their thinking : * To solve this problem, you can use stack . Traverse left to right , If it is a positive number, it will be put on the stack . If the number is negative, the absolute value is compared with the number at the top of the stack , * If it is greater than, the stack will pop out of the stack , Continue to compare the next in the stack ; * If less than, continue to cycle ; * If equal to, the stack comes out of the stack , And continue the cycle . * There is a situation , If the stack is empty , And the reading is still negative , This situation can be directly added to the final result set .
Code :
public class Solution735 {
public int[] asteroidCollision(int[] asteroids) {
List<Integer> list = new ArrayList<>();
Stack<Integer> stack = new Stack<>();
for (int i = 0; i < asteroids.length; i++) {
int value = asteroids[i];
if (value > 0) {
stack.add(value);
continue;
}
int abs = Math.abs(value);
while (stack.size() > 0) {
Integer top = stack.peek();
if (abs > top) {
stack.pop();
} else if (abs < top) {
break;
} else {
//value Change it to 0, Does not represent a collection
value = 0;
stack.pop();
break;
}
}
if (stack.size() == 0 && value != 0) {
list.add(value);
}
}
list.addAll(stack);
int[] ints = list.stream().mapToInt(Integer::intValue).toArray();
return ints;
}
}边栏推荐
- Minikube initial experience environment construction
- Excel shortcut keys (letters + numbers) Encyclopedia
- [real question of written examination]
- Distributed system (III) construction of distributed transaction service
- Globalthis is not defined solution
- Go deadlock - when the channel meets mutex
- 【补题日记】[2022牛客暑期多校2]L-Link with Level Editor I
- Design and implementation of SSM personal blog system
- Unitywebrequest is used in unity to load network and local pictures
- 从0开发一个自己的npm包
猜你喜欢

14、用户web层服务(二)

AlexNet—论文分析及复现

Techniques for visualizing large time series.

直接插入排序与希尔排序

Shell (II)

Unity one key replacement of objects in the scene

Hcip day 6 (OSPF related knowledge)

Untiy controls the playback speed of animation

The principle and use of the wrap file of tolua

Update dev (development version) of the latest win11
随机推荐
R language ggplot2 visualization: ggdensity function of ggpubr package visualizes density graph and uses stat_ overlay_ normal_ Density function superimposes positive distribution curve, custom config
R language ggplot2 visualization: use the ggdotplot function of ggpubr package to visualize dot plot, set the add parameter, add violin graph to the dot plot, and add the vertical line of mean standar
Character function and string function (Part 1)
Service Workers让网站动态加载Webp图片
Learn to use MySQL explain to execute the plan, and SQL performance tuning is no longer difficult
Client service registration of Nacos registry
Will PFP be the future of digital collections?
Hcip (PAP authentication and chap authentication of PPP)
Deployment and use of Minio distributed object storage
ASP. Net core 6 framework unveiling example demonstration [29]: building a file server
Simple selection sort and heap sort
Anonymous subclass objects of abstract classes
Saltstack command injection vulnerability analysis (cve-2020-16846)
Full resolution of the use of go native plug-ins
移动端人脸风格化技术的应用
一些多参数函数的具体作用
Network communication protocol classification and IP address
[real question of written examination]
Why does acid food hurt teeth + early periodontitis
LabVIEW AI visual Toolkit (non Ni vision) download and installation tutorial