美文网首页Leetcode题解-PHP版
Leetcode PHP题解--D68 283. Move Ze

Leetcode PHP题解--D68 283. Move Ze

作者: skys215 | 来源:发表于2019-05-22 08:42 被阅读0次

D68 283. Move Zeroes

题目链接

283. Move Zeroes

题目分析

给定一个整数数组,将值为0的元素移动到数组末尾,而不改动其他元素出现的顺序。

思路

计算总共有多少个元素。

再在去0后的元素末尾填充0到计算出的数组长度。

最终代码

<?php
class Solution {

    /**
     * @param Integer[] $nums
     * @return NULL
     */
    function moveZeroes(&$nums) {
        $total = count($nums);
        $nums = array_pad(array_filter($nums),$total,0);
        return $nums;
    }
}

若觉得本文章对你有用,欢迎用爱发电资助。

相关文章

网友评论

    本文标题:Leetcode PHP题解--D68 283. Move Ze

    本文链接:https://www.haomeiwen.com/subject/rhnezqtx.html