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. UVaLive 6585 && Gym 100299F Draughts (暴力+回溯)

    题意:给定一个 10*10的矩阵,每一个W可以跳过一个B向对角走到#并把B吃掉,并且可以一直跳直到不能动为止,现在是W走的时候,问你最多吃几个B. 析:直接暴力+回溯,深搜就好. 代码如下: #pra ...

  2. 简单聊聊ES6-Promise和Async

    前言 本篇博文出至于我的github仓库:web-study,如果你觉得对你有帮助欢迎star,你们的点赞是我持续更新的动力,谢谢! 异步编程在前端开发中尤为常见,从最早的XHR,到后来的各种封装aj ...

  3. 洛谷 - P1012 - 拼数 - 排序

    https://www.luogu.org/problemnew/show/P1012 这道水题居然翻车了,还发现不了bug,服气了.并不是空字符一定比不空要好,要取决于替代它的字符的大小.所以还是直 ...

  4. Android Studio:layout-sw600dp文件夹中创建activity_main.xml

    1.右键res文件夹,新建Android resource directory文件夹 2.在resource type中选择layout  3.将Directory name命名为layout-sw6 ...

  5. python iteration 迭代

    可迭代的类型:list,tuple,dict,str,bytes,bytearray等 一.怎么判断一个对象是否可迭代 >>> from collections import Ite ...

  6. Luogu P3946 ことりのおやつ(小鸟的点心) 【最短路】By cellur925

    题目传送门 日本的冬天经常下雪.不幸的是,今天也是这样,每秒钟雪的厚度会增加q毫米. 秋叶原共有n个地点,编号从1到n.每个地点在开始的时候的积雪高度为hi. 有m条双向道路连接这些地点,它们的长度分 ...

  7. Beta版本冲刺第一天!

    该作业所属课程:https://edu.cnblogs.com/campus/xnsy/SoftwareEngineeringClass2 作业地址:https://edu.cnblogs.com/c ...

  8. PostgreSQL - psql的使用与退出

    PostgreSQL连接数据库的两种方式 PostgreSQL在安装时自带了pgAdmin和psql,pgAdmin是可视化工具,psql是命令行工具.虽然pgAdmin操作起来会更加直观简单,但是在 ...

  9. vs2010中的ADO控件及绑定控件

    要在项目中添加某一个ActiveX控件,则该ActiveX控件必须要注册.由于VS2010中,并没有自动注册ADO及ADO数据绑定控件(Microsoft ADO Data Control,Micro ...

  10. 用JS改变页面中b标签的样式啊 样式的等

    用JS改变页面中b标签的样式啊 样式的等    ,实际上是在标签内加上样式 ,用媒体查询的话 ,不能生效 <!DOCTYPE html> <html lang="en&qu ...