268 Missing Number 缺失的数字
给出一个包含 0, 1, 2, ..., n 中 n 个数的序列,找出 0 .. n 中没有出现在序列中的那个数。
案例 1
输入: [3,0,1]
输出: 2
案例 2
输入: [9,6,4,2,3,5,7,0,1]
输出: 8
注意事项:
您的算法应该以线性复杂度运行。你能否仅使用恒定的额外空间复杂度来实现它?
详见:https://leetcode.com/problems/missing-number/description/
Java实现:
方法一:
class Solution {
public int missingNumber(int[] nums) {
int res=nums.length;
int i=0;
for(int num:nums){
res^=num;
res^=i;
++i;
}
return res;
}
}
方法二:
class Solution {
public int missingNumber(int[] nums) {
int n=nums.length;
int sum=0;
for(int num:nums){
sum+=num;
}
return (int)(0.5*n*(n+1)-sum);
}
}
方法三:
用二分查找法算出中间元素的下标,然后用元素值和下标值之间做对比,如果元素值大于下标值,则说明缺失的数字在左边,此时将r赋为m,反之则将l赋为m+1。排序的时间复杂度都不止O(n),但是在面试的时候,有可能数组就是排好序的,那么此时用二分查找法肯定要优于上面两种方法。
class Solution {
public int missingNumber(int[] nums) {
Arrays.sort(nums);
int l=0;
int r=nums.length;
while(l<r){
int m=(l+r)>>1;
if(nums[m]>m){
r=m;
}else{
l=m+1;
}
}
return r;
}
}
参考:https://www.cnblogs.com/grandyang/p/4756677.html
268 Missing Number 缺失的数字的更多相关文章
- [LeetCode] 268. Missing Number 缺失的数字
Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missin ...
- [LeetCode] 268. Missing Number ☆(丢失的数字)
转载:http://www.cnblogs.com/grandyang/p/4756677.html Given an array containing n distinct numbers take ...
- LeetCode 268. Missing Number缺失数字 (C++/Java)
题目: Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is mi ...
- <LeetCode OJ> 268. Missing Number
268. Missing Number Total Accepted: 31740 Total Submissions: 83547 Difficulty: Medium Given an array ...
- [LeetCode] Missing Number 丢失的数字
Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missin ...
- 【LeetCode】268. Missing Number
Missing Number Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one ...
- 268. Missing Number@python
Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missin ...
- LeetCode 268. Missing Number (缺失的数字)
Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missin ...
- 268. Missing Number序列中遗失的数字
[抄题]: Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is ...
随机推荐
- Java Web 总结
Java Servlet 总结 Servlet 简介 Servlet 是什么? 运行在Web服务器上的应用程序,作为浏览器和服务器之间的中间层. 与CGI功能类似,优点在于 性能更好 在Web服务器的 ...
- POJ 3233_Matrix Power Series
题意: 求n*n矩阵的幂和 分析: 逐个加起来时间复杂度太高,通过在矩阵中套个矩阵和,再利用矩阵快速幂,最后时间复杂度为O(n3logn) 代码: #include<cstdio> #in ...
- Webstrom打开太慢
让webstrom将安装包目录屏蔽,settings-搜索file types-在ignore file and folders加入node_modules目录,操作方式如下:
- 条款十五: 让operator=返回*this的引用
c++程序员经常犯的一个错误是让operator=返回void,这好象没什么不合理的,但它妨碍了连续(链式)赋值操作,所以不要这样做. 一般情况下几乎总要遵循operator=输入和返回的都是类对象的 ...
- 抢占式内核与非抢占式内核中的自旋锁(spinlock)的差别
一.概括 (1)自旋锁适用于SMP系统,UP系统用spinlock是作死. (2)保护模式下禁止内核抢占的方法:1.运行终端服务例程时2.运行软中断和tasklet时3.设置本地CPU计数器preem ...
- SSL和SSH的差别
有人说,SSH通常是用来提供安全的登录用的.SSL仅仅是一个在协议层中增加的一层用来提供安全. SSH工作在TCP之上,能够在启动一个SSH应用后.在其通道里执行其他协议的应用.如邮件. ...
- 关于mysql建立索引 复合索引 索引类型
这两天有个非常强烈的感觉就是自己在一些特别的情况下还是hold不住,脑子easy放空或者说一下子不知道怎么去分析问题了,比方,问"hash和btree索引的差别",这非常难吗.仅仅 ...
- JavaScriptSerializer 序列号datatime时少了8小时
有人说主要的因素是在于JSON格式不直接支持日期和时间. 简单一点处理办法是ToLocalTime()一下:dt = dt.ToLocalTime(); 参考http://blog.csdn.net/ ...
- GuiLite 1.2 发布(希望通过这100+行代码来揭示:GuiLite的初始化,界面元素Layout,及消息映射的过程)
经过开发群的长期验证,我们发现:即使代码只有5千多行,也不意味着能够轻松弄懂代码意图.痛定思痛,我们发现:虽然每个函数都很简单(平均长度约为30行),可以逐个击破:但各个函数之间如何协作,却很难说明清 ...
- C++中UINT32和INT32以及int,BOOL和bool的差别
在AndroidHAL层开发中,编写C++代码的时候.遇到了数据类型的困扰.经过查找资料,总结例如以下: 1.UNIT32和int以及INT32的差别: (1).int默认是signed int.也就 ...