LeetCode 283. Move Zeroes

计算机视觉之姿态识别(原理+代码实操) 计算机视觉中的姿态识别原理+模型介绍,同时通过代码实操简单的演示基于mediapipe的单人姿态识别。 阅读详情

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:

  1. You must do this in-place without making a copy of the array.
  2. Minimize the total number of operations.
分析:本题不能新建一个数组来辅助排序,首先想到的便是双指针遍历排序数组。
代码:
public class Solution {
    public void moveZeroes(int[] nums) {
        for(int i = 0;i < nums.length;i++){
            if(nums[i] == 0){
                for(int j = i + 1;j < nums.length;j++){
                    if(nums[j] != 0){
                        nums[i] = nums[j];
                        nums[j] = 0;
                        break;
                    }
                }
            }
        }
    }
}
代码注释:1.先定义指针i,2.用指针i来遍历数组,当找到数组中第一个值为0的时候,启用第二个指针j;3.指针j从指针i的下一个值开始遍历,找到第一个不为零的值赋值给指针i指向的数,然后将指针j指向的数置零。4.依次遍历数组中所有数据,得到正确结果。
双指针的另一种表示形式为:
public class Solution {
    public void moveZeroes(int[] nums) {
        // for(int i = 0;i < nums.length;i++){
        //     if(nums[i] == 0){
        //         for(int j = i + 1;j < nums.length;j++){
        //             if(nums[j] != 0){
        //                 nums[i] = nums[j];
        //                 nums[j] = 0;
        //                 break;
        //             }
        //         }
        //     }
        // }
        int i = 0;
        int j = 0;
        while(i < nums.length){
            if(nums[i] == 0 || i == j){
                i++;
            }else{
                if(nums[j] == 0){
                    nums[j] = nums[i];
                    nums[i] = 0;
                    i++;
                }
                j++;
            }
        }
        
    }
}

代码注释:1.先定义两个指针都指向数组的首位,2.取指针i对数组进行遍历,如果和j指向不同数据且i指向的数据不为0时,此时如果j指向的数据不为零,则将i指向的数据赋值给j指针的位置,将i指针的数据置零;如果j指向0,则j++.
图解:
温度PID控制PLC程序 温度PID稳定控制程序,适用于欧姆龙PLC,对其他PLC也具有很大参考价值 立即下载

相关推荐

【硬件】通俗易懂的讲解晶体管(三极管和MOS管)的工作原理

晶体管是一个简单的元器件,可用于构建许多有趣的项目。在本文中,我将用通俗易懂的语言给您讲解晶体管的工作原理,以便您可以在电路设计中更好的使用静态管。一旦你学习这些基础知识,对以后的设计和使用来说,将会变得非常容易。我们将重点介绍两种最常见的晶体管:双极型晶体管(三极管)和MOSFET。晶体管的工作原理其实是类似于电子开关。它可以打开和关闭电路。一个简单的思考方法是将晶体管视为无源的继电器。晶体管类似于继电器,从某种意义上说,您可以使用它来打开和关闭某些东西。但晶体管也可以部分导通,一般在放大电路中使用,这部

1万+

LeetCode 283 move Zeroes

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

老夫编代码就是一把梭 477

Docker启动安装nacos(详情讲解,全网最细)

docker安装nacos保姆级教程

ilvjiale的博客 8万+

LeetCode 283. Move Zeroes 题解

本题是双指针的经典应用问题,主要考察对双指针技巧的理解和使用。通过使用慢指针和快指针,我们可以在原地将所有零移动到数组的末尾,同时保持非零元素的相对顺序。双指针的核心思想是:慢指针指向当前已处理的非零元素的末尾,快指针遍历整个数组,当找到非零元素时,将其移动到慢指针的位置,然后慢指针右移。最后,将慢指针到数组末尾的所有元素设置为零。这种方法不仅适用于移动零问题,还可以应用于许多其他需要原地修改数组的问题,例如删除排序数组中的重复元素、移除元素等。掌握双指针的使用,对于解决这类问题非常重要。

cannonjinx的博客 1748

扣子(coze)案例^第22期:零代码打造小红书竞品监控系统+AI自动生成运营策略建议

本文详细介绍了如何利用扣子(coze)平台零代码搭建小红书竞品监控系统。该系统通过自动化工作流抓取竞品笔记数据,并存入飞书多维表格,结合AI能力自动生成包含可视化分析报告在内的运营策略建议,帮助运营者高效进行数据驱动的市场分析。

weixin_29313239的博客 1005

遇到一个诡异的WiFi网络问题:同一个办公室苹果电脑上不了网,其他电脑都可以

先来把问题掰开揉碎地看清楚:在一个普通的办公室WiFi网络中,所有设备——Windows电脑、安卓手机、甚至是那台老旧的Linux服务器——都能正常联网,唯独苹果电脑(可能是MacBook、iMac或者Mac mini)像被下了魔咒一样,连不上网。这种情况不仅让人抓狂,还透着一股神秘感。为什么偏偏是苹果电脑?难道WiFi路由器对苹果有“偏见”?这些症状都指向一个核心问题:苹果电脑与办公室WiFi之间,似乎存在某种“沟通障碍”。但这障碍到底出在哪里呢?是苹果电脑自己的问题,还是网络设置在“针对”它?

