Leetcode 283. Move Zeroes java解法

Innovus ECO技巧:用setEcoMode+ecoAddRepeater实现批量buffer替换的5个关键步骤 本文详细介绍了在Cadence Innovus工具中,利用setEcoMode与ecoAddRepeater命令实现高效、批量Buffer/Inverter替换的工程化策略。通过启用批处理模式、构建可维护的替换规则、掌握高级定位技巧以及规避布线陷阱等5个关键步骤,帮助芯片设计工程师在ECO阶段大幅提升修改效率与质量,确保设计稳定签核。 阅读详情

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]

Note:

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

Approach #1 Bubble sort,我的解法,慢

class Solution {
    public void moveZeroes(int[] nums) {
        for(int i=0;i<nums.length;i++){
            for(int j=i+1;j<nums.length;j++){
                if(nums[i]==0&&j<nums.length){
                    int temp=nums[i];
                    nums[i]=nums[j];
                    nums[j]=temp;
                }
            }
        }   
    }
}

时间复杂度O(N^2)

空间复杂度O(1)

改进:

public void moveZeroes(int[] nums) {

    int j = 0;
    for(int i = 0; i < nums.length; i++) {
        if(nums[i] != 0) {
            int temp = nums[j];
            nums[j] = nums[i];
            nums[i] = temp;
            j++;
        }
    }
}
}

Approach #2 Insert Index

// Shift non-zero values as far forward as possible
// Fill remaining space with zeros

public void moveZeroes(int[] nums) {
    if (nums == null || nums.length == 0) return;        

    int insertPos = 0;
    for (int num: nums) {
        if (num != 0) nums[insertPos++] = num;
    }        

    while (insertPos < nums.length) {
        nums[insertPos++] = 0;
    }
}

先把非零元素放到应该在的地方,再根据array长度算出结尾需要补的零,补在后面即可。

时间复杂度O(N)

空间复杂度O(1)

Approach #3 Snowball

class Solution {
     public void moveZeroes(int[] nums) {
        int snowBallSize = 0; 
        for (int i=0;i<nums.length;i++){
	        if (nums[i]==0){
                snowBallSize++; 
            }
            else if (snowBallSize > 0) {
	            int t = nums[i];
	            nums[i]=0;
	            nums[i-snowBallSize]=t;
            }
        }
    }
}

像滚雪球一样,把0 都滚到一起,然后最左侧的零和非零元素对调。

时间复杂度O(N)

空间复杂度O(1)

【AI Agent教程】各种Agent开发框架都是如何实现ReAct思想的?深入源码学习一下 驱动大模型有很多种方式,例如纯Prompt方式、思维链方式、ReAct方式等。ReAct 方式是 AI Agent 最常用的实现思路之一,它强调在执行任务时结合推理(Reasoning)和行动(Acting)两个方面,使得Agent能够在复杂和动态的环境中更有效地工作。 阅读详情

相关推荐

吃透 AM32 无人机电调:从源码架构到工作原理的全方位解析(附实践指南)(中)

介绍了三相电机控制中的SVPWM算法实现及保护机制设计。SVPWM部分详细解析了电压矢量扇区判断、占空比计算和过调制保护,通过限制占空比范围(0-100%)防止电机失控,并提供了完整的代码实现。保护模块则重点分析了过流、过压/欠压、过温三种保护机制,采用分级处理策略(硬保护停机/软保护降功率)确保系统安全。此外,文章还介绍了参数管理模块在STM32 Flash中的存储方案,包括参数定义、数据结构和读写操作,为电机控制系统提供了完整的解决方案。

bother3000的博客 1388

Leetcode 283.移动零(Java)

思路:构建一个需要移动步长的数组,实现空间换时间,达到O(n)的时间复杂度。

zc1378的博客 382

OpenManus本地部署实战:从源码编译到浏览器路径优化全解析

本文详细解析了OpenManus本地部署的全过程,从源码编译到浏览器路径优化。涵盖环境准备、源码获取、模型配置、依赖安装等关键步骤,特别针对Windows系统下的chrome.exe路径修改提供了实用解决方案,帮助开发者高效完成部署并优化性能。

