zoukankan      html  css  js  c++  java
  • Move Zeroes

    package cn.edu.xidian.sselab;

    /**
     * titile:Move Zeroes
     * content:
     * 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.
     */
    public class MoveZeroes {

        /**
         * @author wzy
         * @param args
         */
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            int[] nums= new int[]{1,2,3,0,0,1,3,0};
            MoveZeroes mz = new MoveZeroes();
            mz.moveZeroes(nums);
            for(int i=0;i<nums.length;i++){
                System.out.println(nums[i]);
            }
        }
        //给定一个数组,将所有的0值都移动到数组的最后,同时不改变非零数的位置。不能使用数组复制操作,还要使操作数最小
        //这里最开始想法是设置两个标签,一个用来表示数组中循环到数值的位置,第二个标签从上一个标签的下一个标签开始,这样时间复杂度为O(n2)
        //换种思路,时间复杂度为O(n),对数组进行一次遍历,记录下0的个数,同时将后面非零值往前移动,移动的位数为他前面的0的个数,最后根据0的个数在数组最后一次插入0
        //这样操作个数为0个数的2倍。
        public void moveZeroes(int[] nums){
            int len = nums.length;
            int count = 0;
            for(int i=0;i<len;i++){
                if(nums[i] == 0){
                    count++;
                }else{
                    nums[i-count] = nums[i];
                }
            }
            for(int i=0;i<count;i++){
                nums[len-1-i] = 0;
            }
        }
    }

  • 相关阅读:
    微信小程序wx.uploadFile 上传文件 的两个坑
    小程序 滚动wx.pageScrollTo
    scss定义全局变量引入sass-resources-loader报错
    mac 创建多个全局Path
    《node.js开发指南》partial is not defined的解决方案
    jq 将translate的旋转角度转为数值
    js浮点金额计算精度
    移动端页面弹窗滚动,页面也随之滚动解决方案
    js 禁止右击保存图片,禁止拖拽图片
    小程序md5加密
  • 原文地址:https://www.cnblogs.com/wzyxidian/p/4915907.html
Copyright © 2011-2022 走看看