[LeetCode] 283. Move Zeroes 移动零
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.
For example, given nums = [0, 1, 0, 3, 12], after calling your function, nums should be [1, 3, 12, 0, 0].
Note:
You must do this in-place without making a copy of the array.
Minimize the total number of operations.
Credits:
Special thanks to @jianchao.li.fighter for adding this problem and creating all test cases.
把一个数组里所有的0都移到后面,不能改变非零数的相对位置关系,而且不能拷贝额外的数组。
要用替换法in-place,双指针来做,前面的指针指向处理过的最后一个非零数字, 后面一个指针向后扫,遇到非零数字,和前面指针所指数字交换位置,前面指针后移1位。
或者不交换只把非零的数字换到前面,最后子统一把后面的全部变成0。
Java: Time Complexity: O(n), Space Complexity: O(1)
public class Solution {
public void moveZeroes(int[] nums) {
int index = 0;
for (int i = 0; i < nums.length; ++i) {
if (nums[i] != 0) {
nums[index++] = nums[i];
}
}
for (int i = index; i < nums.length; ++i) {
nums[i] = 0;
}
}
}
Python:
class Solution(object):
def moveZeroes(self, nums):
"""
:type nums: List[int]
:rtype: void Do not return anything, modify nums in-place instead.
"""
pos = 0
for i in xrange(len(nums)):
if nums[i]:
nums[i], nums[pos] = nums[pos], nums[i]
pos += 1
C++:
class Solution {
public:
void moveZeroes(vector<int>& nums) {
int pos = 0;
for (auto& num : nums) {
if (num) {
swap(nums[pos++], num);
}
}
}
};
类似题目:
All LeetCode Questions List 题目汇总
[LeetCode] 283. Move Zeroes 移动零的更多相关文章
- [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 ...
- 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 ...
- 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 移动数组中的零 (数组,模拟)
题目描述 已知数组nums,写一个函数将nums中的0移动到数组后面,同时保持非零元素的相对位置不变.比如已知nums=[0,1,0,3,12],调用你写的函数后nums应该是[1,3,12,0,0] ...
- Java [Leetcode 283]Move Zeroes
题目描述: Given an array nums, write a function to move all 0's to the end of it while maintaining the r ...
- leetcode 283. Move Zeroes -easy
题目链接:https://leetcode.com/problems/move-zeroes/ 题目内容: Given an array nums, write a function to move ...
- LeetCode 283 Move Zeroes 解题报告
题目要求 Given an array nums, write a function to move all 0's to the end of it while maintaining the re ...
- LeetCode 283 Move Zeroes(移动全部的零元素)
翻译 给定一个数字数组.写一个方法将全部的"0"移动到数组尾部.同一时候保持其余非零元素的相对位置不变. 比如,给定nums = [0, 1, 0, 3, 12],在调用你的函数之 ...
- 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 ...
随机推荐
- mysql数据库总结。
mysql MySQL语法MySQL采用结构化查询语言SQL (Structured Query Language)语言来操作数据库SQL语句必须以 ; 结束SQL语句分类DDL(数据定义语言): c ...
- 《BUG创造队》第四次作业:基于原型的团队项目需求调研与分析
项目 内容 这个作业属于哪个课程 2016级软件工程 这个作业的要求在哪里 实验八 团队作业4:基于原型的团队项目需求调研与分析 团队名称 BUG创造队 作业学习目标 (1)体验以原型设计为基础的团队 ...
- PHP隐藏IP地址末位的方法
很久之前写过一个使用ASP隐藏IP地址末位的文章,也就是有时候为了保护用户的隐私,会隐藏用户的IP地址,达成类似于 222.222.222.* 的效果. 现在想要用PHP来实现,经过尝试,其实非常简 ...
- Laravel —— could not find driver
Laravel 中的数据库是以 PDO 的方式连接的 数据库连接失败时,先检查问题所在,再对症下药 本文以 pgsql 为例 1.判断 pgsql 是否启动 $ ps -ef | grep pgsql ...
- 爬虫 - 请求库之selenium
介绍 官方文档 selenium最初是一个自动化测试工具,而爬虫中使用它主要是为了解决requests无法直接执行JavaScript代码的问题 selenium本质是通过驱动浏览器,完全模拟浏览器的 ...
- stm32flash的读写特性
在使用stm32自带的flash保存数据时候,如下特点必须知道: 1.必须是先擦除一个扇区,才能写入 2.读数据没有限制 3.写数据必须是2字节,同时写入地址以一定要考虑字节对齐, 4.一般都是在最后 ...
- 02-Flutter移动电商实战-建立项目和编写入口文件
环境搭建请参考之前写的一篇文章:Flutter_初体验_创建第一个应用 1.创建项目 采用AndroidStudio构建本项目,FIle>New>New Flutter Project… ...
- WinDbg的工作空间---Work Space
一.什么是工作空间 Windbg把和调试相关的所有配置称为workspace.WinDbg使用工作空间来描述和存储调试项目的属性.参数及调试器设置等信息.工作空间与vc中的项目文件很相似.退出wind ...
- Calibre中使用DeDRM插件进行Kindle电子书解锁
小书匠 废话不多说,下面是Calibre和DeDRM插件的下载地址: https://calibre-ebook.com/download https://github.com/apprenticeh ...
- rust-vmm 学习(二)
eventfd virtio中,guest和vhost通过evnetfd通知对方,见(Virtio ring and virtio-net). REF: Qemu-kvm的ioeventfd创建与触发 ...