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:

  1. Input: [,,,,]
  2. 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的下一位去找非零元素,找到后交换两个元素。

  1. public void MoveZeroes(int[] nums) {
  2. for(int i = ; i < nums.Length; i++){
  3. if(nums[i]==){
  4. for(int j = i +; j < nums.Length; j++){
  5. if(nums[j] != ){
  6. nums[i]=nums[j];
  7. nums[j]=;
  8. break;
  9. }
  10. }
  11. }
  12. }
  13. }

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

  1. public void MoveZeroes(int[] nums) {
  2. if(nums.Length == )
  3. return;
  4. int i =;
  5. for(int j =; j < nums.Length; j++){
  6. if(nums[j] != ){
  7. Swap(nums, i, j);
  8. i++;
  9. }
  10. }
  11. while(i < nums.Length){
  12. nums[i++]=;
  13. }
  14. }
  15. private void Swap(int[] nums, int i, int j){
  16. int temp = nums[i];
  17. nums[i] = nums[j];
  18. nums[j]=temp;
  19. }

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. 前端学习(三十九)移动端app(笔记)

    移动端App    开发App的三种方式    Native App         原生        底层语言        java         Android        oc      ...

  2. CentOS 7 LNMP环境搭建 Zabbix3.4

    概述:在CentOS 7 64位操作系统环境下搭建LNMP(Linux+Nginx+MySQL+PHP)来运行Zabbix 3.4 监控程序 预先安装: yum install -y autoconf ...

  3. MetaException(message:For direct MetaStore DB connections, we don't support retries at the client level.)

    在mysql中执行以下命令:  drop database hive;  create database hive;  alter database hive character set latin1 ...

  4. HTML5 arc的例子

    demo.html <!DOCTYPE html> <html lang="zh"> <head> <meta charset=" ...

  5. Hadoop中的排序和连接

    MapReduce的全排序 主要是为了保证分区排序,即第一个分区的最后一个Key值小于第二个分区的第一个Key值 与普通的排序仅仅多一个自定义分区类MyPartitioner见自己所写的实验 (设置一 ...

  6. 【leetcode】722. Remove Comments

    题目如下: Given a C++ program, remove comments from it. The program source is an array where source[i] i ...

  7. 如何隐藏一个让人很难发现的bug?

    程序员的日常三件事:写bug.改bug.背锅.连程序员都自我调侃道,为什么每天都在加班?因为我的眼里常含bug. 那么如何写出一个让(坑)人(王)很(之)难(王)发现的bug呢? - 1 - 新手开发 ...

  8. vue循环渲染变量类样式

    由于需求的需要,将五种不同的颜色样式通过v-for进行遍历渲染,所以我这里采用绑定类函数进行判断方式.代码: 效果: 灵感来自:https://www.jianshu.com/p/33e181be3d ...

  9. window安装nodejs

    nvm管理nodejs 原文: https://www.cnblogs.com/shimily/articles/7244058.html1.下载nvm(nodejs版本管理工具) https://g ...

  10. [CSP-S模拟测试]:Lighthouse(哈密顿回路+容斥)

    题目背景 $Billions\ of\ lighthouses...stuck\ at\ the\ far\ end\ of\ the\ sky.$ 题目描述 平面有$n$个灯塔,初始时两两之间可以相 ...