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/ 目录 题目描述 题目大意 解题方法 方法一:首尾指针 方法二:头部双指针+双循环 方法三 ...
随机推荐
- hdoj5813【构造】
2016 Multi-University Training Contest 7 05 真的真的好菜哇... 思路: 暴力. 我对那些到达目的地少的点做硬性规定就是去比他要到达目的地更少的点,这样一来 ...
- hdoj1097
好久没有遇到过这样的题,似乎记得以前完全就是靠规律啊什么的.... 然后刚刚看到,这不就是快速幂取膜就好了嘛- #include <stdio.h> #include <string ...
- win10家庭版安装
https://www.microsoft.com/zh-cn/software-download/windows10ISO https://www.2cto.com/os/201704/621770 ...
- SQL语句合集
UNION (合并) UNION 操作符用于合并两个或多个 SELECT 语句的结果集. 请注意,UNION 内部的 SELECT 语句必须拥有相同数量的列.列也必须拥有相似的数据类型.同时,每条 S ...
- 《编译原理》-用例题理解-自顶向下语法分析及 FIRST,FOLLOW,SELECT集,LL(1)文法
<编译原理>-用例题理解-自顶向下语法分析及 FIRST,FOLLOW,SELECT集,LL(1)文法 此编译原理确定某高级程序设计语言编译原理,理论基础,学习笔记 本笔记是对教材< ...
- PHP 遍历一个文件夹下所有文件和子文件夹的方法
话不多说,直接上代码 <?php function my_dir($dir) { $files = []; if(@$handle = opendir($dir)) { while(($file ...
- hdu 1558 Segment set 计算几何+并查集★
#include <cstdio> #include <iostream> #include <string.h> using namespace std; ; # ...
- iOS NSUserDefaults [setValue:forKey:] [setObject:forKey:] <Objc> setValue(_,forKey:) set(_,forKey) <Swift 3>
前者其实是NSObject都可以调用的KVC方法,后者才是NSUserDefaults的实例方法: 这里参数的类型是nullable id,但是我建议你在传null的时候慎重考虑,否则你的应用就可能面 ...
- php url地址栏传中文乱码解决方法集合
php地址栏传中文$_GET下来后乱码,urlencode和urldecode,iconv,base64_encode等方法,整理基本是常用的了. php地址栏传中文$_GET下来后乱码,urlen ...
- zoj3772Calculate the Function(矩阵+线段树)
链接 表达式类似于斐波那契 但是多了一个变量 不能用快速幂来解 不过可以用线段树进行维护 对于每一个点够一个2*2的矩阵 1 a[i] 1 0 这个矩阵应该不陌生 类似于构造斐波那契的那个数列 ...