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.
题目标签:Array, Two Pointers
题目给了我们一个nums array, 让我们把所有的0 都移动到最后面,保留其他数字的排序。
利用two pointers p1 和 p2, 基本思想是让p2停留在0的数字上,让p1找到不是0的数字,对换p1 p2的值。
遍历nums array,当遇到0的话,p1++;当遇到不是0的数字,对换p1 和 p2的值,p1++ p2++。
Java Solution:
Runtime beats 74.14%
完成日期:04/27/2017
关键词:Array, Two Pointers
关键点:找到不是0的数字,与0置换
public class Solution
{
public void moveZeroes(int[] nums)
{
int p1 = 0; // iterate each number
int p2 = 0; // stop at 0 while(p1 < nums.length)
{
if(nums[p1] != 0) // find the non-zero number
{
if(p1 != p2) // swap non-zero number with zero number
{ // if p1 = p2, no need to swap
int temp = nums[p1];
nums[p1] = nums[p2];
nums[p2] = temp;
} p2++;
} p1++;
} }
}
参考资料:N/A
LeetCode 算法题目列表 - LeetCode Algorithms 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 ...
- [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 移动数组中的零 (数组,模拟)
题目描述 已知数组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 ...
随机推荐
- 才趟过的一个坑,css造成的Validform表单提交按钮点击无效
最近入手的一个项目,在开发的过程中,遇到了一个以前没遇到过的问题,废了半天的功夫才弄懂原因,留下足迹,警醒后人,下面开始讲故事啦! 在一个昏天暗地的上午,我一个人照常坐在办公室安静的工作中!项目编码已 ...
- Jquery总结图
读完锋利Jquery第二版书,对其进行整理做出的思维导图:
- XML【介绍、用途、了解XML技术架构、语法】
什么是XML? XML:extensiable markup language 被称作可扩展标记语言 XML简单的历史介绍: gml->sgml->html->xml gml(通用标 ...
- JSP第四篇【EL表达式介绍、获取各类数据、11个内置对象、执行运算、回显数据、自定义函数、fn方法库】
什么是EL表达式? 表达式语言(Expression Language,EL),EL表达式是用"${}"括起来的脚本,用来更方便的读取对象! EL表达式主要用来读取数据,进行内容的 ...
- Android 之http编程
HTTP-GET和HTTP-POST定义: 都是使用HTTP的标准协议动词,用于编码和传送变量名/变量值对参数,并且使用相关的请求语义. 每个HTTP-GET和HTTP-POST都由一系列HTTP请求 ...
- JVM菜鸟进阶高手之路六(JVM每隔一小时执行一次Full GC)
转载请注明原创出处,谢谢! 上次分析详细地址在:http://www.jianshu.com/p/a6236cd39e2d 以为上次问题是rmi的问题就此结束了,但是问题并没有结束,其实本次问题不是r ...
- php字符的替换,截取,指定查找
<?php/** * Created by 郭鹏. * User: msi * Date: 2017/9/27 * Time: 14:17 *///随机数生成器echo rand();echo ...
- 浅析强连通分量(Tarjan和kosaraju)
理解 在有向图G中,如果两点互相可达,则称这两个点强连通,如果G中任意两点互相可达,则称G是强连通图. 定理: 1.一个有向图是强连通的,当且仅当G中有一个回路,它至少包含每个节点一次. ...
- iOS逆向环境以及常用命令行(逆向一)
一.环境介绍 越狱环境:iPhone 5s iOS9.3.1 yueyu:~ root# uname -a Darwin yueyu 15.4.0 Darwin Kernel Version 15.4 ...
- 斐波那契数列第N项f(N)[矩阵快速幂]
矩阵快速幂 定义矩阵A(m*n),B(p*q),A*B有意义当且仅当n=p.即A的列数等于B的行数. 且C=A*B,C(m*q). 例如: 进入正题,由于现在全国卷高考不考矩阵,也没多大了解.因为遇到 ...