Move Zeroes

package cn.edu.xidian.sselab.array;

/**
 * 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;
        }
    }
}

时间: 2024-08-02 14:56:24

Move Zeroes的相关文章

[LintCode] 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. Notice You must do this in-place without making a copy of the array. Minimize the total number of operations. Exampl

[LeetCode][JavaScript]Move Zeroes

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]

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 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,

【5_283】Move Zeroes

终于碰到一道水题,睡觉去~ Move Zeroes Total Accepted: 37369 Total Submissions: 88383 Difficulty: Easy 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 =

leetCode 283. Move Zeroes 数组

283. 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,

LeetCode:Move Zeroes - 将数组中的0移到最后

1.题目名称 Move Zeroes(将数组中的0移到最后) 2.题目地址 https://leetcode.com/problems/move-zeroes 3.题目内容 英文: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. 中文:给出一个数字数组,写一个函数将数组中所有的0移

【leetcode】Move Zeroes

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

Leetcode刷题记录[python]——283 Move Zeroes

一.前言 题是上周五做的,开始思路有点问题,考虑不全,导致submit了3次才AC. 二.题283 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 call

283. Move Zeroes - LeetCode

Question 283. Move Zeroes Solution 题目大意:将0移到最后 思路: 1. 数组复制 2. 不用数组复制 Java实现: 数组复制 public void moveZeroes(int[] nums) { int[] arr = Arrays.copyOf(nums, nums.length); int start = 0; int end = nums.length - 1; for (int i=0; i<arr.length; i++) { int tmp