Leetcode PHP题解--D68 283. Move Zeroes


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解析(123. Best Time to Buy and Sell Stock III)

>> 下一篇: 通过 Let's Encrypt 和七牛云两种方式免费部署 HTTPS 站点