当前位置:网站首页>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;
}
}边栏推荐
- Are interviews all about memorizing answers?
- Upgrading of computing power under the coordination of software and hardware, redefining productivity
- Minikube initial experience environment construction
- Specific process of strong cache and negotiation cache
- 强缓存、协商缓存具体过程
- Consumer installation and configuration
- R language uses LM function to build regression model with interactive items, and uses: sign (colon) to represent the interaction of variables (colon is pure multiplication, excluding the constituent
- Some knowledge concepts
- Traversal and copy of files in jar package
- 程序的存储态与运行态
猜你喜欢

可视化大型时间序列的技巧。

Hcip rip comprehensive experiment

中国业务型CDP白皮书 | 爱分析报告
![[real question of written examination]](/img/3f/e061df6a2c5c92429cfd3c69cc94ce.png)
[real question of written examination]

IDEA复制模块

Simple selection sort and heap sort

Globalthis is not defined solution

Training mode and practice of digital applied talents in Colleges and Universities under the integration of industry and education

REST风格

Lua 中 __index、__newindex、rawget、rawset的理解
随机推荐
Go deadlock - when the channel meets mutex
多线程与高并发(三)—— 源码解析 AQS 原理
What is WordPress
The game process and the underlying implementation are gradually completed
14、用户web层服务(二)
【补题日记】[2022牛客暑期多校2]H-Take the Elevator
R language uses LM function to build regression model with interactive items, and uses: sign (colon) to represent the interaction of variables (colon is pure multiplication, excluding the constituent
2022.07.08 summer training personal qualifying (III)
Detailed explanation of boost official website search engine project
Introduction to the usage of SAP ui5 image display control avatar trial version
Alexnet - paper analysis and reproduction
Matlab sets the size of graphics window and image and the position of legend
Redis安装
Interfaces and abstract classes
Shell (II)
强缓存、协商缓存具体过程
Start from scratch blazor server (2) -- consolidate databases
jar 包内文件的遍历以及文件的拷贝
“蔚来杯“2022牛客暑期多校训练营2
15、用户web层服务(三)