LeetCode(283. 移动零)
问题描述
给定一个数组 nums,编写一个函数将所有 0 移动到数组的末尾,同时保持非零元素的相对顺序。
示例:
输入: [0,1,0,3,12]
输出: [1,3,12,0,0]
说明:
- 必须在原数组上操作,不能拷贝额外的数组。
- 尽量减少操作次数。
解决方案
1.最快的原地置换
class Solution:
def moveZeroes(self, nums):
"""
:type nums: List[int]
:rtype: void Do not return anything, modify nums in-place instead.
"""
zero = 0
for i in range(len(nums)):
if not nums[i] == 0 and zero <= i:
nums[i], nums[zero] = nums[zero], nums[i]
zero += 1
2.使用remove+append
class Solution:
def moveZeroes(self, nums):
"""
:type nums: List[int]
:rtype: void Do not return anything, modify nums in-place instead.
"""
count = 0
while 0 in nums:
nums.remove(0)
count+=1
for i in range(count):
nums.append(0)
3.先覆盖,再写入0
class Solution:
def moveZeroes(self, nums):
"""
:type nums: List[int]
:rtype: void Do not return anything, modify nums in-place instead.
"""
count = 0
for i in range(0, len(nums)):
if (nums[i] != 0):
nums[count] = nums[i]
count += 1
for j in range(count, len(nums)):
nums[j] = 0
LeetCode(283. 移动零)的更多相关文章
- 前端与算法 leetcode 283. 移动零
目录 # 前端与算法 leetcode 283. 移动零 题目描述 概要 提示 解析 解法一:暴力法 解法二:双指针法 算法 传入[0,1,0,3,12]的运行结果 执行结果 GitHub仓库 # 前 ...
- Java实现 LeetCode 283 移动零
283. 移动零 给定一个数组 nums,编写一个函数将所有 0 移动到数组的末尾,同时保持非零元素的相对顺序. 示例: 输入: [0,1,0,3,12] 输出: [1,3,12,0,0] 说明: 必 ...
- Leetcode 283.移动零
移动零 给定一个数组 nums,编写一个函数将所有 0 移动到数组的末尾,同时保持非零元素的相对顺序. 示例: 输入: [0,1,0,3,12] 输出: [1,3,12,0,0] 说明: 必须在原数组 ...
- python(leetcode)-283移动零
给定一个数组 nums,编写一个函数将所有 0 移动到数组的末尾,同时保持非零元素的相对顺序. 示例: 输入: [0,1,0,3,12] 输出: [1,3,12,0,0] 说明: 必须在原数组上操作, ...
- Leetcode 283.移动零 By Python
思路 我们可以用python的list comprehension来取出所以非0的元素,而且这样取出来会保持原有的相对顺序,再统计先后变化的长度,补上相应的0即可 代码 class Solution( ...
- LeetCode:移动零【283】
LeetCode:移动零[283] 题目描述 给定一个数组 nums,编写一个函数将所有 0 移动到数组的末尾,同时保持非零元素的相对顺序. 示例: 输入: [0,1,0,3,12] 输出: [1,3 ...
- 【Leetcode】【简单】【283. 移动零】【JavaScript】
题目描述 283. 移动零 给定一个数组 nums,编写一个函数将所有 0 移动到数组的末尾,同时保持非零元素的相对顺序. 示例: 输入: [0,1,0,3,12]输出: [1,3,12,0,0] 说 ...
- 【LeetCode】283.移动零
283.移动零 知识点:数组:双指针: 题目描述 给定一个数组 nums,编写一个函数将所有 0 移动到数组的末尾,同时保持非零元素的相对顺序. 示例 输入: [0,1,0,3,12] 输出: [1, ...
- LeetCode:打印零与奇偶数【1116】
LeetCode:打印零与奇偶数[1116] 题目描述 假设有这么一个类: class ZeroEvenOdd { public ZeroEvenOdd(int n) { ... } // 构造函数 ...
- [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 ...
随机推荐
- 论文阅读笔记二十二:End-to-End Instance Segmentation with Recurrent Attention(CVPR2017)
论文源址:https://arxiv.org/abs/1605.09410 tensorflow 代码:https://github.com/renmengye/rec-attend-public 摘 ...
- Python Day-1 练习
作业1 要求:1.输入用户密码 2.认真成功后显示欢迎信息 3.输入三次锁定 代码如下: __author__ = 'zhang.ning' username = "zhangning&qu ...
- Visual Studio 2017离线安装失败:安装程序清单签名验证失败
解决办法: 方法1:运行gpeidit.msc,然后 Windows 设置-安全设置->本地策略-安全选项-系统机密->将FIPS兼容算法用于加密.哈希和签名-设置禁用 方法2:删除vs ...
- Go语言之闭包
闭包的最初目的是为了减少全局变量,在函数调用过程中,隐式的传递共享变量. 但这样的编辑,带来的坏处是不够直接清晰. 所以,如非必要,不要使用. 对象是附有行为的数据,它在类中集中定义, 而闭包是附有数 ...
- Failed to create Accelerated Display. Please check the display hardware and drivers meet the minimum requirements.
ArcGIS Runtime for WPF开发中Map设置了属性UseAcceleratedDisplay="True",报错: Sample: LocalMap Error: ...
- Python_time模块
time模块:用于时间的访问和转换 UTC:协调世界时(世界统一时间) 时间戳:是指格林威治时间1970年01月01日00时00分00秒(北京时间1970年01月01日08时00分00秒)起至现在的总 ...
- js获取单选框的值
js获取单选框的值 var lx= $("input[name='lx']:checked").val();
- Unable to load configuration. - action - file:/F:/apache-tomcat-8.0.30/webapps/test1Struts2/WEB-INF/classes/struts.xml:11:71
Unable to load configuration. - action - file:/F:/apache-tomcat-8.0.30/webapps/test1Struts2/WEB-INF/ ...
- CentOS 7 休眠系统
CentOS 7的电源按钮只有关机和重启两项,但是可以用命令来休眠系统: 重启: $ systemctl reboot 退出系统并停止电源: $ systemctl poweroff 待机: $ sy ...
- 最短路(bellman)-hdu2066
题目链接:https://vjudge.net/problem/HDU-2066 题目描述: 代码实现: #include <cstdio> #include <cstring> ...