Leetcode PHP题解--D51 136. Single Number


D51 136. Single Number

题目链接

136. Single Number

题目分析

返回给定数组中,只出现了一次的元素。

思路

用array_count_values计算元素出现的次数。

再用array_search返回出现次数为1的元素。

最终代码

<?php
class Solution {

    /**
     * @param Integer[] $nums
     * @return Integer
     */
    function singleNumber($nums) {
        $values = array_count_values($nums);
        return array_search(1,$values);
    }
}

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


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

<< 上一篇: Leetcode基础刷题之PHP解析( 98. Validate Binary Search Tree)

>> 下一篇: Leetcode PHP题解--D52 496. Next Greater Element I