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/ 目录 题目描述 题目大意 解题方法 方法一:首尾指针 方法二:头部双指针+双循环 方法三 ...
随机推荐
- Codeforces Round #421 (Div. 2)D - Mister B and PR Shifts(模拟)
传送门 题意 给出n个数,计算在进行n-1次右移中\(min\sum_{i=1}^nabs(p_i-i)\) 分析 我们设置cnt[p[i]-i]为一个数p[i]与它标准位置(如1的标准位置为1)的左 ...
- python __builtins__ staticmethod类 (64)
64.'staticmethod', 返回静态方法 class staticmethod(object) | staticmethod(function) -> method | | Conve ...
- poj1850 Code【组合数学】By cellur925
题意: * 按照字典序的顺序从小写字母 a 开始按顺序给出序列 (序列中都为升序字符串)* a - 1* b - 2* ...* z - 26* ab - 27* ...* az - 51* bc - ...
- 9-26模拟赛 By cellur925
1.计数 (count.cpp/c/pas)时间限制:1s内存限制:256MB[问题描述]给出 m 个数 a[1],a[2],…,a[m]求 1~n 中有多少数不是 a[1],a[2],…,a[m]的 ...
- PHPmail 亲测可用
2017年5月8日9:10:47 1.在模块的配置文件中加入下里面代码,账号最好用126邮箱'THINK_EMAIL' => array( 'SMTP_HOST' => 'smtp.163 ...
- Hexo瞎折腾系列(5) - 使用hexo-neat插件压缩页面静态资源
为什么要压缩页面静态资源 对于个人博客来说,优化页面的访问速度是很有必要的,如果打开你的个人站点,加载个首页就要十几秒,页面长时间处于空白状态,想必没什么人能够忍受得了吧.我个人觉得,如果能把页面的加 ...
- iOS开发隐藏tabBar的问题
开发中遇到第一个页面需要显示tabBar,但是第二个页面不需要显示,当回到第一个页面的时候又需要显示的情况. 在第一个页面跳转到第二个页面的时候需要给第二个页面设置tabBar的隐藏 - (void) ...
- 516 Longest Palindromic Subsequence 最长回文子序列
给定一个字符串s,找到其中最长的回文子序列.可以假设s的最大长度为1000. 详见:https://leetcode.com/problems/longest-palindromic-subseque ...
- [转]EntityFramework之领域驱动设计实践
本文转自:http://www.cnblogs.com/daxnet/archive/2010/11/02/1867392.html Entity Framework之领域驱动设计实践 EntityF ...
- empty 和 isset的区别和联系
empty 和 isset的区别和联系 要说它们的联系,其共同点就是empty()和isset()都是变量处理函数,作用是判断变量是否已经配置,正是由于它们在处理变量过程中有很大的相似性,才导致对它们 ...