[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 example, given nums = [0, 1, 0, 3, 12], after calling your function, nums should be [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数组保持原顺序
思路:用一个i表示最近一个0点,for遍历中遇到非0点与num[i]交换
class Solution:
def moveZeroes(self, nums):
"""
:type nums: List[int]
:rtype: void Do not return anything, modify nums in-place instead.
"""
i = 0
for x in range(len(nums)):
if(nums[x] != 0):
nums[x], nums[i] = nums[i], nums[x]
i += 1
return nums
leetcode提供的解
思路:将非0元素全交换在前,最后对数组用0补全
class Solution:
def moveZeroes(self, nums):
"""
:type nums: List[int]
:rtype: void Do not return anything, modify nums in-place instead.
"""
k = 0
for x in nums:
if x != 0:
nums[k] = x
k += 1
nums[k:] = [0] * (len(nums) - k)
return nums
[leetcode]python 283. Move Zeroes的更多相关文章
- 【leetcode】283. Move Zeroes
problem 283. Move Zeroes solution 先把非零元素移到数组前面,其余补零即可. class Solution { public: void moveZeroes(vect ...
- leetcode:283. Move Zeroes(Java)解答
转载请注明出处:z_zhaojun的博客 原文地址:http://blog.csdn.net/u012975705/article/details/50493772 题目地址:https://leet ...
- 【LeetCode】283. Move Zeroes 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:首尾指针 方法二:头部双指针+双循环 方法三 ...
- LeetCode之283. Move Zeroes
---------------------------------------------------------------------- 解法一:空间换时间 我使用的办法也是类似于"扫描 ...
- 【leetcode❤python】Move Zeroes
#-*- coding: UTF-8 -*- #filter()函数可以对序列做过滤处理,就是说可以使用一个自定的函数过滤一个序列,#把序列的每一项传到自定义的过滤函数里处理,并返回结果做过滤.最终一 ...
- 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 ...
- LN : leetcode 283 Move Zeroes
lc 283 Move Zeroes 283 Move Zeroes Given an array nums, write a function to move all 0's to the end ...
- 283. Move Zeroes - LeetCode
Question 283. Move Zeroes Solution 题目大意:将0移到最后 思路: 1. 数组复制 2. 不用数组复制 Java实现: 数组复制 public void moveZe ...
- 283. Move Zeroes(C++)
283. Move Zeroes Given an array nums, write a function to move all 0's to the end of it while mainta ...
随机推荐
- LOG4NET图文教程
LOG4NET教程 一:简介 从操作系统到大多数的大型软件,都会有自己的程序运行时的日志跟踪API.因为一旦程序被部署以后,就不太可能再利用专门的调试工具了.然而软件开发人员需要一套强大的日志系统来记 ...
- 使用熔断器仪表盘监控(hystrix)
概述 在 Ribbon 和 Feign 项目增加 Hystrix 仪表盘功能,两个项目的改造方式相同. 在 pom.xml 中增加依赖 <dependency> <groupId&g ...
- java之jdbc学习——QueryRunner
jdbc是ORM框架的基础,但将数据库中的表映射到java对象,并进行增删改查,并不是一件简单的事情. 涉及到jdbc.注解和反射的一些基础知识. 以下内容来自网友的分享,并做了一些增减,作为笔记记录 ...
- Scala 学习之路(八)—— 类和对象
一.初识类和对象 Scala的类与Java的类具有非常多的相似性,示例如下: // 1. 在scala中,类不需要用public声明,所有的类都具有公共的可见性 class Person { // 2 ...
- 汇编入门二 一些概念与PC组件
1.内存:想让CPU工作,必须提供指令与数据,而指令和数据存在于内存中. 2.指令和数据:有点抽象,上书(汇编语言 第二版): 3.存储单元:存储器(内存)被划分为多个存储单元,内个存储单元从0开始顺 ...
- java中更新文件时,指定原文件的编码格式,防止编码格式不对,造成乱码
1.pom中添加引入cpdetector(一个可以自动检测文本编码格式的项目) //pom中添加引入cpdetector(一个可以自动检测文本编码格式的项目) <dependency> & ...
- 解读TIME_WAIT--你在网上看到的大多数帖子可能都是错误的
由于TCP协议整个机制也非常复杂我只能尽可能的在某一条线上来说,不可能面面俱到,如果有疏漏或者对于内容有异议可以留言.谢谢大家. 查看服务器上各个状态的统计数量: netstat -ant | awk ...
- lower_bound 和 upper_bound 功能和用法
以前用这两个函数的时候,简单看了几句别人的博客,记住了大概,用的时候每用一次就弄混一次,相当难受,今天对照着这两个函数的源码和自己的尝试发现:其实这两个函数只能用于 "升序" 序列 ...
- (H5)canvas实现裁剪图片和马赛克功能,以及又拍云上传图片
1.核心功能 此组件功能包含: 图片裁剪(裁剪框拖动,裁剪框改变大小): 图片马赛克(绘制马赛克,清除马赛克): 图片预览.图片还原(返回原图.返回处理图): 图片上传(获取签名.上传图片). 2.核 ...
- hihoCoder 1312:搜索三·启发式搜索(A* + 康托展开)
题目链接 题意 中文题意 思路 做这题的前置技能学习 康托展开 这个东西我认为就是在排列组合问题上的Hash算法,可以压缩空间. A*搜索. 这里我使用了像k短路一样的做法,从最终状态倒回去预处理一遍 ...