网络技术联盟站 842

力扣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

单片机:实现FFT快速傅里叶变换算法(附完整源码)

单片机:实现FFT快速傅里叶变换算法(附完整源码)

希望我的博客,能帮上你解决学习中工作中所遇到的问题 1103

Leetcode283. Move Zeroes

给定一个数组,将所有 0 移动到数组的末尾。

magic_jiayu的博客 205

【华为OD机试真题】29、组成最大数 | 机试真题+思路参考+代码解析 (C++、Java、Py、C语言、JS)

题目描述 >小组中每位都有一张卡片,卡片上是6位内的正整数,将卡片连起来可以组成多种数字,计算组成的最大数字

KJ.JK 2419

PowerPMAC配置EtherCAT设备

本文详细介绍了在PowerPMAC IDE环境下配置EtherCAT伺服电机的完整流程。主要内容包括:准备工作(连接硬件和获取ESI文件)、通过ESI管理器加载配置文件、网络扫描识别从站设备、变量映射(定义输入输出位置参数)、激活EtherCAT通讯(验证状态字和实际位置)以及相关参数说明。文章还概述了主站配置的8个方面和从站配置的7个选项,重点说明了PDO映射和变量分配机制,为伺服电机的EtherCAT通信配置提供了系统指导。

mahuliang321的博客 975

[leetcode]python 283. Move Zeroes

[leetcode]python 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 exa...

Rh's blog 147

一次携程酒店 phantom-token 逆向实战复盘(附python纯算)

摘要 本文介绍了通过分析携程酒店列表页面的数据接口,使用Python实现抓取的方法。主要内容包括: 目标站点为携程酒店列表页,通过开发者工具发现关键接口fetchHotelList 将浏览器请求转换为Python代码,包含请求头(headers)、Cookies和POST数据 提供完整的请求示例代码,包含日期参数、城市ID等必要信息 特别声明仅供学习交流使用,禁止商业用途 展示了如何获取酒店列表数据,包括排序筛选等参数设置 该方法可用于研究酒店数据采集,但需要注意遵守网站使用条款。

自成背后的博客 3026

【leetcode】283. Move Zeroes

problem 283.Move Zeroes solution 先把非零元素移到数组前面,其余补零即可。 class Solution { public: void moveZeroes(vector<int>& nums) { int j = 0; for(int i=0; i<nums.size(); ...

weixin_30739595的博客 83

lwip-2.0.3

lwip是瑞典计算机科学院(SICS)的Adam Dunkels 开发的一个小型开源的TCP/IP协议栈。实现的重点是在保持TCP协议主要功能的基础上减少对RAM 的占用。

Data-Structures-and-Algorithm-Analysis-in-Java.pdf

作者: Weiss, Mark A. 出版社: Addison Wesley Data Structures and Algorithm Analysis in Java is an “advanced algorithms” book that fits between traditional CS2 and Algorithms Analysis courses. In the old ACM Curriculum Guidelines, this course was known as CS7. This text is for readers who want to learn good programming and algorithm analysis skills simultaneously so that they can develop such programs with the maximum amo...

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

IMSL CNL 7.0.0 (x86-64)Crack

IMSL CNL 7.0.0 (x86-64)Crack intel的算法库破解文件,这是C++版本的破解文件

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的博客 523

VB摄像头监控

该VB模块可对摄像头捕获的画面进行录像等,可在创建的窗口进行控制

LeetCode 283. Move Zeroes (力扣283)

遍历数组,把非零元素移到数组钱前面(从下标零开始),剩下的元素用零补充。

Byte_Me_A_Bit的博客 216

【leetcode】283.move_zeroes

【leetcode】283.Move ZeroesGiven 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],

绯浅yousa的笔记 428

LeetCode 283. 移动零 Move Zeroes

Table of Contents 一、中文版 二、英文版 三、My answer 四、解题报告 一、中文版 给定一个数组 nums,编写一个函数将所有 0 移动到数组的末尾,同时保持非零元素的相对顺序。 示例: 输入: [0,1,0,3,12] 输出: [1,3,12,0,0] 说明: 必须在原数组上操作,不能拷贝额外的数组。 尽量减少操作次数。 二、英文版 Given a...

三妹的博客 288

Leetcode 283 Move Zeroes

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

herosunly的博客 1万+

【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(移动零)题目链接: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

题目: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

皮蛋瘦肉的博客 277

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. 实现: public void moveZeroes(int[] nu...

ccystewart的博客 137

[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], after calling your fu

coder 进阶的专栏 3640

[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], after calling you

ER_Plough的专栏 649
上一篇: LeetCode 226. Invert Binary Tree
下一篇: LeetCode 100. Same Tree
Appletable
博客等级 码龄14年 0粉丝 36原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值