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. android采集设备

    http://www.hingway.com/xindalu/xindalu-data.htm 安卓新大陆设备 wce优博讯设备

  2. NodeJs初相识

    一.nodeJs简介 1.Node 是一个服务器端 JavaScript 解释器. 2.Node 的目标是帮助程序员构建高度可伸缩的应用程序,编写能够处理数万条同时连接到一个物理机的连接代码.处理高并 ...

  3. GSL--GNU Scientific Library 小记

    摘自http://qianjigui.iteye.com/blog/847612 GSL(GNU Scientific Library)是一个 C 写成的用于科学计算的库,下面是一些相关的包 Desi ...

  4. pipelines和重定向命令

    pipelines: command1 | command2 例如,ls -l /usr/bin | less,将输出结果作为 less 命令的输入结果,在standard output 中显示出来. ...

  5. JAVA代码覆盖率采集与分析方案

    原文地址-> http://m.blog.csdn.net/article/details?id=48688763

  6. appium介绍和工作原理

    导读 Appium这个听起来既生疏也熟悉的自动化测试工具,比起原生的UiAutomator可能是异常的不起眼,可是却是有自身独当一面的能力,可以完成许多高难度作业,完成UiAutomator不可能完成 ...

  7. php stripcslashes()函数 语法

    php stripcslashes()函数 语法 作用:删除由 addcslashes() 函数添加的反斜杠.深圳直线电机 语法:stripcslashes(string) 参数: 参数 描述 str ...

  8. 绕X 轴 Y轴 Z轴旋转的结果

    void warp_perspect_3_angle(cv::Mat face, float roll, float yaw, float pitch) { cv::Mat face_img = fa ...

  9. cython安装、使用

    cython安装.使用 原创 2012年09月27日 17:25:11 8436 0 0 一.cython 在linux(ubuntu)下安装 sudo apt-get install cython ...

  10. Tarjan 总结

    Tarjan 基础 dfn[i]: 在dfs中该节点被搜索的次序(时间戳). low[i]: 为i或i的子树能够追溯到的最早的栈中节点的次序号. 当 dfn[i] == low[i] 时,为i或i的子 ...