Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
门心叼龙
LeetCodeAnimation
提交
c4a29593
L
LeetCodeAnimation
项目概览
门心叼龙
/
LeetCodeAnimation
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
L
LeetCodeAnimation
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
提交
c4a29593
编写于
5月 17, 2019
作者:
M
misterbooo
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
ADD 268
上级
a372d1c6
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
127 addition
and
0 deletion
+127
-0
Readme.md
Readme.md
+1
-0
notes/LeetCode第268号问题:缺失数字.md
notes/LeetCode第268号问题:缺失数字.md
+126
-0
未找到文件。
Readme.md
浏览文件 @
c4a29593
...
...
@@ -61,6 +61,7 @@
| 231 |
[
2的幂
](
https://github.com/MisterBooo/LeetCodeAnimation/tree/master/notes/LeetCode第231号问题:2的幂.md
)
|
| 237 |
[
删除链表中的节点
](
https://github.com/MisterBooo/LeetCodeAnimation/tree/master/notes/LeetCode第237号问题:删除链表中的节点.md
)
|
| 239 |
[
滑动窗口最大值
](
https://github.com/MisterBooo/LeetCodeAnimation/tree/master/notes/LeetCode第239号问题:滑动窗口最大值.md
)
|
| 268 |
[
缺失数字
](
https://github.com/MisterBooo/LeetCodeAnimation/tree/master/notes/LeetCode第268号问题:缺失数字.md
)
|
| 279 |
[
完全平方数
](
https://github.com/MisterBooo/LeetCodeAnimation/tree/master/notes/LeetCode第279号问题:完全平方数.md
)
|
| 283 |
[
移动零
](
https://github.com/MisterBooo/LeetCodeAnimation/tree/master/notes/LeetCode第283号问题:移动零.md
)
|
| 295 |
[
数据流的中位数
](
https://github.com/MisterBooo/LeetCodeAnimation/tree/master/notes/LeetCode第295号问题:数据流的中位数.md
)
|
...
...
notes/LeetCode第268号问题:缺失数字.md
0 → 100644
浏览文件 @
c4a29593
# LeetCode 第 268 号问题:缺失数字
> 本文首发于公众号「五分钟学算法」,是[图解 LeetCode ](<https://github.com/MisterBooo/LeetCodeAnimation>)系列文章之一。
>
> 个人网站:[https://www.cxyxiaowu.com](https://www.cxyxiaowu.com)
今天分享一道很简单的算法题。
题目来源于 LeetCode 上第 268 号问题:缺失数字。题目难度为 Easy,目前通过率为 50.2% 。
## 题目描述
给定一个包含
`0, 1, 2, ..., n`
中
*n*
个数的序列,找出 0 ..
*n*
中没有出现在序列中的那个数。
**说明:**
你的算法应该具有线性时间复杂度。 你可以不使用额外空间来实现吗?
![](
https://raw.githubusercontent.com/MisterBooo/myBlogPic/master/20190516113448.png
)
## 题目解析
这道题目有三种解法。
### 解法一:异或法
和之前那道
**只出现一次的数字**
很类似:
> 只出现一次的数字: 给定一个**非空**整数数组,除了某个元素只出现一次以外,其余每个元素均出现两次。找出那个只出现了一次的元素。
如果我们补充一个完整的数组和原数组进行组合,那所求解的问题就变成了
**只出现一次的数字**
。
将少了一个数的数组与 0 到 n 之间完整的那个数组进行异或处理,因为相同的数字异或会变为了 0 ,那么全部数字异或后,剩下的就是少了的那个数字。
![](
https://raw.githubusercontent.com/MisterBooo/myBlogPic/master/20190516143539.png
)
#### 代码实现1
```
java
class
Solution
{
public
int
missingNumber
(
int
[]
nums
)
{
int
res
=
0
;
//注意数组越界情况
for
(
int
i
=
0
;
i
<
nums
.
length
;
i
++){
// i 表示完整数组中的数字,与原数组中的数字 nums[i] 进行异或,再与保存的结果异或
res
=
res
^
i
^
nums
[
i
];
}
//最后需要与循环中无法使用到的那个最大的数异或
return
res
^
i
;
}
}
```
#### 代码实现2
```
java
class
Solution
{
public
int
missingNumber
(
int
[]
nums
)
{
int
res
=
nums
.
length
;
for
(
int
i
=
0
;
i
<
nums
.
length
;
++
i
){
res
^=
nums
[
i
];
res
^=
i
;
}
return
res
;
}
}
```
### 解法二:求和法
-
求出 0 到 n 之间所有的数字之和
-
遍历数组计算出原始数组中数字的累积和
-
两和相减,差值就是丢失的那个数字
![](
https://raw.githubusercontent.com/MisterBooo/myBlogPic/master/20190516151203.gif
)
```
java
//小吴之前担心会数据溢出,不过估计这题考察的不是这个,所以测试用例没写这种吧,还是能 AC 的
class
Solution
{
public
int
missingNumber
(
int
[]
nums
)
{
int
n
=
nums
.
length
;
int
sum
=
(
n
+
0
)*(
n
+
1
)/
2
;
for
(
int
i
=
0
;
i
<
n
;
i
++){
sum
-=
nums
[
i
];
}
return
sum
;
}
}
```
### 解法三:二分法
将数组进行排序后,利用二分查找的方法来找到缺少的数字,注意搜索的范围为 0 到 n 。
-
首先对数组进行排序
-
用元素值和下标值之间做对比,如果元素值大于下标值,则说明缺失的数字在左边,此时将 right 赋为 mid ,反之则将 left 赋为 mid + 1 。
> 注:由于一开始进行了排序操作,因此使用二分法的性能是不如上面两种方法。
```
java
public
class
Solution
{
public
int
missingNumber
(
int
[]
nums
)
{
Arrays
.
sort
(
nums
);
int
left
=
0
;
int
right
=
nums
.
length
;
while
(
left
<
right
){
int
mid
=
(
left
+
right
)
/
2
;
if
(
nums
[
mid
]
>
mid
){
right
=
mid
;
}
else
{
left
=
mid
+
1
;
}
}
return
left
;
}
}
```
![](
https://bucket-1257126549.cos.ap-guangzhou.myqcloud.com/blog/fz0rq.png
)
\ No newline at end of file
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录