问题描述:

给定一个数组 nums,编写一个函数将所有 0 移动到数组的末尾,同时保持非零元素的相对顺序。

示例:

输入: [0,1,0,3,12]
输出: [1,3,12,0,0]

说明:

  1. 必须在原数组上操作,不能拷贝额外的数组。
  2. 尽量减少操作次数。

方法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的更多相关文章

  1. 前端与算法 leetcode 283. 移动零

    目录 # 前端与算法 leetcode 283. 移动零 题目描述 概要 提示 解析 解法一:暴力法 解法二:双指针法 算法 传入[0,1,0,3,12]的运行结果 执行结果 GitHub仓库 # 前 ...

  2. [LeetCode] 283. Move Zeroes ☆(移动0到最后)

    描述 给定一个数组nums,写一个函数,将数组中所有的0挪到数组的末尾,维持其他所有非0元素的相对位置. 举例: nums = [0, 1, 0, 3, 12], 函数运行后结果为[1, 3, 12, ...

  3. 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 ...

  4. leetcode 283. Move Zeroes -easy

    题目链接:https://leetcode.com/problems/move-zeroes/ 题目内容: Given an array nums, write a function to move ...

  5. 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 ...

  6. 2017-3-9 leetcode 283 287 289

    今天操作系统课,没能安心睡懒觉23333,妹抖龙更新,可惜感觉水分不少....怀念追RE0的感觉 =================================================== ...

  7. [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 ...

  8. Java实现 LeetCode 283 移动零

    283. 移动零 给定一个数组 nums,编写一个函数将所有 0 移动到数组的末尾,同时保持非零元素的相对顺序. 示例: 输入: [0,1,0,3,12] 输出: [1,3,12,0,0] 说明: 必 ...

  9. 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 ...

  10. LeetCode 283

    Move Zeros Given an array nums, write a function to move all 0's to the end of it while maintaining ...

随机推荐

  1. 尚硅谷面试第一季-12Linux常用服务类相关命令

    课堂重点: 实操命令及运行结果: (centos 6) service network status chkconfig --list chkconfig --level 5 network off ...

  2. rsync命令解析

    !rsync同步模式sync在进行同步或备份时,使用远程shell,或TCP连接远程daemon,有两种途经连接远程主机.shell模式,不需要使用配置文件,也不需要启动远端rsync.远程传输时一般 ...

  3. (转)开源项目miaosha(下)

    石墨文档:https://shimo.im/docs/2XlwliBQAYsKCHbq/ (二期)20.开源秒杀项目miaosha解读(下) [课程20]jmeter.xmind81.5KB [课程2 ...

  4. better-scroll影响vue中radio和checkbox的双向数据绑定

    我的解决办法:radio <input v-model="answer" type="radio" name="answer" val ...

  5. LightOJ 1268 Unlucky Strings(KMP+矩阵乘法+基础DP)

    题意 给出字符串的长度 \(n\) ,以及该字符串是由哪些小写字母组成,现给出一个坏串 \(S\) ,求存在多少种不同的字符串,使得其子串不含坏串. \(1 \leq n \leq 10^9\) \( ...

  6. mybatis配置文件resultMap标签的使用

    本文为博主原创,未经允许不得转载: resultMap标签是为了映射select查询出来结果的集合,其主要作用是将实体类中的字段与 数据库表中的字段进行关联映射. 注意:当实体类中的字段与数据库表中的 ...

  7. 七、面向对象编程(OOP)

    面向对象编程:一种程序设计思想.OOP把对象作为程序的基本单元,一个对象包含了数据和操作数据的函数. 1.类(class) class:自定义的对象数据类型.基于类创建多个对象,每个对象具备类的通用行 ...

  8. Codeforces 781B. Innokenty and a Football League

    题目链接:http://codeforces.com/contest/781/problem/B 去tmd 2-SAT 直接贪心就可以过去了,优先选择第二种情况. 然而....可以被叉掉(数据水了) ...

  9. WebSocket 教程

    转载自:http://www.ruanyifeng.com/blog/2017/05/websocket.html WebSocket 是一种网络通信协议,很多高级功能都需要它. 本文介绍 WebSo ...

  10. Tp5.1使用导出Excel

    composer require phpoffice/phpexcel 不管它的警告,都能用的. use PHPExcel; use PHPExcel_IOFactory; public static ...