Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit f2fedad

Browse files
committed
Feat: 268
1 parent ed6f8ce commit f2fedad

File tree

1 file changed

+55
-0
lines changed

1 file changed

+55
-0
lines changed

‎problems/268-missing-number.md

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
## 题目
2+
3+
4+
* 268. 丢失的数字
5+
6+
给定一个包含 [0, n] 中 n 个数的数组 nums ,找出 [0, n] 这个范围内没有出现在数组中的那个数。
7+
8+
## 思路
9+
10+
## 代码
11+
12+
```php
13+
class Solution {
14+
15+
/**
16+
* @param Integer[] $nums
17+
* @return Integer
18+
*/
19+
function missingNumber($nums) {
20+
21+
// 方法1,异或方式
22+
//0和任意数异或的结果为那个数 相同的两个数异或为0
23+
$res = sizeof($nums);
24+
for ($i = 0; $i < sizeof($nums); ++$i) {
25+
$res ^= $nums[$i];
26+
$res ^= $i;
27+
}
28+
return $res;
29+
30+
/*
31+
// 方法2,hash表
32+
$arr = [];
33+
for ($i = 0; $i < sizeof($nums); $i++) {
34+
$arr[$nums[$i]] = $nums[$i];
35+
}
36+
for ($i = 0; $i < sizeof($nums)+1; $i++) {
37+
if (!isset($arr[$i])) {
38+
return $i;
39+
}
40+
}
41+
return -1;
42+
*/
43+
/*
44+
// 方法3,数学计算
45+
$indexSum = $sum = 0;
46+
for($i = 0; $i < sizeof($nums); $i++){
47+
$indexSum += ($i+1);
48+
$sum += $nums[$i];
49+
}
50+
return $indexSum - $sum;
51+
*/
52+
}
53+
}
54+
```
55+

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /