Description

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.

Example:

Input: [,,,,]
Output: [,,,,]

Note:

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

问题描述:给一个数组,将数组中所有的0移动到数组的尾部,而不改变其他非零元素的相对位置

个人思路:

1.暴力解法。遍历整个数组,当找到0时,开始从0的下一位去找非零元素,找到后交换两个元素。

public void MoveZeroes(int[] nums) {
for(int i = ; i < nums.Length; i++){
if(nums[i]==){
for(int j = i +; j < nums.Length; j++){
if(nums[j] != ){
nums[i]=nums[j];
nums[j]=;
break;
}
}
}
}
}

第二种解法:设定一个指针i=0,遍历数组,如果当前值不为零 则交换 i j 的元素,

public void MoveZeroes(int[] nums) {
if(nums.Length == )
return;
int i =;
for(int j =; j < nums.Length; j++){
if(nums[j] != ){
Swap(nums, i, j);
i++;
}
}
while(i < nums.Length){
nums[i++]=;
}
}
private void Swap(int[] nums, int i, int j){
int temp = nums[i];
nums[i] = nums[j];
nums[j]=temp;
}

LeetCode Array Easy 283. Move Zeroes的更多相关文章

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

  2. [LeetCode&Python] Problem 283. Move Zeroes

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

  3. 283. Move Zeroes【easy】

    283. Move Zeroes[easy] Given an array nums, write a function to move all 0's to the end of it while ...

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

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

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

  6. 【leetcode】283. Move Zeroes

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

  7. 283. Move Zeroes - LeetCode

    Question 283. Move Zeroes Solution 题目大意:将0移到最后 思路: 1. 数组复制 2. 不用数组复制 Java实现: 数组复制 public void moveZe ...

  8. 283. Move Zeroes(C++)

    283. Move Zeroes Given an array nums, write a function to move all 0's to the end of it while mainta ...

  9. 283. Move Zeroes@python

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

随机推荐

  1. 07.Linux-CentOS系统库文件libaudit.so.1丢失问题

    问题:缺少共享库文件sudo: error while loading shared libraries: libaudit.so.1: cannot open shared object file: ...

  2. vim常用快捷键及操作记录

    1. 安装 sudo apt-get install vim 或者 yum install -y vim-enhanced 2. 具体使用技巧如下 打开文件: 命令/操作 说明 vim + filen ...

  3. 【LeetCode】深搜DFS(共85题)

    [98]Validate Binary Search Tree [99]Recover Binary Search Tree [100]Same Tree [101]Symmetric Tree [1 ...

  4. python 模仿 C/C++ 结构体

    import struct from ctypes import * class MyStruct(Structure): _fields_ = [ ("v1", c_char), ...

  5. Autoit 3 常用的语句

    {系统环境变量} EnvUpdate ( ) ;更新环境变量 EnvGet ( "变量名称" ) ;取环境变量 ClipGet ( ) ;取剪辑板文本 EnvSet ( " ...

  6. SpringBoot---监控与管理actuator

    1.概述 SpringBoot在Start POMS中提供了一个特殊依赖模块spring-boot-starter-actuator: 引入spring-boot-starter-actuator模块 ...

  7. python 列表使用

    下面实现的类似于java中的数组: names[-2]表示实现倒数的第2个参数 names[-3,-1]表示实现-3到-1的值不包含-1 增删改查 下面代码实现列表的增删改查功能: 复制copy 深c ...

  8. linux开机启动jar

    一.使用系统文件rc.local 启动命令可添加在/etc/rc.local(链接地址为/etc/rc.d/rc.local)中即可开机启动,不建议使用此种方法. 二.自定义启动脚本 1.新建启动脚本 ...

  9. [USACO09DEC] Video Game Troubles

    背包DP:有依赖的背包问题 #include <cstdio> #include <cstdlib> #include <cmath> #include <c ...

  10. No orientation specified, and the default is horizontal.异常处理(转)

    参考:http://blog.csdn.net/sky_monkey/article/details/21466975 整的错误提示信息为: No orientation specified, and ...