LeetCode: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:

  1. You must do this in-place without making a copy of the array.
  2. Minimize the total number of operations.

【优质算法】

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];
}
while(index<nums.length)
nums[index++]=0;
}
}

【题后反思】

  这里也有双指针的应用,index是一个慢指针,i代表一个快指针,i不断向后移动,并判断所在位置元素是否为0,如果为0的话,就往后移动;如果不为0的话,让index等于这个值,这里是最关键的一步,在这里index起到从头开始更新这个数组的作用,把所有的的非0数字更新到数组中,忽略的0很可能就在更新中被赋予新的值。所以元素为0便往后面移动并不影响算法。

LeetCode:Move Zeroes的更多相关文章

  1. [LeetCode] Move Zeroes 移动零

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

  2. LeetCode——Move Zeroes

    Description: Given an array nums, write a function to move all 0's to the end of it while maintainin ...

  3. LeetCode Move Zeroes (简单题)

    题意: 给定一个整型数组nums,要求将其中所有的0移动到末尾,并维护所有非0整数的相对位置不变. 思路: 扫一遍,两个指针维护0与非0的交界,将非0的数向前赋值就行了. C++ class Solu ...

  4. leetcode:283. Move Zeroes(Java)解答

    转载请注明出处:z_zhaojun的博客 原文地址:http://blog.csdn.net/u012975705/article/details/50493772 题目地址:https://leet ...

  5. leetcode之旅(7)-Move Zeroes

    Move Zeroes 题目描述: Given an array nums, write a function to move all 0's to the end of it while maint ...

  6. LeetCode Javascript实现 283. Move Zeroes 349. Intersection of Two Arrays 237. Delete Node in a Linked List

    283. Move Zeroes var moveZeroes = function(nums) { var num1=0,num2=1; while(num1!=num2){ nums.forEac ...

  7. 【leetcode】283. Move Zeroes

    problem 283. Move Zeroes solution 先把非零元素移到数组前面,其余补零即可. class Solution { public: void moveZeroes(vect ...

  8. 【leetcode】Move Zeroes

    Move Zeroes 题目: Given an array nums, write a function to move all 0‘s to the end of it while maintai ...

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

随机推荐

  1. Reactnative 随笔一

    ---恢复内容开始--- 1.ReactDOM.render()    React的最基本方法,用于将模板转为HTML语言,并插入指定的DOM节点 将h标签插入example节点 2.HTML语言直接 ...

  2. 原创 Datareader 导出为csv文件方法

    DataReader 是游标只读数据, 如果是大数据导出,用Datatable 将耗费巨大内存资源.因为Datatable 其实就是内存中的一个数据表 代码如下 /// <summary> ...

  3. blender 2.6 快捷键

    右键单击 选中 A 取消选择/全选 中键拖动 旋转 中键滚动放大/缩小 Shift+中键拖动 平移 右上斜线三角区域拖动 产生新视窗 Shift+c准星复位 Num1前视图 Ctrl+Num1后视图 ...

  4. 中国大学MOOC-陈越、何钦铭-数据结构-2016秋期末考试

    判断题: 1-1 N2logN和NlogN2具有相同的增长速度. (2分) 1-2 对一棵平衡二叉树,所有非叶结点的平衡因子都是0,当且仅当该树是完全二叉树.(2分) 1-3 无向连通图所有顶点的度之 ...

  5. tcp/udp高并发和高吐吞性能测试工具

    在编写一个网络服务的时候都比较关心这个服务能达到多少并发连接,而在这连接的基础上又能达到一个怎样的交互能力.编写服务已经是一件很花力气的事情,而还要去编写一个能够体现结果的测试工具就更加消耗工作时间. ...

  6. Orleans 客户端请求的消息流转以及消息在Silo中再路由机制

    1.客户端是一个OutSideRuntimeClient,在这个客户端类中有一个消息代理中心transport(类型为ProxiedMessageCenter) 2.ProxiedMessageCen ...

  7. 第一个 Asp.Net vNext 应用程序

    要说免费的虚拟主机的话,最好的服务商应该就是Microsoft Azure(不是Windows Azure由世纪互联运营),提供免费的1GB .NET/Java/Python/Php空间,日流量有限制 ...

  8. $.ajax 跨域请求 Web Api

    WepApi确实方便好用,没有配置文件,一个apicontroller直接可以干活了.但今天用$.ajax跨域请求的时候总是获取不到数据,用fiddler一看确实抓到了数据,但回到$.ajax函数中, ...

  9. Win + D 和 Win + M的区别

    在Windows系统上,Win + D是显示桌面,Win + M是最小化所有窗口,咋一看,这两个快捷键貌似没有区别,但是在某些方面还是有细微的区别. 威力 从威力上来说,Win + D更牛逼一,因为显 ...

  10. webservice 之 WSDL的解析

    先看一个wsdl, <?xml version="1.0" encoding="UTF-8" standalone="no"?> ...