LeetCode 283:移动零 Move Zeroes
给定一个数组 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]
说明:
- 必须在原数组上操作,不能拷贝额外的数组。
- 尽量减少操作次数。
Note:
- You must do this in-place without making a copy of the array.
- 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的更多相关文章
- 前端与算法 leetcode 283. 移动零
目录 # 前端与算法 leetcode 283. 移动零 题目描述 概要 提示 解析 解法一:暴力法 解法二:双指针法 算法 传入[0,1,0,3,12]的运行结果 执行结果 GitHub仓库 # 前 ...
- Java实现 LeetCode 283 移动零
283. 移动零 给定一个数组 nums,编写一个函数将所有 0 移动到数组的末尾,同时保持非零元素的相对顺序. 示例: 输入: [0,1,0,3,12] 输出: [1,3,12,0,0] 说明: 必 ...
- [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 ...
- Leetcode 283.移动零
移动零 给定一个数组 nums,编写一个函数将所有 0 移动到数组的末尾,同时保持非零元素的相对顺序. 示例: 输入: [0,1,0,3,12] 输出: [1,3,12,0,0] 说明: 必须在原数组 ...
- python(leetcode)-283移动零
给定一个数组 nums,编写一个函数将所有 0 移动到数组的末尾,同时保持非零元素的相对顺序. 示例: 输入: [0,1,0,3,12] 输出: [1,3,12,0,0] 说明: 必须在原数组上操作, ...
- Leetcode 283.移动零 By Python
思路 我们可以用python的list comprehension来取出所以非0的元素,而且这样取出来会保持原有的相对顺序,再统计先后变化的长度,补上相应的0即可 代码 class Solution( ...
- leetcode:283. Move Zeroes(Java)解答
转载请注明出处:z_zhaojun的博客 原文地址:http://blog.csdn.net/u012975705/article/details/50493772 题目地址:https://leet ...
- 【leetcode】283. Move Zeroes
problem 283. Move Zeroes solution 先把非零元素移到数组前面,其余补零即可. class Solution { public: void moveZeroes(vect ...
- 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 ...
随机推荐
- pymongo和mongoengine安装和使用教程 包含常用命令行和代码示例 | pymongo and mongoengine tutorial on ubuntu 16.04
本文首发于个人博客https://kezunlin.me/post/e88f04e5/,欢迎阅读最新内容! pymongo and mongoengine tutorial on ubuntu 16. ...
- PyCharm 2017: Remote debugging using remote interpreter doesn't work
I set up a remote interpreter and verified that I can run a script using the remote interpreter. Con ...
- oracle中decode函数用法及应用
用法 1.decode(条件,值1,返回值1,值2,返回值2,...值n,返回值n,缺省值) 相当于if else 那种方式. 2.decode(字段或字段的运算,值1,值2,值3) 当字段或字段的运 ...
- tensorflow查看使用的是cpu还是gpu
https://stackoverflow.com/questions/38009682/how-to-tell-if-tensorflow-is-using-gpu-acceleration-fro ...
- go-面向对象编程(上)
一个程序就是一个世界,有很多对象(变量) Golang 语言面向对象编程说明 1) Golang 也支持面向对象编程(OOP),但是和传统的面向对象编程有区别,并不是纯粹的面向对 象语言.所以我们说 ...
- Java生鲜电商平台-微服务架构概述
Java生鲜电商平台-微服务架构概述 单体架构存在的问题 在传统的软件技术架构系统中,基本上将业务功能集中在单一应用内,或者是单一进程中.尽管现代化的软件架构理论以及设计原则已推广多年,但实际技术衍化 ...
- 解决pip使用异常No module named 'pip'
场景 在使用pip进行升级时 python install --upgrade named pip 时提示: No module named pip 注: 博客: https://blog.csdn. ...
- 「SAP技术」SAP 如何看序列号被包在哪些HU里?
「SAP技术」SAP 如何看序列号被包在哪些HU里? 事务代码SE16 ,表名OBJK, 输入物料号,序列号,HeadTable 输入值SER06, 查询结果如下, 根据objlist, 去表ser0 ...
- nRF24L01+启用自动应答ACK及自动重发的实战分享
ACK模式(自动应答)功能的官方说明 当用W_TX_PAYLOAD命令对发送端TX FIFO写数据时,将数据打包后,数据包中包控制字段NO_ACK 标志位复位.接收端接收到一帧有效数据后, 产生RX_ ...
- 029.[转] SSO单点登录的通用架构实现
单点登录的通用架构实现 pphh发布于2018年4月26日 http://www.hyhblog.cn/2018/04/26/single_sign_on_arch/ 目录 1. 什么是单点登录 2. ...