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. linux mplayer 播放yuv格式 (转载)

    转自:http://blog.csdn.net/ly0303521/article/details/38713791 在mplayer中查看YUV格式的图片或视频,可使用如下命令: mplayer - ...

  2. 20180504-hm

    trail vt. 跟踪,追踪; 拖曳:让…拖或飘扬在后面; 拖沓而行; 跟在…后面; n. 小径; 足迹; 臭迹; 一缕;

  3. E20180412-hm

    overflow  vt. 溢出,淹没; 挤满,充满; 洋溢; 资源过剩;   n. 泛滥,溢出物; 溢流管; 充溢,过多; 超出额;   vi. 泛滥,溢出; 充溢;

  4. IT兄弟连 JavaWeb教程 请求重定向案例

    Check2Servlet类与Output2Servlet类之间为请求转发关系.在web.xml文件中,为Check2Servlet映射的URL为"/check2",为Output ...

  5. MyEclipse中安装SVN插件的最有效的方法

    (1)下载svn插件:http://subclipse.tigris.org/files/documents/906/49209/site-1.8.8.zip (2)解压svn包,找到其中的两个文件夹 ...

  6. (一)搭建自己的SpringBoot后台框架整合MyBatis

    一:通过idea工具构建基础框架 1.  打开idea,左上角File→New→Project, 2.  点击Next 3.  点击Next,配置如下图,这里我们选择数据库MySQL和持久层框架MyB ...

  7. print打印

    print打印输出的优点是简单直接粗暴有效,就是用print()把可能有问题的变量打印出来看看缺点是将来还得删掉它,想想程序里到处都是print(),运行结果也会包含很多垃圾信息 __________ ...

  8. iOS 将navigationItem.titleView设置为自定义UISearchBar (Ficow实例讲解)

    这篇文章可以解决以下问题: 1.将searchBar设置为titleView后,无法调整位置的问题 : 2.searchBar的背景色无法设置为透明色的问题: 3.searchBar输入框内用户输入的 ...

  9. Codeforces 1144G(dp)

    据说这题是种dp的套路?然后被我国红名神仙(南大Roundgod)贪心了,不过思路上非常相近了,故而可贪吧. 设的dp[i][0]是:如果把第i个数放在上升序列里了,那么下降序列结尾的那个最大是多少: ...

  10. mysql replication错误常见处理

    大部分的错误,都是日志错误 日志本身的错误 主日志和中继日志都可能出错,可以使用mysqlbinlog来读一下mysqlbinlog mysql-bin.000007>/dev/null ##只 ...