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

Example

Given nums = [0, 1, 0, 3, 12], after calling your function, nums should be [1, 3, 12, 0, 0].

LeetCode上的原题,请参见我之前的博客Move Zeroes

解法一:

class Solution {
public:
    /**
     * @param nums an integer array
     * @return nothing, do this in-place
     */
    void moveZeroes(vector<int>& nums) {
        for (int i = 0, j = 0; i < nums.size(); ++i) {
            if (nums[i]) {
                swap(nums[i], nums[j++]);
            }
        }
    }
};

解法二:

class Solution {
public:
    /**
     * @param nums an integer array
     * @return nothing, do this in-place
     */
    void moveZeroes(vector<int>& nums) {
        int left = 0, right = 0;
        while (right < nums.size()) {
            if (nums[right]) {
                swap(nums[left++], nums[right]);
            }
            ++right;
        }
    }
};
时间: 2024-08-11 11:58:28

[LintCode] 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

[LintCode] Trailing Zeroes 末尾零的个数

Write an algorithm which computes the number of trailing zeros in n factorial. Have you met this question in a real interview? Yes Example 11! = 39916800, so the out should be 2 Challenge O(log N) time s

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

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

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