【LeetCode】283. Move Zeroes(移动零)

【LeetCode】283. Move Zeroes(移动零)

题目链接:https://leetcode.com/problems/move-zeroes/description/

难度: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:

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

解释:在不改变数组非零元素顺序的情况下,把零放到数组末尾。注意:in-palce

思路:类似于移除元素的双指针法,只不过反过来;快指针遍历,遇到0不动,遇到非0,则和慢指针交换,慢指针+1。

class Solution {
    public void moveZeroes(int[] nums) {
        int n=nums.length;
        int index=0;
        for(int i=index;i



日期:2018/2/24-0:19

你可能感兴趣的:(LeetCode)