[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 ...
随机推荐
- MySQL 查询缓存 QUERY_CACHE
查询缓存(QueryCache)保存查询返回的完整结果.当查询命中该缓存,MySQL会立即返回结果,跳过解析.优化和执行阶段. 官方在特定环境测试结果(官方文档中有详细说明): 1.如果对某表进行简单 ...
- 实现Qt日志功能并输出到文件(使用qInstallMsgHandler安装customMessageHandler)good
原文 http://www.cppblog.com/lauer3912/archive/2011/04/10/143870.html 一.基本分类:qDebug : 调试信息提示qWarning: 一 ...
- 发布Qt Quick桌面应用程序的方法(使得planets在XP上运行)
发布Qt Quick桌面应用程序的方法 Qt是一款优秀的跨平台开发框架,它可以在桌面.移动平台以及嵌入式平台上运行.目前Qt 5介绍程序发布的文章帖子比较少.大家又非常想要知道如何发布Qt应用程序,于 ...
- Redis EXISTS命令耗时过长case排查
一.背景 redis慢日志分析平台上线后,随便看了一下,发现onestore使用的缓存集群,存在大量的EXISTS命令慢查询的情况: 平均每个EXISTS命令需要13ms,最大耗时近20ms.这个结果 ...
- 章节十四、8-javaScript弹框处理
一.javaScript弹框没有id.也没有xpath,在F12开发者选项中无法直接通过鼠标去选择弹窗来确定元素在代码中的位置. 弹窗有两种,一种实只有"确定"按钮的alert类型 ...
- vue.js异步上传文件前后端代码
上传文件前端代码如下: <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type&q ...
- ZooKeeper学习之路(四)—— Java 客户端 Apache Curator
一.基本依赖 Curator是Netflix公司开源的一个Zookeeper客户端,目前由Apache进行维护.与Zookeeper原生客户端相比,Curator的抽象层次更高,功能也更加丰富,是目前 ...
- 简单DI
<?php class DI { private $container; public function set($key, $obj, ...$args) { $this->contai ...
- 【朝花夕拾】Android自定义View篇之(五)Android事件分发机制(上)Touch三个重要方法的处理逻辑
前言 转载请注明,转自[https://www.cnblogs.com/andy-songwei/p/10998855.html]谢谢! 在自定义View中,经常需要处理Android事件分发的问题, ...
- Programming In Lua 第二章
1,lua基本类型:nil,boolean,number,string,userdata,function,thread,table.可以用函数type获取变量的类型. 2,lua中的字符串可以用单引 ...