Leetcode PHP题解--D79 448. Find All Numbers Disappeared in an Array


D79 448. Find All Numbers Disappeared in an Array

题目链接

448. Find All Numbers Disappeared in an Array

题目分析

给定一个1到n的数组,返回其中缺失的数字。

思路

用range得出1到n的数字,再用array_diff和给定的数组计算差集。

最终代码

<?php
class Solution {

    /**
     * @param Integer[] $nums
     * @return Integer[]
     */
    function findDisappearedNumbers($nums) {
        if(!$nums){
            return [];
        }
        return array_diff(range(1,count($nums)),$nums);
    }
}

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


点赞 取消点赞 收藏 取消收藏

<< 上一篇: Leetcode基础刷题之PHP解析(17. Letter Combinations of a Phone Number)

>> 下一篇: Leetcode基础刷题之PHP解析(22. Generate Parentheses)