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.

Example:

Input: [,1,,3,12]
Output: [1,3,12,0,0]

原题地址: Move Zeroes

难度: Easy

题意:将数组内的0数移到数组末尾,非0值需要保证相对位置不变,比如上面例子中,非0值的顺序为 1, 3, 12 ,结果也得保证这个顺序

思路1:有点像冒泡,将数组0一个一个的移到末尾

代码描述:

循环{
如果值为0
     循环{
  当前值与下一个值交换
     }
}

代码:

n = len(nums)
i = 0
while i < n:
if nums[i] == 0:
j = i
while j < n-1:
nums[j], nums[j+1] = nums[j+1], nums[j]
j += 1
n -= 1
i -= 1
  i += 1

时间复杂度:O(n^2)

空间复杂度:O(1)

思路2:

先想一想,对数组进行一次遍历可以知道什么信息或者将数组改变成什么样子?

(1)可以找到任意一个数的位置(快排),存在数字交换

(2)找到最大(小)值(冒泡)

(3)统计各个数的个数

再看看快排(原地):

2    8    7    1    3    5    6     
将4作为分割点
2    1    3    4    8    7    5    6

一次排序之后,元素的相对位置没有改变,可以模仿这种写法,将非0数都移到数组的前面

代码:

class Solution(object):
def moveZeroes(self, nums):
"""
:type nums: List[int]
:rtype: void Do not return anything, modify nums in-place instead.
"""
n = len(nums)
idx = -1
for i in range(n):
if nums[i] != 0:
idx += 1
nums[idx], nums[i] = nums[i], nums[idx]

时间复杂度:O(n)

空间复杂度:O(1)

283. Move Zeroes@python的更多相关文章

  1. Leetcode 283 Move Zeroes python

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

  2. 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 ...

  3. 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 ...

  4. 【leetcode】283. Move Zeroes

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

  5. 283. Move Zeroes【easy】

    283. Move Zeroes[easy] Given an array nums, write a function to move all 0's to the end of it while ...

  6. 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 ...

  7. 283. Move Zeroes - LeetCode

    Question 283. Move Zeroes Solution 题目大意:将0移到最后 思路: 1. 数组复制 2. 不用数组复制 Java实现: 数组复制 public void moveZe ...

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

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

  9. 【LeetCode】283. Move Zeroes 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:首尾指针 方法二:头部双指针+双循环 方法三 ...

随机推荐

  1. SpringBoot第九篇:整合Spring Data JPA

    作者:追梦1819 原文:https://www.cnblogs.com/yanfei1819/p/10910059.html 版权声明:本文为博主原创文章,转载请附上博文链接! 前言   前面几章, ...

  2. E20170426-gg

    recursive   adj. 回归的,递归的; removal    n. 除去; 搬迁; 免职; 移走; customize vt. 定制,定做; 按规格改制;

  3. mongodb 安装问题

    重新安装一台机器时出现头疼的问题,老是说什么 dbpath 不存在 结果最后发现是没有写 mongodb.log 这个log文件名 1. 创建 datas 文件夹  e:\mongodb\datas ...

  4. python 生成器 generator

    一.生成器定义 通过列表生成表达式,我们可以直接创建一个列表.但是,受到内存限制,列表容量肯定是有限的.所以,如果列表元素可以按照某种算法推算出来,那我们是否可以在循环的过程中不断推算出后续的元素呢? ...

  5. AppStore 审核拒绝原因记录

    此文仅记录审核app被拒绝的原因 1.未提供充值功能,app中出现vip或者会员图标文字 被拒 解决,隐藏或取消该图标或文字 2.第三方登录,需要跳转到第三方app登录 被拒 解决,审核时隐藏第三方登 ...

  6. 【模板】c++动态数组vector

    相信大家都知道$C$++里有一个流弊的$STL$模板库.. 今天我们就要谈一谈这里面的一个容器:动态数组$vector$. $vector$实际上类似于$a[]$这个东西,也就是说它重载了$[]$运算 ...

  7. 关于能ping通服务器但ssh登陆不上的问题

    一般来说能ping通服务器说明网没问题 这是可以查看一下防火墙的设置和ip的屏蔽设置 /etc/init.d/iptables status  查看防火墙状态 vim /etc/hosts.allow ...

  8. 用css来修饰页面文本

    <html> <head> <title>修饰文本字体</title> <style type="text/css"> ...

  9. React.js 的 context

    这一节我们来介绍一个你可能永远用不上的 React.js 特性 —— context.但是了解它对于了解接下来要讲解的 React-redux 很有好处,所以大家可以简单了解一下它的概念和作用. 在过 ...

  10. Spring-bean(零)

    内容提要:红为1,黄2,绿3 -----配置形式:基于xml文件的方式:基于注解的方式 -----Bean的配置方式:通过全类名(反射),通过工厂方法(静态工厂方法&实例工厂方法),Facto ...