weixin_28826545的博客 378

LeetCode(283题)--移动零[JAVA]

LeetCode(283题)--移动零[JAVA](两种解题思路+解题代码详解+心得体会)

心态的博客 496

一文搞定 GitHub SSH Key:生成、配置、好处全攻略

本文详细介绍了如何配置GitHub SSH Key,实现免密安全认证。主要内容包括:SSH协议的四大优势(免密登录、安全性高、稳定省心、支持自动化);Git身份配置步骤;检查本地SSH Key;生成RSA密钥对;添加密钥到SSH代理;复制公钥到GitHub;测试连接。配置完成后,开发者可通过SSH地址克隆仓库,实现push/pull全程免密操作。该方案显著提升Git使用效率,同时保障安全性,是开发者必备技能。

qq_62904883的博客 956

从 0 到 1,实战 Power BI 电商销售可视化看板全流程

本文详细介绍了从零开始使用Power BI构建电商销售可视化看板的全流程,涵盖数据准备、建模、可视化设计及高级功能应用。通过实战案例展示如何将复杂销售数据转化为直观图表,帮助电商从业者快速掌握销售趋势、热销产品和用户画像等关键信息,提升数据分析效率。

weixin_29061041的博客 483

LeetCode 初级 - 移动零

移动零 给定一个数组 nums,编写一个函数将所有 0 移动到数组的末尾,同时保持非零元素的相对顺序。 示例: 输入: [0,1,0,3,12] 输出: [1,3,12,0,0] 说明: 必须在原数组上操作,不能拷贝额外的数组。 尽量减少操作次数。 分析 维护index,记录非0数字应当存放的位置 代码 class Solution { public v...

whdAlive 's blog 1841

点云特征提取的实战对比:Voxel-based 与 Pillar-based 在自动驾驶中的表现

本文深入对比了自动驾驶中点云特征提取的两种主流方法:Voxel-based与Pillar-based。Voxel-based方法通过三维体素化保留完整几何信息,检测精度高,但计算消耗大;Pillar-based方法将点云压缩为二维柱状特征图,牺牲部分垂直细节以换取极高的处理速度,更适合实时性要求高的量产场景。文章结合实战数据,为不同算力、精度与工程需求的场景提供了清晰的选型指南。

weixin_29045001的博客 222

Leetcode:283. Move Zeroes (JAVA)

【问题描述】 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

莫名的程序猿 434

廉价却有效?ESD防护中的电容

摘要:在成本敏感型电子产品设计中,多层陶瓷电容(MLCC)可作为TVS二极管的替代方案用于静电放电(ESD)防护。其工作原理是通过电容充放电特性吸收静电能量,而非电压钳位。计算表明100nF/50V MLCC可有效缓冲8kV静电冲击,但存在吸收能量有限、无自恢复等局限。建议将MLCC作为一级缓冲措施,配合TVS等器件形成多级防护,并优先选择X7R材质、100nF容值、50V耐压的0402/0603封装MLCC。该方案在低速信号和电源引脚防护中具有显著成本优势,但不能完全替代专业ESD抑制器。

硬核科技 430

Power domain 划分该如何下手

假如PD1比PD3更always on的话,我们只需要插入普通buffer即可,因为PD3 on,PD1也肯定是ON,PD1 off,PD3肯定时off,PD1能cover PD3的电源情况,因此使用普通buffer也可,但是处于省电角度,我们还是使用AO buffer;在我们做多power domain时,在切分power domain时,需要谨慎,稍有不慎,就会出现congestion以及timing问题,那问题来了,我们在切分power domain时有什么考究么,今天小编就和大家一起讨论讨论。

芯冰乐 206

leetcode | 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, 1, 0, 3, 12], after calling you

weizaishouex2010的专栏 415

Cadence OrCAD Capture原理图中为功能模块分配ROOM的方法

本文详解介绍原理图设计阶段为原理图模块分配ROOM的方法。

