转载请注明出处:z_zhaojun的博客

原文地址:http://blog.csdn.net/u012975705/article/details/50493772

题目地址:https://leetcode.com/problems/move-zeroes/

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.

解法(java):

public class Solution {
public void moveZeroes(int[] nums) {
if (nums != null) {
int length = nums.length;
for (int i = 0, j = 0; i < length; i++) {
if (nums[i] != 0) {
if (i != j) {
nums[j] = nums[i];
nums[i] = 0;
}
j++;
}
}
}
}
}

leetcode:283. Move Zeroes(Java)解答的更多相关文章

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

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

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

  4. [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 ...

  5. leetcode 283. Move Zeroes -easy

    题目链接:https://leetcode.com/problems/move-zeroes/ 题目内容: Given an array nums, write a function to move ...

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

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

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

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

  10. Leetcode 283. Move Zeroes 移动数组中的零 (数组,模拟)

    题目描述 已知数组nums,写一个函数将nums中的0移动到数组后面,同时保持非零元素的相对位置不变.比如已知nums=[0,1,0,3,12],调用你写的函数后nums应该是[1,3,12,0,0] ...

随机推荐

  1. ssh和sshd服务

    1.1 对称加密和非对称加密 对称加密:加密和解密使用一样的算法,只要解密时提供与加密时一致的密码就可以完成解密.例如QQ登录密码,银行卡密码,只要保证密码正确就可以. 非对称加密:通过公钥(publ ...

  2. phpize Cannot find autoconf. 错误解决

    phpize Configuring for: PHP Api Version: 20151012 Zend Module Api No: 20151012 Zend Extension Api No ...

  3. IDEA中远程Debug调试

    一.设置JVM支持远程Debug调式 由于我的应用是springboot, 所以直接使用java -jar的方法将服务启动起来. java -Xdebug -Xrunjdwp:transport=dt ...

  4. 杭电 5326 Work (并查集求子结点为k的结点数)

    Description It’s an interesting experience to move from ICPC to work, end my college life and start ...

  5. 【HIHOCODER1527 】 快速乘法

    描述 在写代码时,我们经常要用到类似 x × a 这样的语句( a 是常数).众所周知,计算机进行乘法运算是非常慢的,所以我们需要用一些加法.减法和左移的组合来实现乘一个常数这个操作.具体来讲, 我们 ...

  6. JVM——内存管理和垃圾回收

    1.  何为GC 转载请注明出处:http://blog.csdn.net/seu_calvin/article/details/51892567 Java与C语言相比的一个优势是,可以通过自己的JV ...

  7. [WPF自定义控件]Window(窗体)的UI元素及行为

    1. 前言 本来打算写一篇<自定义Window>的文章,但写着写着发觉内容太多,所以还是把使用WindowChrome自定义Window需要用到的部分基础知识独立出来,于是就形成了这篇文章 ...

  8. spring的IOC和DI

    https://blog.csdn.net/fuzhongmin05/article/details/55802816 (1)IOC:控制反转,把对象创建交给spring进行配置 (2)DI:依赖注入 ...

  9. xtu summer individual 1 E - Palindromic Numbers

    E - Palindromic Numbers Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%lld & %l ...

  10. hexo干货系列:(总纲)搭建独立博客初衷

    前言 我是一名程序员,以前知识整理都是整理在为知笔记上,博客用的比较少,更别说是使用独立博客,因为不会... 2016年过年在家期间偶然的机会萌发了自己要搭建一个属于自己的独立博客的想法,于是就有了下 ...