[LeetCode] Move Zeroes

[LeetCode] 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.

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

Note:

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

    分析:

    将数组中0移到数组后边去,注意要保持数组的对峙成分地方不改变。

     

    代码:

     

    class Solution(object):
        def moveZeroes(self, nums):
    
            :type nums: List[int]
            :rtype: void Do not return anything, modify nums in-place instead.
    
            nums_l = len(nums)
            if nums_l <= 1:
                return
            z = i = 0
            while i < nums_l:
                t = nums[i]
                if t:
                    nums[i] = 0
                    nums[z] = t
                    z += 1
                i += 1
    

     

     

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
examp…

leetcode笔记: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.

二. 标题深入分析

问题的情致很显明,给定三个数组,将非零成分调解到前边,零成分放在数组前面,供给原来的地方操作并利用尽量少的操作次数。

难题相比轻便,只需扫描三遍数组,在此进度中动用贰个整形变量Index用于记录非零元素的个数,每境遇一个非零数,将其坐落nums[Index]中,然后Index1

遍历一次后,nums的非零成分地点已经规定,只需将数组后半段从头至尾置零就能够,由于Index笔录了非零成分的个数,所以置零操作也很有利。

三. 示例代码

class Solution {
public:
    void moveZeroes(vector& nums) {
        if (nums.size() < 1) return;  
        int Index = 0;
        for (int i = 0; i < nums.size(); ++i) 
            if (nums[i] != 0) 
                nums[Index++] = nums[i];

        for (;Index < nums.size(); ++Index) 
            nums[Index] = 0;
    }
};

四. 小结

对于那类难点,一般都务求原来的地点操作及尽量少的操作次数。

Zeroes 一. 标题陈述 Given an
array nums, write a function to move all 0s to the end of it while
maintaining the relative order of the non-zero elements….

解题思路

用多少个指针i,j分别指向0和非零,当j指向非零时,沟通八个指针所针对的成分,而且i向前走一步;j则每趟循环无论是还是不是交流都向前走一步。

达成代码

C++:

// Runtime: 20 ms
class Solution {
public:
    void moveZeroes(vector& nums) {
        int i = 0;
        int j = 0;
        while (j < nums.size())
        {
            if (nums[j] != 0)
            {
                swap(nums[i], nums[j]);
                i++;
            }
            j++;
        }
    }
};

Java:

// Runtime: 1 ms
public class Solution {
    public void moveZeroes(int[] nums) {
        int i = 0;
        int j = 0;
        while (j < nums.length) {
            if (nums[j] != 0) {
                if (j != i) {
                    int temp = nums[i];
                    nums[i] = nums[j];
                    nums[j] = temp;
                }
                i++;
            }
            j++;
        }
    }
}

 

] 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
num…