LeetCode--283--移动0
问题描述:
给定一个数组 nums,编写一个函数将所有 0 移动到数组的末尾,同时保持非零元素的相对顺序。
示例:
输入:[0,1,0,3,12]
输出:[1,3,12,0,0]
说明:
- 必须在原数组上操作,不能拷贝额外的数组。
- 尽量减少操作次数。
方法1:检测到为0纪录0的个数,不为0时进行赋值运算nums[i - k] = nums[i]
最后将检测到的0补到nums的最后
class Solution(object):
def moveZeroes(self, nums):
"""
:type nums: List[int]
:rtype: void Do not return anything, modify nums in-place instead.
"""
if len(nums) == 2:
if nums[0] == 0:
nums[0],nums[1] = nums[1],nums[0]
return
else:
return
k = 0
for i in range(len(nums)):
if nums[i] == 0:
k += 1
elif nums[i] != 0 :
nums[i - k] = nums[i]
for j in range(len(nums) - k,len(nums)):
nums[j] = 0
方法2:每次将元素等于0的位置变成[]。
class Solution(object):
def moveZeroes(self, nums):
"""
:type nums: List[int]
:rtype: void Do not return anything, modify nums in-place instead.
"""
i = 0
length = len(nums)
while i < len(nums):
while nums != [] and nums[i] == 0:
nums[i:i+1] = []
if i == len(nums):
break
i += 1
nums += (length - len(nums))*[0]
将方法1改进:
class Solution(object):
def moveZeroes(self, nums):
"""
:type nums: List[int]
:rtype: void Do not return anything, modify nums in-place instead.
"""
length = len(nums)
i = 0
n = 0
while i + n < length:
if nums[i] == 0:
n += 1
del nums[i]
else:
i += 1
nums += [0]*n
2018-09-23 09:07:49
LeetCode--283--移动0的更多相关文章
- 前端与算法 leetcode 283. 移动零
目录 # 前端与算法 leetcode 283. 移动零 题目描述 概要 提示 解析 解法一:暴力法 解法二:双指针法 算法 传入[0,1,0,3,12]的运行结果 执行结果 GitHub仓库 # 前 ...
- [LeetCode] 283. Move Zeroes ☆(移动0到最后)
描述 给定一个数组nums,写一个函数,将数组中所有的0挪到数组的末尾,维持其他所有非0元素的相对位置. 举例: nums = [0, 1, 0, 3, 12], 函数运行后结果为[1, 3, 12, ...
- LeetCode 283. Move Zeroes (移动零)
Given an array nums, write a function to move all 0's to the end of it while maintaining the relativ ...
- leetcode 283. Move Zeroes -easy
题目链接:https://leetcode.com/problems/move-zeroes/ 题目内容: Given an array nums, write a function to move ...
- 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 ...
- 2017-3-9 leetcode 283 287 289
今天操作系统课,没能安心睡懒觉23333,妹抖龙更新,可惜感觉水分不少....怀念追RE0的感觉 =================================================== ...
- [LeetCode] 283. Move Zeroes 移动零
Given an array nums, write a function to move all 0's to the end of it while maintaining the relativ ...
- Java实现 LeetCode 283 移动零
283. 移动零 给定一个数组 nums,编写一个函数将所有 0 移动到数组的末尾,同时保持非零元素的相对顺序. 示例: 输入: [0,1,0,3,12] 输出: [1,3,12,0,0] 说明: 必 ...
- LeetCode 283 Move Zeros
Problem: Given an array nums, write a function to move all 0's to the end of it while maintaining th ...
- LeetCode 283
Move Zeros Given an array nums, write a function to move all 0's to the end of it while maintaining ...
随机推荐
- xlrd、xlwt 操作excel表格详解
转自:https://www.cnblogs.com/jiablogs/p/9141414.html python操作excel主要用到xlrd和xlwt这两个库,即xlrd是读excel,xlwt是 ...
- CentOS7学习记录(工具使用篇)
一. 远程连接终端中文乱码:如xShell 检查当前系统语言:echo $LANG 查看系统安装语言包:locale ,如果包含zh_CN.UTF-8表示已经安装中文语言.如果没有中文包,使用命令 ...
- Dubbo集群配置和官方文档
集群配置: https://blog.csdn.net/zh520qx/article/details/63679908 https://www.cnblogs.com/hd3013779515/p/ ...
- (zhuan) 自然语言处理中的Attention Model:是什么及为什么
自然语言处理中的Attention Model:是什么及为什么 2017-07-13 张俊林 待字闺中 要是关注深度学习在自然语言处理方面的研究进展,我相信你一定听说过Attention Model( ...
- JS 事件绑定、事件监听、事件委托详细介绍
原:http://www.jb51.net/article/93752.htm 在JavaScript的学习中,我们经常会遇到JavaScript的事件机制,例如,事件绑定.事件监听.事件委托(事件代 ...
- awk详解2
7.控制语句 if(condition) {statments} 单分支语句 if(condition) {statments} else {statements}组合语句 while(condito ...
- JS进阶系列之this (call、apply、bind)
在javascript中,this的指向是在执行上下文的创建阶段确定的,其实只要知道不同执行方式下,this的指向分别是是什么,就能很好的掌握this这个让人摸不透的东西. 一.全局执行 全局执行又分 ...
- git Bush应用崩溃If no other git process is currently running, this probably means a git process crashed
问题: 用git Bush提交的时候遇到一个问题,不论做什么操作都遇到下面的错误信息: fatal: Unable to create 'XXXXXXXXX' : File exists. If no ...
- 根据元素取两个list<T>不同
var aa = ltB.FindAll(b => ltA.Any(a => a.PolicyNo == b.ID)); //得出不同 var expectedList = ltB.Exc ...
- VR外包团队:魔幻的三星GearVR
如果问大大菌,移动端什么产品最好,大大菌一定会和你说,是gearvr,对于已经购买了三星2015旗舰手机.又对VR技术感到好奇的用户,建议花99美元买一台Gear VR,绝对会让你物有所值. 在短短的 ...