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 ...
随机推荐
- volatile的陷阱
对于volatile关键字,大部分C语言的教程都是一笔带过,并没有做太深入的分析,所以这里简单的整理了一些 关于volatile的使用注意事项.实际上从语法上来看volatile和const ...
- centos7之安装wordpress
wordpress安装教程如下: mysql安装可以参考我的博客园Centos构建Java环境:https://www.cnblogs.com/youcong/p/9118753.html 1.安装a ...
- 配置maven默认jdk版本
1.在setting.xml中配置.对所有通过该配置文件构建的maven项目有效. <profile> <id>jdk-1.8</id> <activatio ...
- How to install Maven on Windows
To install Apache Maven on Windows, you just need to download the Maven’s zip file, and Unzip it to ...
- CentOS7下Docker中构建可以自动发布到项目的Tomcat容器
步骤 下载镜像 搜索相应的镜像文件:docker search 'tomcat' 如下 下载镜像:docker pull tomcat:7,如下图 PS:后面的数字代表tomcat的版本,可以自己选择 ...
- Flutter提升开发效率的一些方法和工具
Flutter的环境搭配完之后,就开始Flutter的开发,下面的一些工具和方法,可以省下一些时间. 自己在用的,暂时想到的,就是这些了,总结一下. 1.JSON解析快速生成实体类 根据接口返回的数据 ...
- IP/子网掩码/网关/广播地址
判断两个IP是否处于同一子网(网段) 广播地址的作用是什么? 每天一个linux命令(52):ifconfig命令 什么是IP地址.子网掩码.路由和网关
- P3211 [HNOI2011]XOR和路径
思路 看到异或,容易联想到二进制位之间是相互独立的,所以可以把问题变成每个二进制位为1的概率再乘上(1<<pos)的值 假设现在考虑到pos位,设f[i]为第i个节点期望的异或和第pos位 ...
- 数据集是 seq 文件的处理办法
数据集是 seq 文件的处理办法 2017-03-17 最近下了一个数据集,是 seq 格式的,第一次处理这种数据.使用了官方提供的 matlab 工具包:https://pdollar.github ...
- 【C#】委托的发展
"用事件去处理程序, 进而解决问题" ---- 委托的目的 委托早在C#2的时候就已经初具模型, 但是并不是特别灵活, 制止C#3才在代码中被广泛使用. C#4中泛型委托, C#5 ...