leetcode第283题 ,Move Zeroes

第二节:Vben Admin 最新 v5.0 (vben5) + Python Flask 快速入门 - Python Flask 后端开发详解(附源码) 大家好,我是cuiwin!本节内容,我们从零开始开发基于Python(Flask)的后端项目,对接Vue Vben Admin(vben5)。登录mysql数据库执行: 创建数据库。 阅读详情

题目:Move Zeroes
LeetCode:(https://leetcode.com/problems/move-zeroes/description/)

**Given an array nums, write a function to move all 0’s to the end of it while maintaining the relative order of the non-zero elements.

For example, given nums = [0, 1, 0, 3, 12], after calling your function, nums should be [1, 3, 12, 0, 0].

Note:
You must do this in-place without making a copy of the array.
Minimize the total number of operations.

本题的要求是将给定的数组中的0值移动到数组末尾,并且要求不能增加额外的数组进行复制操作。

采用循环遍历,ReplceIndex用于指定被取代的索引位置,若检测到非0数值,就进行取代操作。
遍历完数组之后,再将ReplceIndex到numsSize之间的位置置0。
程序不断优化,共三个函数,leetcode测试案例总时间一直降低,16-9-6ms。
时间复杂度都为O(n);

#include<stdio.h>

void moveZeroes(int *nums , int numsSize);//16ms
void moveZeroes1(int *nums , int numsSize);//9ms
void moveZeroes2(int *nums , int numsSize);//6ms
int TestArr0[] = {0,1,2,3,4,5};
int TestArr1[] = {1,1,2,3,4,5};
int TestArr2[] = {0,1,0,3,0,5};
int TestArr3[] = {0,0,2,3,4,5};
int TestArr4[] = {2,3,6,4,0,0};

int main(int argc ,char *argv[])
{
    int len = 0;


    printf("\r\n********systerm start **************\r\n");

    //test 0
    len = 6;
    printf("\r\ntest0 :\r\n ");

    for(int i = 0 ;i < len ;i++)
    {
        printf("%d\t",TestArr0[i]);
    }
    printf("\r\n");

    moveZeroes1(TestArr0,len);

    for(int i = 0 ;i < len ;i++)
    {
        printf("%d\t",TestArr0[i]);
    }


    //test 1
    len = 6;
    printf("\r\ntest1 :\r\n ");

    for(int i = 0 ;i < len ;i++)
    {
        printf("%d\t",TestArr1[i]);
    }

    printf("\r\n");
    moveZeroes1(TestArr1,len);

    for(int i = 0 ;i < len ;i++)
    {
        printf("%d\t",TestArr1[i]);
    }

    //test 2
    len = 6;
    printf("\r\ntest2 :\r\n ");

    for(int i = 0 ;i < len ;i++)
    {
        printf("%d\t",TestArr2[i]);
    }
    printf("\r\n");

    moveZeroes1(TestArr2,len);

    for(int i = 0 ;i < len ;i++)
    {
        printf("%d\t",TestArr2[i]);
    }
    //test 3
    len = 6;
    printf("\r\ntest3 :\r\n ");

    for(int i = 0 ;i < len ;i++)
    {
        printf("%d\t",TestArr3[i]);
    }
    printf("\r\n");

    moveZeroes1(TestArr3,len);

    for(int i = 0 ;i < len ;i++)
    {
        printf("%d\t",TestArr3[i]);
    }

    //test 4
    len = 6;
    printf("\r\ntest4 :\r\n ");

    for(int i = 0 ;i < len ;i++)
    {
        printf("%d\t",TestArr4[i]);
    }
    printf("\r\n");

    moveZeroes1(TestArr4,len);

    for(int i = 0 ;i < len ;i++)
    {
        printf("%d\t",TestArr4[i]);
    }
    printf("\r\n********systerm finish **************\r\n");
    return 1;
}

void moveZeroes(int *nums , int numsSize)
{
    int index = 0;
    int ReplceIndex = 0;
    int IncFlag = 1;
    if(nums == NULL )
    {
        return;
    }

    for(index = 0; index < numsSize - 1; index++)
    {
        if(IncFlag)
        {
            if(*(nums + index) == 0)
            {
                IncFlag = 0;

            }
            else 
            {
                ReplceIndex++;

            }
        }

        if((*(nums + index) == 0)
            && *(nums + index +1))
        {
            *(nums + ReplceIndex) = *(nums + index +1);
            *(nums + index +1) = 0;
            ReplceIndex++;
        }
    }
}



void moveZeroes1(int *nums , int numsSize)
{
    int index = 0;
    int ReplceIndex = 0;
    int IncFlag = 1;
    if(nums == NULL )
    {
        return;
    }

    for(index = 0; index < numsSize; index++)
    {
        if(*(nums + index))
        {
            *(nums + ReplceIndex) = *(nums + index);
            ReplceIndex++;
        }

    }
    for(index = ReplceIndex ;index < numsSize; index++)
    {
        *(nums + index) = 0;
    }
}

void moveZeroes3(int* nums, int numsSize) 
{
    int index;
    int ReplceIndex = 0;

    if(nums == NULL )
    {
        return;
    }

    for(index = 0; index < numsSize; index++)
    {
        if(nums[index])
        {
            nums[ReplceIndex++] = nums[index];
        }

    }
    for(index = ReplceIndex ;index < numsSize; index++)
    {
        nums[index] = 0;
    }


}
UG\NX二次开发 找不到指定的dlx文件(Specified Dlx file not found) 有群友问:用Ctrl+U运行的时候就报错,找不到指定的dlx文件Specified Dlx file not found。怎么处理。 阅读详情

相关推荐

通达信股本变迁文件(gbbq)解密方法

近年来A股市场跌宕起伏真特么精彩,本人愚钝,被整爆仓,损失惨重 痛定思痛,认为交易这种对纪律要求极高,且必须完全排除情绪影响的勾当,还是应该交给程序来干 数据哪里来呢?当然是拿来主义。。。问券商的交易软件要呗 到处查资料,得知通达信的权息文件数据齐全,不仅含有除权除息数据,还含有限售解禁、增发、可转债上市等股本变化数据 ——这对于某些对成交量变化敏感的交易模型是非常重要的

不甘碌碌老,结庐铸宝刀 2万+

移动零java

给定一个数组 nums,编写一个函数将所有 0 移动到数组的末尾,同时保持非零元素的相对顺序。 示例: 输入: [0,1,0,3,12] 输出: [1,3,12,0,0] 说明: 必须在原数组上操作,不能拷贝额外的数组。 尽量减少操作次数。 来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/move-zeroes 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。 class Solution { public void mov

weixin_43824233的博客 494

CANopen中SDO、PDO字典以及COB-ID理解

CANopen SDO PDO

sunarmy的专栏 2万+

LeetCode——第283题:移动0

题目: 给定一个数组 nums,编写一个函数将所有 0 移动到数组的末尾,同时保持非零元素的相对顺序。 示例: 输入: [0,1,0,3,12] 输出: [1,3,12,0,0] 说明: 必须在原数组上操作,不能拷贝额外的数组。 尽量减少操作次数。 代码: package leetCode; /** * 2018.7.19 移动0 * * @author dhc...

RBreeze的博客 304

【VSCode Python无法格式化代码】

提示:需要Install Formatter…安装插件autopep8,点击install。出现Format Document。

AIFRZ的博客 1万+

上证指数30年k线图_金融占星简单解读上证指数30年历史行情

阅读此文需要一定的占星基础知识,本人对于金融知识了解甚少,如有描述错误请包涵指正!影响大众事件发展趋势的都是外行星,即木星-冥王星这5颗,由于时间跨度长达30年,绘制社会行星的K线图工作量太大,所以只以时代行星对这30年行情的简单解读作为分享,在今后的短期分析中会逐渐加入社会行星,甚至在个股分析上还会加入个人行星。那即使只绘制时代行星,所展现出来的信息也无法在一张图上画出来,否则太乱难以分辨,所以...

weixin_39831242的博客 1686

LeetCode第283题

描述: 给定一个数组nums,编写一个函数将所有0移动到数组的末尾,同时保持非零元素的相对顺序。 示例: 输入: [0,1,0,3,12] 输出: [1,3,12,0,0] 解题思路:遍历数组,将所有非0的数依次放到数组的前面,遍历完成后,假设非0的元素的个数为i个,数组元素为n,再次遍历数组n-i次,将数组中剩余的其他位置置为0,这种思路也是比较简单可行的。代码如下: pu...

Chivalrous man 192

5个企业级SOC平台横向对比:IBM QRadar vs Splunk vs 奇安信NGSOC

本文深度对比了IBM QRadar、Splunk和奇安信NGSOC等五大企业级SOC平台的核心能力,从架构设计、威胁检测、运营效率、成本模型和行业适配度五个维度进行解析。文章特别强调安全运营中心(SOC)在现代企业中的关键作用,并提供了实战数据和优化建议,帮助企业选择最适合自身需求的SOC解决方案。

a1b2c的博客 674

[leetcode] 283. Move Zeroes 解题报告

题目链接:https://leetcode.com/problems/move-zeroes/ Given an array nums, write a function to move all 0's to the end of it while maintaining the relative order of the non-zero elements. For exam

小榕流光的专栏 566

04目标检测-Two-stage的目标检测算法

前面我们对进行了对比分析,同时上一篇我们介绍了传统算法在目标检测的原理和效果,此篇我们记录了我学习目标检测算法的深度,深入讲述一下深度学习算法在目标检测的原理和效果。原文链接:https://blog.csdn.net/qq_41946216/article/details/132800752。版权声明:本文为CSDN博主「爱吃面的猫」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。One-stage(YOLO和SSD系列):直接回归目标位置。

qq_41946216的博客 3659

基于stm32f407VGT6控制WS2812的TIM1+PWM+DMA实现方式

项目中使用到了ws2812灯带,作为产品的外观显示灯,经过开发之后,整理一下,仅供大家参考。 WS2812B是一个集控制电路与发光电路于一体的智能外控LED光源。 其外型与一个5050 LED灯珠相同, 每个元件即为一个像素点。 像素点内部包含了智能数字接口数据锁存信号整形放大驱动电路, 还包含有高精度的内部振荡器和可编程定电流控制部分, 有效保证了像素点光的颜色高度一致。数据协议采用单线归零码的通讯方式, 像素点在上电复位以后, DIN端接受从控制器传输过来的数据, 首先送过来的24bit数据被第一个像素

weixin_43608153的博客 9223

LeetCode 283. 移动零

题意: 给定一个数组 nums, 编写一个函数将所有 0 移动到它的末尾,同时保持非零元素的相对顺序。 例如, 定义 nums = [0, 1, 0, 3, 12],调用函数之后, nums 应为 [1, 3, 12, 0, 0]。 注意事项: 必须在原数组上操作,不要为一个新数组分配额外空间。 尽量减少操作总数。 分析: 先将非0数移到首部,尾部补零 java_code: ...

spring_3_shine的博客 870

433M遥控器无线解码1527协议

433M遥控器无线解码1527协议,定时器轮询法

weixin_58595117的博客 1万+

[东哥的leetcode刷题日记] leetcode 283 : Move Zeroes

leetcode 283 : Move Zeroes 题目链接: https://leetcode-cn.com/problems/move-zeroes/ 难度: 简单 归类 : 数组操作 题目: 给定一个数组 nums,编写一个函数将所有 0 移动到数组的末尾,同时保持非零元素的相对顺序。 说明: 必须在原数组上操作,不能拷贝额外的数组。 尽量减少操作次数。 示例: 示例 1: 输入: [0,1,0,3,12] 输出: [1,3,12,0,0] 解法: 个人解法 双指针法,快指

qq_41847324的博客 349

C# WinForm 基于DirectShow.dll编写的程序,可边预览边保存视频,音频和图片

基于微软的DirectShow.dll的二次开发1.自动识别连接的摄像头跟麦克风且可以对其进行选择(默认选中第一个)2.可录制wmv和avi两种格式的视频并且都带有音频,avi可以录制的分辨率和帧数进行选择,wmv默认进行了淸晰度优化如不喜欢可去除代码中有相关提示3.可在预览点开之后进行拍照,也可边录制视频边拍照4.添加了视频和音频的压缩功能(酌情使用)这次二次开发发现C#对DirectShow的相关信息太少,这次做完也不敢藏私共享出来,还望大家指正

visio安装包

Microsoft Office Visio 是一款专业的制图软件。利用经过更新的形状、协作工具和数据链接图表,创建专业级图表以简化复杂的信息。

【LeetCode刷题之旅】283 Move Zeroes【C++】

给定一个数组 nums,编写一个函数将所有 0 移动到数组的末尾,同时保持非零元素的相对顺序。 示例: 输入: [0,1,0,3,12]输出: [1,3,12,0,0] 说明: 必须在原数组上操作,不能拷贝额外的数组。 尽量减少操作次数。   思路1: 开辟一个新的空间 vector&lt;int&gt; nonZeroElements存放数组中所有非零的值,然后将其赋值给nums向...

Jeloys的博客 297

怀旧版电脑GBC模拟器

再现原版GBC掌机的界面,电脑运行连游戏界面都一样大小,很怀旧,很真实,(英文版)强烈推荐,很小的文件,很难才找到的。

【leetcode每日刷题】283. Move Zeroes

Given an arraynums, write a function to move all0's to the end of it while maintaining the relative order of the non-zero elements. Example: Input: [0,1,0,3,12] Output: [1,3,12,0,0] Note: You m...

菜菜鸟的博客 122

leetcode刷题:283.Move Zeroes(Easy)

地址:https://leetcode.com/problems/move-zeroes/ 应用场景说明 这个题是很Easy的一道题,它的应用场景是在我尝试写小游戏2048时,采用了二维数组存放数字占位,当按上下左右键时,要把所有的数字靠在一边,而所有为0的靠在另一边,这时候用到这个题的解题思路很快能做出来。 解法 目的就是把一个数组中所...

weixin_34337381的博客 147

leetCode刷题记录41_283_Move Zeroes

/*****************************************************问题描述************************************************* Given an array nums, write a function to move all 0's to the end of it while maintaining the...

gunsmoke的专栏 190

LeetCode刷题283. Move Zeroes

题目描述: Given an array nums, write a function to move all 0's to the end of it while maintaining the relative order of the non-zero elements. Example: Input: [0,1,0,3,12] Output: [1,3,12,0,0] 解题思路1...

码上暴富 311

leetcode:283 Move Zeroes-每日编程第五题

Move Zeroes Total Accepted: 36621 Total Submissions: 86626 Difficulty: Easy Given an array nums, write a function to move all 0's to the end of it while maintaining the relative order of

易水的博客 304

LeetCode 283 move Zeroes

开始准备算法面试,毕竟马上找工作。将最近自己在leetcode做的题目记录一下,方便自己复习。同时也提供给有需要的朋友,https://leetcode.com/还没账号的朋友赶快去注册一个吧。 内容将包括:自己第一遍做的想法和类比其他人的优化。 优化主要是参考:慕课网一个实战课程:http://coding.imooc.com/class/82.html。感兴趣的可以看一下 正文: 28

老夫编代码就是一把梭 477

Leetcode Move Zeroes python 移动数列中全部的0 以及sort方法简介

三个思路解决Leetcode Move Zeroes python 移动数列中全部的0 以及sort方法简介

润泽的博客 260

【leetcode刷题】[简单]283. 移动零(move zeroes)-java

移动零 move zeroes题目分析解答 题目 给定一个数组 nums,编写一个函数将所有 0 移动到数组的末尾,同时保持非零元素的相对顺序。 示例: 输入: [0,1,0,3,12] 输出: [1,3,12,0,0] 说明: 必须在原数组上操作,不能拷贝额外的数组。 尽量减少操作次数。 代码模板: class Solution { public void moveZeroes(i...

mikuluna的博客 286

leetcode刷题笔记(Golang)--283. Move Zeroes

283. Move Zeroes Given an array nums, write a function to move all 0’s to the end of it while maintaining the relative order of the non-zero elements. Example: Input: [0,1,0,3,12] Output: [1,3,12,0,0]...

weixin_44555304的博客 235

Java详解LeetCode 热题 100(04):LeetCode 283. 移动零(Move Zeroes)详解

理解题目要求:移动零到数组末尾,保持其他元素相对顺序不变原地操作:不使用额外数组,控制空间复杂度为O(1)优化时间复杂度:从暴力法的O(n²)优化到双指针法的O(n)减少操作次数:通过避免不必要的交换,进一步优化算法处理边界情况:考虑空数组、单元素数组、无零数组等特殊情况。

yk_dflkg的博客 1381

Leetcode刷题74-283. 移动零(C++详细解法!!!)

Come from : [https://leetcode-cn.com/problems/move-zeroes/] 283. Move Zeroes1.Question2.Answer3.大神们的解决方案4.我的收获 1.Question Given an array nums, write a function to move all 0’s to the end of it while ...

qq_40858438的博客 365

LeetCode--283. Move Zeroes

283. Move Zeroes 题目描述: Given an array nums, write a function to move all 0's to the end of it while maintaining the relative order of the non-zero elements. For example, given nums = [0,

BestFSQ的博客 524
上一篇: leetcode第1题 ,Two Sum
下一篇: C程序中内核是如何调用main函数的
皮蛋瘦肉
博客等级 码龄13年 70粉丝 141原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值