LeetCode(283): Move Zeros

Move Zeros: 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:

  1. You must do this in-place without making a copy of the array.
  2. Minimize the total number of operations.

题意:移除给定数组中的0元素,将0元素移动到数组的末尾,并且保持不是0元素的元素的顺序。

思路: 可以利用一个指针将不是0 的元素尽可能的往前排,然后将剩余的全部设置为0。

代码:

public void moveZeroes(int[] nums) {
        int cnt =0,pos = 0;
        int len = nums.length;

        for(int i =0;i<len;i++){
            //非0元素向前移
            if(nums[i]!=0){
                nums[pos] = nums[i];
                pos++;
            }
        }
        //其余元素设置为0
        for(;pos<len;pos++){
            nums[pos]= 0;
        }
    }
时间: 2024-08-06 08:20:18

LeetCode(283): Move Zeros的相关文章

[刷题] LeetCode 283 Move Zeros

要求: 将所有的0,移动到vector的后面比如; [1,3,0,12,5] -> [1,3,12,5,0] 第一版程序,时间.空间复杂度都是O(n) 1 #include<iostream> 2 #include<vector> 3 4 using namespace std; 5 6 class Solution{ 7 public: 8 void moveZeros(vector<int>& nums){ 9 vector<int> no

【LeetCode】283. Move Zeros

题目 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:Yo

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] 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, 0, 0]. Note: You

LeetCode 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, 0, 0]. Note: You

LeetCode: 283 Move Zeroes(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 = [0, 1, 0, 3, 12], after calling your function, nums should be [1, 3, 12, 0, 0]. Note:

[LeetCode]: 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, 0, 0]. Note:

LeetCode 283 Move Zeroes(移动全部的零元素)

翻译 给定一个数字数组.写一个方法将全部的"0"移动到数组尾部.同一时候保持其余非零元素的相对位置不变. 比如,给定nums = [0, 1, 0, 3, 12],在调用你的函数之后,nums应该变为[1, 3, 12, 0, 0]. 备注: 你必须就地完毕,不得复制该数组. 最小化总共的操作数. Given an array nums, write a function to move all 0's to the end of it while maintaining the re

Leetcode 283 Move Zeroes python

题目: 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]. python