给定一个数组 nums,编写一个函数将所有 0 移动到数组的末尾,同时保持非零元素的相对顺序。

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.

示例:

输入: [0,1,0,3,12]
输出: [1,3,12,0,0]

说明:

  1. 必须在原数组上操作,不能拷贝额外的数组。
  2. 尽量减少操作次数。

Note:

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

解题思路:

​ 千万不要被题目局限了思维!题目让把所有0移到末尾,如果你的思路是遇零与末尾数字交换位置,然后还需要把非零数字排序,那么就被带偏了。

​ 换个思路,把非 0 数字前移,不去管数字 0

定义两个指针:指针 i 直接遍历数组遇到非 0 数字把该数字赋值给指针 j 所在的索引,索引 j 自增 1,i继续遍历。

这样遍历完之后,数组索引从0到 j 之间的数值即为所求得保持非零元素的相对顺序,而 j 之后的数值只需要全部赋值 0 即可。

Java:

class Solution {
public void moveZeroes(int[] nums) {
int numsLen = nums.length;
if (numsLen < 1) return;//数组长度小于一直接返回
int j = 0;
for (int i = 0; i < numsLen; i++) {//遍历数组
if (nums[i] != 0) {//如果该数不为0
nums[j++] = nums[i];//赋值给索引j
}
}
while (j < numsLen) nums[j++] = 0;//把从j到末尾所有数字赋值0
}
}

Python3:

class Solution:
def moveZeroes(self, nums: List[int]) -> None:
"""
Do not return anything, modify nums in-place instead.
"""
if len(nums) < 1:
return
j = 0
for num in nums:
if num != 0:
nums[j] = num
j += 1
for i in range(j, len(nums)):
nums[i] = 0

如果题目不限制在原数组上操作,用python一行可解决:

nums = [i *for* i in nums *if* i != 0]+[i *for* i in nums *if* i == 0]

公众号: 爱写bug(ID:iCodeBugs)

LeetCode 283:移动零 Move Zeroes的更多相关文章

  1. 前端与算法 leetcode 283. 移动零

    目录 # 前端与算法 leetcode 283. 移动零 题目描述 概要 提示 解析 解法一:暴力法 解法二:双指针法 算法 传入[0,1,0,3,12]的运行结果 执行结果 GitHub仓库 # 前 ...

  2. Java实现 LeetCode 283 移动零

    283. 移动零 给定一个数组 nums,编写一个函数将所有 0 移动到数组的末尾,同时保持非零元素的相对顺序. 示例: 输入: [0,1,0,3,12] 输出: [1,3,12,0,0] 说明: 必 ...

  3. [Swift]LeetCode283. 移动零 | Move Zeroes

    Given an array nums, write a function to move all 0's to the end of it while maintaining the relativ ...

  4. Leetcode 283.移动零

    移动零 给定一个数组 nums,编写一个函数将所有 0 移动到数组的末尾,同时保持非零元素的相对顺序. 示例: 输入: [0,1,0,3,12] 输出: [1,3,12,0,0] 说明: 必须在原数组 ...

  5. python(leetcode)-283移动零

    给定一个数组 nums,编写一个函数将所有 0 移动到数组的末尾,同时保持非零元素的相对顺序. 示例: 输入: [0,1,0,3,12] 输出: [1,3,12,0,0] 说明: 必须在原数组上操作, ...

  6. Leetcode 283.移动零 By Python

    思路 我们可以用python的list comprehension来取出所以非0的元素,而且这样取出来会保持原有的相对顺序,再统计先后变化的长度,补上相应的0即可 代码 class Solution( ...

  7. leetcode:283. Move Zeroes(Java)解答

    转载请注明出处:z_zhaojun的博客 原文地址:http://blog.csdn.net/u012975705/article/details/50493772 题目地址:https://leet ...

  8. 【leetcode】283. Move Zeroes

    problem 283. Move Zeroes solution 先把非零元素移到数组前面,其余补零即可. class Solution { public: void moveZeroes(vect ...

  9. LeetCode Javascript实现 283. Move Zeroes 349. Intersection of Two Arrays 237. Delete Node in a Linked List

    283. Move Zeroes var moveZeroes = function(nums) { var num1=0,num2=1; while(num1!=num2){ nums.forEac ...

随机推荐

  1. 05爬虫-requests模块基础(2)

    今日重点: 1.代理服务器的设置 2.模拟登陆过验证码(静态验证码) 3.cookie与session 4.线程池 1.代理服务器的设置 有时候使用同一个IP去爬取同一个网站,久了之后会被该网站服务器 ...

  2. Linux中Swap与Memory内存简单介绍

    1.背景介绍   这篇文章介绍一下Linux中swap与memory.对于memory没什么可说的就是机器的物理内存,读写速度低于cpu一个量级,但是高于磁盘不止一个量级.所以,程序和数据如果在内存的 ...

  3. 使用Redis实现最近N条数据的决策

    前言 很多时候,我们会根据用户最近一段时间的行为,做出一些相应的策略,从而改变系统的运动轨迹. 举个简单的例子来说明一下: 假设A公司现在有两个合作伙伴(B和C),B和C都是提供天气数据的,现在A公司 ...

  4. 数据库——SQL-SERVER练习(1)连接与子查询

    一.实验准备 1.复制实验要求文件及“CREATE-TABLES.SQL”文件, 粘贴到本地机桌面. 2.启动SQL-SERVER服务. 3. 运行查询分析器, 点击菜单<文件>/< ...

  5. 几个高逼格 Linux 命令!

    作者:忧郁巫师 https://dwz.cn/A1FOjLXk 1. sl 命令 你会看到一辆火车从屏幕右边开往左边…… 安装 $ sudo apt-get install sl 运行 $ sl 命令 ...

  6. Oracle:Redhat 7.4+Oracle Rac 11.2.0.4 执行root.sh报错处理

    一.报错信息 二.原因分析 因为RHEL 7使用systemd而不是initd运行进程和重启进程,而root.sh通过传统的initd运行ohasd进程 三.解决办法 在RHEL 7中ohasd需要被 ...

  7. PHP面试题2019年奇虎360面试题及答案解析

    一.单选题(共29题,每题5分) 1.以下代码 a.php 输出的结果是? a.php 的代码如下: b.php的代码如下: A.foo in a B.什么也不输出 C.报错 D.foo in b 参 ...

  8. JavaWeb之Fliter & Listener

    Fliter & Listener Listener 监听器 作用 监听某一事件的发生.状态的改变. 监听器内部实现机制 接口回调 接口回调 A在执行循环,当循环到5的时候, 通知B. 事先先 ...

  9. react网页版聊天|仿微信、微博web版|react+pc端仿微信实例

    一.项目介绍 基于react+react-dom+react-router-dom+redux+react-redux+webpack2.0+nodejs等技术混合开发的仿微信web端聊天室react ...

  10. js的运用

    JS数组的4种声明方法: //第一种 ,,,,] console.log(A) //第二种 var B=new Array(); B[]= B[]= B[]= console.log(B)//第三种 ...