当前位置:网站首页>[C language brush leetcode] 735. Planetary collision (m)
[C language brush leetcode] 735. Planetary collision (m)
2022-07-26 02:00:00 【kinbo88】
【
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 .
】
It's easy to think of monotonous stack from the meaning of the title , But I read the meaning of the title wrong twice , So read the meaning of the question three times before doing it , Have a good understanding of .
Because it either disappears or doesn't change its size when it collides , So the processing of elements in the stack idx Index carefully .
The top element of the stack is right , The current left will collide . So judge the result after the collision , You can sum two elements .
int* asteroidCollision(int* asteroids, int asteroidsSize, int* returnSize){
int *stack = NULL;
int idx = -1; // idx Point to the location where the element is stored
int i;
int flag = 1; // The direction of the previous element ,1: Right ;-1: Left
stack = (int *)malloc(sizeof(int) * asteroidsSize);
memset(stack, 0, sizeof(int) * asteroidsSize);
stack[++idx] = asteroids[0];
for (i = 1; i < asteroidsSize; i++) {
int comp = -1;
// The top element of the stack is right , The current left will collide
while (asteroids[i] < 0 && idx > -1 && stack[idx] > 0) {
comp = stack[idx] + asteroids[i]; // Compare positive and negative with the top element
if (comp < 0) {
idx--; // The top element of the stack exploded
continue;
} else if (comp == 0) {
idx--;
break;
} else {
break;
}
}
if (comp < 0) {
stack[++idx] = asteroids[i]; // Put the current element in
}
}
*returnSize = idx + 1;
return stack;
}
/*
1. I read the wrong title again , Smaller ones will explode , Not a big reduction , Some code will be readjusted
2. Understand the wrong meaning . Think the first left , The second right will also collide , I understand [-2,-1,1,2] The return value of is null , The correct return value of the result is [-2,-1,1,2], Complicate the subject
3. flag Attention should be paid to when judging idx
Don't take out the elements this time , Direct comparison
*/边栏推荐
- [Android development IOS series] Language: swift vs kotlin
- HTC手机官解、S-ON/S-OFF与超级CID的关系
- Arm assembly foundation of SOC
- Implementation of C iterator
- Three modes of CPU
- Video game quiz? I think it's useless. It's better to do these well!
- The e-commerce project is written in the resume. How to answer it during the interview
- JS add random pixel noise background to the page
- mysql 事务隔离级别
- leetcode/只出现一次的数字
猜你喜欢

Zhinai buys melons (DP backpack)

Implementation of recommendation system collaborative filtering in spark

Why does the debugger display the wrong function

SQLyog数据导入导出图文教程

E. Split into two sets

# Dest0g3 520迎新赛(更新中)
![[untitled]](/img/bf/7c4be5b442d12b2b1896f197be30fa.png)
[untitled]

D. Rating compression (thinking + double pointer)

【2020】【论文笔记】磁控溅射法生长Bi2Te3/CoFeB双层异质结——

Implementation of C iterator
随机推荐
The import and Export button of Damon database table is gray, and the DMP file cannot be imported
NFT market also began to diversify
The work of robot engineering and the puzzle of postgraduate entrance examination "volume" supplement
What is cross site scripting (XSS)?
SQL manual blind injection and error reporting injection
Web3.0 blog DAPP development practice [2022]
Worthington nuclease and Micrococcus related research and determination scheme
【深入浅出玩转FPGA学习11----Testbench书写技巧1】
2022 love analysis ― bank digitalization practice report
There is no setter method in grpc list under flutter. How to use related attributes
Proto conversion dart | project uses protobuf | fluent uses grpc
The e-commerce project is written in the resume. How to answer it during the interview
Remember a laravel problem script @php artist package:discover handling the post autoload dump event returned with
[Verilog digital system design (Xia Yuwen) 3 ----- basic concepts of Verilog syntax 1]
Image batch processing Gaussian filter noise reduction + peak signal-to-noise ratio calculation
MPLS knowledge points
【2021】【论文笔记】6G技术愿景——OTFS调制技术
Guys, the flinksql datahub source table has a field timestamp 16 bits, which is written to ora
How does Flink SQL configure to print the insert parameter log
dataframe 修改某行某列位置的值