当前位置:网站首页>剑指 Offer II 037. 小行星碰撞
剑指 Offer II 037. 小行星碰撞
2022-07-30 14:20:00 【Mr Gao】
剑指 Offer II 037. 小行星碰撞
给定一个整数数组 asteroids,表示在同一行的小行星。
对于数组中的每一个元素,其绝对值表示小行星的大小,正负表示小行星的移动方向(正表示向右移动,负表示向左移动)。每一颗小行星以相同的速度移动。
找出碰撞后剩下的所有小行星。碰撞规则:两个行星相互碰撞,较小的行星会爆炸。如果两颗行星大小相同,则两颗行星都会爆炸。两颗移动方向相同的行星,永远不会发生碰撞。
示例 1:
输入:asteroids = [5,10,-5]
输出:[5,10]
解释:10 和 -5 碰撞后只剩下 10 。 5 和 10 永远不会发生碰撞。
示例 2:
输入:asteroids = [8,-8]
输出:[]
解释:8 和 -8 碰撞后,两者都发生爆炸。
示例 3:
输入:asteroids = [10,2,-5]
输出:[10]
解释:2 和 -5 发生碰撞后剩下 -5 。10 和 -5 发生碰撞后剩下 10 。
示例 4:
输入:asteroids = [-2,-1,1,2]
输出:[-2,-1,1,2]
解释:-2 和 -1 向左移动,而 1 和 2 向右移动。 由于移动方向相同的行星不会发生碰撞,所以最终没有行星发生碰撞。
解题代码如下:
/** * Note: The returned array must be malloced, assume caller calls free(). */
int* asteroidCollision(int* asteroids, int asteroidsSize, int* returnSize){
int r[asteroidsSize];
r[0]=asteroids[0];
int size=1;
int i;
for(i=1;i<asteroidsSize;i++){
int now=asteroids[i];
// printf("-- %d %d %d",now,r[size-1],size);
if(size==0){
r[size++]=now;
continue;
}
if(now*r[size-1]<0&&now<0){
//printf("%d %d ",abs(now),abs(r[size-1]));
while(abs(r[size-1])<=abs(now)&&size!=0&&now*r[size-1]<0&&now<0){
if(abs(r[size-1])==abs(now)){
size--;
// printf("%d ",size);
break;
}
//if(size==)
size--;
if(size==0){
break;
}
}
if(size>=1)
if(now*r[size-1]>0&&r[size]!=-now){
r[size++]=now;
}
if(size==0&&r[0]!=-now){
r[size++]=now;
}
}
else{
r[size++]=now;
// printf("| %d ",size);
}
}
// printf("size %d ",size);
for(i=0;i<size;i++){
asteroids[i]=r[i];
// printf("%d ",r[i]);
}
* returnSize=size;
return asteroids;
}
边栏推荐
- 双碳目标下:农田温室气体排放模拟
- 关于容器的小案例
- 开始学习C语言了
- ECCV 2022 | Towards Data Efficient Transformer Object Detectors
- Redis6.0 source code learning (5) ziplist
- eclipse连接SQL server数据库「建议收藏」
- Teach you how to write an eye-catching software testing resume, if you don't receive an interview invitation, I will lose
- 基于FPGA的DDS任意波形输出
- 基于5G的仓储信息化解决方案2022
- 什么是缺陷分析?一篇文章带你了解,测试工程师必备技能
猜你喜欢

使用 protobuf 进行数据序列化

2022年,目前大环境下还适合转行软件测试吗?

Flink实时数仓完结

元宇宙邮局AI航天主题系列数字藏品 将于7月30日10:00点上线“元邮数藏”

Six-faced ant financial clothing, resisting the bombardment of the interviewer, came to interview for review

Eight years of testing experience, why was the leader criticized: the test documents you wrote are not as good as those of fresh graduates

The highest level of wiring in the computer room, the beauty is suffocating

ECCV 2022 | 通往数据高效的Transformer目标检测器

新时代背景下智慧城市的建设与5G技术有何关联

canal抓取数据
随机推荐
新时代背景下智慧城市的建设与5G技术有何关联
Eight years of testing experience, why was the leader criticized: the test documents you wrote are not as good as those of fresh graduates
English语法_不定代词 - both / either / neither
【Vue.js 3.0源码】KeepAlive 组件:如何让组件在内存中缓存和调度?
Redis6.0 source code learning (5) ziplist
【回归预测-lssvm分类】基于最小二乘支持向量机lssvm实现数据分类代码
业内人士真心话:只会测试没有前途的,我慌了......
Eclipse connects to SQL server database "recommended collection"
吃透Chisel语言.28.Chisel进阶之有限状态机(二)——Mealy状态机及与Moore状态机的对比
基于FPGA的DDS任意波形输出
A new generation of open source free terminal tools, so cool
MPSK抗噪声性能对比(即MPSK标准误码率曲线)
Application of time series database in the field of ship risk management
Hello, World
吃透Chisel语言.29.Chisel进阶之通信状态机(一)——通信状态机:以闪光灯为例
Hello,World
2022年,目前大环境下还适合转行软件测试吗?
元宇宙邮局AI航天主题系列数字藏品 将于7月30日10:00点上线“元邮数藏”
双碳目标下:农田温室气体排放模拟
关于String的一些思考