283. Move Zeroes@python
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的更多相关文章
- 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 ...
- 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 ...
- 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 ...
- 【leetcode】283. Move Zeroes
problem 283. Move Zeroes solution 先把非零元素移到数组前面,其余补零即可. class Solution { public: void moveZeroes(vect ...
- 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 ...
- 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 ...
- 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/ 目录 题目描述 题目大意 解题方法 方法一:首尾指针 方法二:头部双指针+双循环 方法三 ...
随机推荐
- NYOJ7——街区最短路径问题
街区最短路径问题 时间限制:3000 ms | 内存限制:65535 KB 难度:4 描述:一个街区有很多住户,街区的街道只能为东西.南北两种方向.住户只可以沿着街道行走.各个街道之间的间隔相等 ...
- css里关于浏览器的前缀
今天遇到一个比较坑爹的 -moz-box-sizing: border-box; box-sizing' border-box; 一下子有点懵逼,第一个什么鬼??一查,原来是火狐浏览器的前缀.应该 ...
- https://www.luogu.org/blog/An-Amazing-Blog/mu-bi-wu-si-fan-yan-ji-ge-ji-miao-di-dong-xi
https://www.luogu.org/blog/An-Amazing-Blog/mu-bi-wu-si-fan-yan-ji-ge-ji-miao-di-dong-xi
- 51nod 1225:余数之和
传送门 题意 略 分析 \(\sum_i^n(n\%i)=\sum_i^n(n-i*n/i)=n^2-\sum_i^ni*n/i\) \(=\sum r\sum_i^ni[n/i==r]\) 可以证明 ...
- hdoj5805【模拟】
BestCoder Round #86 B NanoApe Loves Sequence 题意: 中文题,题意就算了 思路: 弱的思路- 找一个最大,和第二大,第三大,标记下标(前面那个) ①:如果是 ...
- bzoj 2806: [Ctsc2012]Cheat【广义SAM+二分+dp+单调队列】
把模板串建一个广义SAM 然后在线查询,每次在SAM上预处理出一个a[i]表示i位置向前最多能匹配多长的模板串 二分答案L,dp判断,设f[i]为·~i有几个匹配,转移显然是f[i]=max{f[i- ...
- 一道简单的 Java 笔试题,但值得很多人反思!
前言 面试别人,对我来说是一件新奇事,以前都是别人面试我.我清楚地知道,我在的地域与公司,难以吸引到中国的一流软件人才.所以,我特地调低了期望,很少问什么深入的技术问题,只问一些广泛的.基础的.我只要 ...
- PJzhang:centos7上LNMP方式安装dvwa漏洞测试环境
猫宁!!! 参考链接:https://www.jianshu.com/p/5491ce5bfbac https://www.cnblogs.com/wujuntian/p/8183952.html h ...
- springboot&mybatis 增删改查系列(二)
数据库篇 我的数据库名为data0525,数据表名为user,其中有五列uid,uname,upass,usex,umessage.uid为主键并且自动生成,由于是练习表,所以并没有考虑设计的合理性. ...
- 跟我一起玩Win32开发(21):复制&粘贴&剪贴板操作
我要提醒一下大家,看了我的博文学到的知识,千万不要用于实际开发,不然你会被你的上司骂:“妈的,这些东西哪来的,从来没有人这样做过.”不信你试试,脑细胞被冻结的经理或者技术总监们肯定会这样说的. 如果是 ...