fydar的博客 1345

move zeroes --java 解法 同leetcode 283

题目是我做一个公司的笔试题目出现的,后来发现leetcode 上也有。 参考:leetcode 283 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...

lonyw的博客 616

Probability and Statistics - 4th edition

Probability and Statistics - 4th edition

jdk-8u144-linux-x64.tar.gz.

该资源为搭建Linux系统Java环境用,资源的后缀为tar.gz格式的压缩包

Leetcode 283 Move Zeroes

题目描述 给定一个数组 nums,编写一个函数将所有 0 移动到数组的末尾,同时保持非零元素的相对顺序。 示例: 输入: [0,1,0,3,12] 输出: [1,3,12,0,0] 说明: 必须在原数组上操作,不能拷贝额外的数组。 尽量减少操作次数 思路分析之基本套路 在不申请新的数组的前提下,遍历一遍数组,而且只使用一个索引变量,是无法求解这个题目。 既然无法使用一个索引变量,那我们是否能使用...

herosunly的博客 1万+

温度PID控制PLC程序

温度PID稳定控制程序,适用于欧姆龙PLC,对其他PLC也具有很大参考价值

leetcode 283. Move Zeroes

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.For example, given nums = [0, 1, 0, 3, 12

Decorator2015的博客 462

LeetCode Top 100 Liked Questions 283. Move Zeroes (Java版; Easy)

welcome to my blog LeetCode Top 100 Liked Questions 283. Move Zeroes (Java版; Easy) 题目描述 Given an array nums, write a function to move all 0's to the end of it while maintaining the relative order of ...

littlehaes的博客 186

力扣LeetCode 283. Move Zeroes题解java

题目 给定一个数组 nums,编写一个函数将所有 0 移动到数组的末尾,同时保持非零元素的相对顺序。 示例: 输入: [0,1,0,3,12] 输出: [1,3,12,0,0] 说明: 必须在原数组上操作,不能拷贝额外的数组。 尽量减少操作次数。 来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/move-zeroes 解题思路 使用指针count来指向数组中第1个0。因为考虑到当多个0在一块时,只需要让第1个0与第1个非0元素交换计科,而不用交换每

mrhanzhou5273的博客 305

[Leetcode] 283. Move Zeroes

link: https://leetcode.com/problems/move-zeroes/ Solution: two pointers class Solution { public void moveZeroes(int[] nums) { int len = nums.length; int left = 0; for(int i...

Renoler的博客 164

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

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

yk_dflkg的博客 1375

leetcode 283: Move Zeroes

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

西施豆腐渣 8646

LeetCode 283. Move Zeroes(移动数字0)

原题网址: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

James Pan 537

算法:移动零283. Move Zeroes

283. Move Zeroes Given an integer array nums, move all 0’s to the end of it while maintaining the relative order of the non-zero elements. Note that you must do this in-place without making a copy of the array. Example 1: Input: nums = [0,1,0,3,12] Output:

AI架构师易筋 376

【Leetcode-Easy-283】Move Zeroes

【Leetcode-Easy-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, 1, 0,

LYZ 789

LeetCode 283. Move Zeroes

LeetCode 283. Move Zeroes (easy) 主要知识点:快慢指针 优先级:4(最优解有点绕脑子)

温迪今天刷题了吗 375

【LeetCode】283. Move Zeroes(移动零)

【LeetCode】283. Move Zeroes(移动零)题目链接:https://leetcode.com/problems/move-zeroes/description/难度:Easy问题描述:Given an array nums, write a function to move all 0's to the end of it while maintaining the relat...

Mr.王同学 736

LeetCode笔记:283. Move Zeroes

将数组中的所有“0”移动到最后的游戏。多种解法,希望大家来解答一下我的疑问

Minecraft 1281
上一篇: Leetcode 1119. Remove Vowels from a String, Java解法
下一篇: Leetcode[498, 54, 118 ] Array 专题(2) 2D Array
YuanTheCoder
博客等级 码龄9年 2粉丝 34原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值