Skip to content

Commit 52268bb

Browse files
committed
Leetcode 260
1 parent 808a77b commit 52268bb

File tree

3 files changed

+51
-0
lines changed

3 files changed

+51
-0
lines changed

251-300/260.md

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
## :pencil2:Leetcode之PHP版题目解析(260. Single Number III)
2+
**2020-02-29 吴亲库里 库里的深夜食堂**
3+
****
4+
### :pencil2:描述
5+
**给定一组数字数组,只有两个元素只出现一次,其余元素都出现两次。找出只出现一次的两个元素。**
6+
****
7+
### :pencil2:题目实例
8+
<a href="https://github.com/wuqinqiang/">
9+
​ <img src="https://github.com/wuqinqiang/Lettcode-php/blob/master/images/260.png">
10+
</a>
11+
****
12+
13+
### :pencil2:题目分析
14+
**可以直接复用上一题的代码,用哈希表来做一个值和出现频率的绑定,最终返回频率为1的数。**
15+
16+
```php
17+
18+
/**
19+
* @param Integer[] $nums
20+
* @return Integer[]
21+
*/
22+
function singleNumber($nums) {
23+
$res=[];
24+
$data=[];
25+
for($i=0;$i<count($nums);$i++){
26+
if(!$data[$nums[$i]]) $data[$nums[$i]]=0;
27+
$data[$nums[$i]]++;
28+
}
29+
return array_keys($data,1,false);
30+
//或者遍历
31+
// foreach($data as $key=>$value){
32+
// if($data[$key]==1) $res[]=$key;
33+
// if(count($res)==2) break;
34+
// }
35+
36+
// return $res;
37+
}
38+
```
39+
40+
**上述代码时间复杂度O(n),空间复杂度O(n),时间复杂度不能再进行优化了,空间复杂度可以优化为O(1),可以利用异或运算,留给你们实现。**
41+
### 联系
42+
43+
<a href="https://github.com/wuqinqiang/">
44+
​ <img src="https://github.com/wuqinqiang/Lettcode-php/blob/master/qrcode_for_gh_c194f9d4cdb1_430.jpg" width="150px" height="150px">
45+
</a>
46+
47+
48+
49+
50+

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,7 @@
196196
**251-300**
197197
- [Leetcode257](https://github.com/wuqinqiang/Lettcode-php/blob/master/251-300/257.md)
198198
- [Leetcode258](https://github.com/wuqinqiang/Lettcode-php/blob/master/251-300/258.md)
199+
- [Leetcode260](https://github.com/wuqinqiang/Lettcode-php/blob/master/251-300/260.md)
199200
- [Leetcode263](https://github.com/wuqinqiang/Lettcode-php/blob/master/251-300/263.md)
200201
- [Leetcode268](https://github.com/wuqinqiang/Lettcode-php/blob/master/251-300/268.md)
201202
- [Leetcode283](https://github.com/wuqinqiang/Lettcode-php/blob/master/251-300/283.md)

images/260.png

55.5 KB
Loading

0 commit comments

Comments
 (0)