给出一个包含 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 缺失的数字的更多相关文章

  1. [LeetCode] 268. Missing Number 缺失的数字

    Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missin ...

  2. [LeetCode] 268. Missing Number ☆(丢失的数字)

    转载:http://www.cnblogs.com/grandyang/p/4756677.html Given an array containing n distinct numbers take ...

  3. 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 ...

  4. &lt;LeetCode OJ&gt; 268. Missing Number

    268. Missing Number Total Accepted: 31740 Total Submissions: 83547 Difficulty: Medium Given an array ...

  5. [LeetCode] Missing Number 丢失的数字

    Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missin ...

  6. 【LeetCode】268. Missing Number

    Missing Number Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one ...

  7. 268. Missing Number@python

    Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missin ...

  8. LeetCode 268. Missing Number (缺失的数字)

    Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missin ...

  9. 268. Missing Number序列中遗失的数字

    [抄题]: Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is ...

随机推荐

  1. android中webview的实现

    设置从当前页面打开链接,而不是跳转到系统默认浏览器打开: webview.setWebViewClient(new WebViewClient(){ @Override public boolean ...

  2. Spring Boot - how to configure port

    https://stackoverflow.com/questions/21083170/spring-boot-how-to-configure-port

  3. pycharm下运行和调试scrapy项目

    1. 新建项目 默认在本地已经新建了一个scrapy爬虫项目 2. 打开项目 点击open à 选择刚刚那个本地的scrapy项目meijutt100 3. 项目结构 各个py文件的作用不作介绍,不懂 ...

  4. spring mvc 访问静态资源404

    访问比如css js出现404提示 在spring的配置文件中加上如下代码即可 <!-- 静态资源404 --> <mvc:resources location="/res ...

  5. hdu oj 1285 确定比赛名次

    hdu oj 1285 确定比赛名次 题目: 确定比赛名次 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K ...

  6. mysql 將時間戳直接轉換成日期時間

    from_unixtime()是MySQL裏的時間函數 Sql代碼 select uid,userid,username,email,FROM_UNIXTIME(addtime,'%Y年%m月%d') ...

  7. checkstyle+ant生成checkstyle报告

    <?xml version="1.0" encoding="UTF-8" ?> <project name="tibim" ...

  8. EEPlat的控制器概念

    控制器是EEPlat平台界面层部分的核心概念.平台中界面展示都是通过平台的各种控制器综合控制输出的. EEPlat平台的界面层模型採用了HMVC模式.HMVC模式的採用使得EEPlat平台界面层可以实 ...

  9. Fragment进阶(五)-----&gt;监听fragment回退事件

    activity_main.xml <? xml version="1.0" encoding="utf-8"?> <LinearLayout ...

  10. iPhone微信防止撤销插件开发

    导语: 随着移动时代的发展以及微信的普及流行,越来越多的用户使用微信发送消息,但经常出现撤销消息的情况.因此需要一款微信防止消息撤回插件,微信用户可以防止对方撤回消息,看到对方发出的任何消息,妈妈再也 ...