LeetCode 1089: Duplicate Zeros

LeetCode 1089: Duplicate Zeros solution in PHP.

class Solution {

    /**
     * @param Integer[] $arr
     * @return NULL
     */
    function duplicateZeros(&$arr) {
        $len = count($arr);
        for ($i=0; $i < $len; $i++) {
            if (0 === $arr[$i]) {
                array_splice($arr, $i++, 0, 0);
            }
        } 
        array_splice($arr, $len);
    }
}

Leave a Reply