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.
Credits:
Special thanks to @jianchao.li.fighter for adding this problem and creating all test cases.

Subscribe to see which companies asked this question

思路:

说实话。刚看到有点头大,后面尝试写,思路是遍历每一个元素,如果是0,就把后面的元素往前移动一个,然后把零移到后面。

注意:在循环的时候,不要急着对循环变量加1,先判断移动后,是不是0.如果是零,接着移动,不加1.还有每次移动,都把后面就加一个 零,设置一个变量count技术,后面循环n = n - count

代码:

public class Solution {
    public void moveZeroes(int[] nums) {
        int n = nums.length;
        //用来后面减去移动次数的计数
        int num = n;
        int i = 0;
        while(i < num){
            if(nums[i] == 0){
                int last = nums[i];
                for(int a = i;a < n-1;a++){
                    nums[a] = nums[a+1];
                }
                nums[n-1] = last;
            }
            if(nums[i] != 0){
            //如果不是零,才往后加1
                i = i+1;
            }else{
            //减去移动次数,与因为后面都是零
                num = num-1;
            }
        }
    }
}

leetcode之旅(7)-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. LeetCode(283)Move Zeroes

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

  4. LeetCode Array Easy 283. Move Zeroes

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

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

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

  6. LeetCode:Move Zeroes

    LeetCode:Move Zeroes [问题再现] Given an array nums, write a function to move all 0's to the end of it w ...

  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. java中List接口的实现类 ArrayList,LinkedList,Vector 的区别 list实现类源码分析

    java面试中经常被问到list常用的类以及内部实现机制,平时开发也经常用到list集合类,因此做一个源码级别的分析和比较之间的差异. 首先看一下List接口的的继承关系: list接口继承Colle ...

  2. 与信号相关的linux系统编程API

    1. kill(pid_t pid, int sig); //给指定的进程发送sig信号   raise(int sig); //给当前进程发送sig信号2. 处理指定的信号    typedef v ...

  3. Android的数字选择器NumberPicker-android学习之旅(三十七)

    我想说的话 今天晚上我依然在图书馆写博客,其实此刻我的没心激动而忐忑,因为明天就是足球赛的决赛,我作为主力球员压力很大,因对对方很强大,但是那又怎么样.so what...我不会停止写博客的 Numb ...

  4. Android的DatePicker和TimePicker-android学习之旅(三十八)

    DatePicker和TimePicker简介 DatePicker和TimePicker是从FrameLayout继承而来,他们都是比较简单的组件.时间改变时间分别添加OnDateChangeLis ...

  5. 最简单的基于FFmpeg的内存读写的例子:内存转码器

    ===================================================== 最简单的基于FFmpeg的内存读写的例子系列文章列表: 最简单的基于FFmpeg的内存读写的 ...

  6. Linux:ssh_config快速访问服务器

    在当前用户的根目录下: cd ~/.ssh vi config 编辑config内容为下面: ForwardAgent yes Host 1 Hostname 192.168.1.1 User roo ...

  7. Web Service进阶(五)SOAPBinding方式讲解

    Web Service进阶(五)SOAPBinding方式讲解 Java API for XML Web Services (JAX-WS) 2.0 (JSR 224) Standard Implem ...

  8. metasploit使用

    新版本的Metasploit分为Pro和Communicate版本,都可以使用WebUI的方式和Console的方式 下面主要介绍console方式的使用 1.   use

  9. TCP的ACK确认系列 — 发送状态转换机

    主要内容:TCP的ACK发送方式,以及ACK发送状态转换机的实现. 内核版本:3.15.2 我的博客:http://blog.csdn.net/zhangskd 概述 TCP采用两种方式来发送ACK: ...

  10. 青年菜君与小农女送菜商业模式PK

    青年菜君与小农女送菜商业模式PK   对比项 青年菜君 小农女送菜 优势 劣势 开业 2014年3月3日 2013年9月 渠道 地铁捕获用户 写字楼配送 送货 来店面自取 送货到写字楼 菜君 1.减少 ...