Leetcode之PHP版题目解析(136. Single Number)

栏目: PHP · 发布时间: 7年前

内容简介:题目描述

2 0 1 9 - 3 - 1 1        

Leetcode之 <a href='https://www.codercto.com/topics/18749.html'>PHP</a> 版题目解析(136. Single Number)

题目描述

, , , .

题目示例

1 [ 2 , 2 , 1 ] , 1 , 2 [ 4 , 1 , 2 , 1 , 2 ] , 4 .

题目思路

( 使 , , , , , , )

/**
     * @param Integer[] $nums
     * @return Integer
     */
    function singleNumber($nums) {
        $data=[];
       foreach($nums  as $num) {
           if(!in_array($num,$data)) {
               array_push($data,$num);
           }else{
               $index=array_search($num,$data);
               array_splice($data,$index,1);
           }
       }
        return $data[0];
    }

, . , , f o r , O ( n * n ) , O ( n ) . .

Leetcode之PHP版题目解析(136. Single Number)

( ^ ) , ^ , 0 , ^ , 1 . .

/**
     * @param Integer[] $nums
     * @return Integer
     */
    function singleNumber($nums) {
        $res=0;
        for($i=0;$i<count($nums);$i++) {
            $res ^=$nums[$i];
        }
        return $res;
    }

Leetcode之PHP版题目解析(136. Single Number)

, , O ( n ) , O ( 1 ) .


以上所述就是小编给大家介绍的《Leetcode之PHP版题目解析(136. Single Number)》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Language Implementation Patterns

Language Implementation Patterns

Terence Parr / Pragmatic Bookshelf / 2010-1-10 / USD 34.95

Knowing how to create domain-specific languages (DSLs) can give you a huge productivity boost. Instead of writing code in a general-purpose programming language, you can first build a custom language ......一起来看看 《Language Implementation Patterns》 这本书的介绍吧!

Markdown 在线编辑器
Markdown 在线编辑器

Markdown 在线编辑器

RGB HSV 转换
RGB HSV 转换

RGB HSV 互转工具

RGB CMYK 转换工具
RGB CMYK 转换工具

RGB CMYK 互转工具