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/ 目录 题目描述 题目大意 解题方法 方法一:首尾指针 方法二:头部双指针+双循环 方法三 ...
随机推荐
- R Factor 如何转为c()
从 R语言本身来说 > f [1] 130015.IB 130013.IB Levels: 130013.IB 130015.IB > data = c(f) > data [1] ...
- python property的2种使用方法
一.property类 class Person(): def __init__(self, name): self.set_name(name) def get_name(self): return ...
- Jquery+ajaxfileupload上传文件
1.说明 ajaxfileupload.js是一款jQuery插件,用于通过ajax上传文件. 下载地址:http://files.cnblogs.com/files/lengzhan/ajaxfil ...
- dwr学习 之 一、dwr+spring的简单集成
1. 环境搭建 我采用的环境为SpringMVC + myBatis + mySql + maven: 关于使用Eclipse构建Maven的SpringMVC项目,请参考: http://limin ...
- jQuery中的.html()和.text()及.val()区别
https://www.cnblogs.com/zhang-xun/p/6766264.html
- C++类 单冒号
1.继承 class Base { }; class Derived : public Base { }; 示列: class Base { public: int a=10; }; class ...
- 洛谷p2234/BZOJ1588 [HNOI2002]营业额统计
题目链接: 洛谷 BZOJ 分析: 好像没有什么好说的就是一个平衡树的板子--唯一要注意的就是这里要找的并不是严格的前驱和后继,因为如果找到之前某一天的营业额和它相等那么差就是0,所以我们仍然在结构体 ...
- 人品问题 树形dp
题目 网上出现了一种高科技产品——人品测试器.只要你把你的真实姓名输入进去,系统将自动输出你的人品指数.把儿不相信自己的人品为0.经过了许多研究后,把儿得出了一个更为科学的人品计算方法.这种方法的理论 ...
- C#中Json的简单处理
命名空间:Windows.Data.Json在Windows Runtime中,可以使用Json类对获取的Json字符串进行操作,相比DataContractJsonSerializer类操作更加直观 ...
- sql server 2012 从删库到跑路
问题: 向sql server 2012单个数据库中导入1500万+条数据的时候,报错: 错误 0xc0202009: 数据流任务 1: SSIS 错误代码 DTS_E_OLEDBERROR.出现 O ...