给定一个数组 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. H5纯前端生成Excel表格

    H5纯前端生成Excel表格方法如下: <!DOCTYPE html> <html> <head> <title></title> < ...

  2. javascript检索某个字符或字符串在源字符串中的位置(下标)

    indexOf()方法 JavaScript中的String对象提供了一个indexOf(searchValue, fromIndex)方法用于检索某个字符或字符串在源字符串中第一次出现的位置(下标) ...

  3. vscode打开文件在同一个tab的问题

    当我们单击或者 cmd+鼠标左键单击打开文件时,有时候是在同一个窗口,有时候是新的窗口,这是啥样呢?   这是因为vscode有 "预览模式" , 当是预览模式时,打开的是当前窗口 ...

  4. Netty与RPC

    一.Netty原理 Netty是一个高性能.异步事件驱动的NIO框架,基于Java NIO提供的API实现.它提供了对TCP.UDP和文件传输的支持,作为一个异步NIO框架,Netty的所有IO操作都 ...

  5. E203数据冲突处理OITF

    流水线的数据冲突分为三类:WAR,RAW,WAW https://wenku.baidu.com/view/e066926d48d7c1c708a14508.html WAR: write after ...

  6. SQLi-LABS Page-4 (Challenges) Less-54-Less-65

    Less-54 union - 1 http://10.10.202.112/sqli/Less-54?id=-1' union select 1,2,group_concat(table_name) ...

  7. 第三方库Mantle的简单实用

    1. 测试时, 可以使用下面这个网址及代码来测试, 里面有模型,数组,以及字典, 还可以有long long 转NSDate,  string 转 int等. NSURL *url = [NSURLU ...

  8. Gradle task简单使用

    还望支持个人博客站:http://www.enjoytoday.cn task是什么 task是gradle构建脚本的最小运行单元,我们通过在gradle脚本中创建task任务,以期完成某个特定的功能 ...

  9. 获取oracle的建表DLL语句

    get_ddl('TABLE','表名','实例名') from dual   select dbms_metadata.get_ddl('TABLE','RMS_CITY','RMS') from ...

  10. MSSQL添加外键

    alter table 需要建立外键的表 with check/nocheck add constraint 外键名字 foreign key (需要建立外键的字段名) references 外键表( ...