当前位置:网站首页>剑指 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;
}
边栏推荐
- Some thoughts on String
- Machine learning difference in the competition and industry application
- localhost与127.0.0.1
- The evolution of content products has three axes: traffic, technology, and product form
- 使用 protobuf 进行数据序列化
- Remember an experience of interviewing an outsourcing company, should you go?
- Meta首份元宇宙白皮书9大看点,瞄准80万亿美元市场
- 关于华为应用市场审核App无法启动的问题
- MIMO雷达波形设计
- Androd 跳转到google应用市场
猜你喜欢

基于FPGA的DDS任意波形输出

Still saying software testing doesn't have a midlife crisis?9 years of test engineers were eliminated

Recommended open source tools: MegPeak, a high-performance computing tool

打破原则引入SQL,MongoDB到底想要干啥?

吃透Chisel语言.29.Chisel进阶之通信状态机(一)——通信状态机:以闪光灯为例

Remember an experience of interviewing an outsourcing company, should you go?

如何在 TiDB Cloud 上使用 Databricks 进行数据分析 | TiDB Cloud 使用指南

The truth of the industry: I will only test those that have no future, and I panic...

四大首搭加持,美学、安全、操控、效率优势明显,比亚迪海豹售价20.98万元起售!

跳槽前,把自己弄成卷王
随机推荐
百家号取消接口发文功能:插外链获权重被堵死
SLF4J的使用
71页全域旅游综合整体解决方案2021 ppt
A Small Case About Containers
惊艳!京东T8纯手码的Redis核心原理手册,基础与源码齐下
Digital signal processing course lab report (what foundation is needed for digital signal processing)
业内人士真心话:只会测试没有前途的,我慌了......
【Vue.js 3.0源码】KeepAlive 组件:如何让组件在内存中缓存和调度?
The website adds a live 2d kanban girl that can dress up and interact
PyQt5快速开发与实战 9.1 使用PyInstaller打包项目生成exe文件
MPSK抗噪声性能对比(即MPSK标准误码率曲线)
What is the relationship between the construction of smart cities and 5G technology in the new era
去腾讯面试,直接让人出门左拐 :幂等性都不知道!
5. DOM
新时代背景下智慧城市的建设与5G技术有何关联
那些破釜沉舟入局Web3.0的互联网精英都怎么样了?
Eclipse connects to SQL server database "recommended collection"
从实例来看DAO:权力分散的伟大尝试
网络安全——lcx的使用
ROS 导航