问题:给定一个包含n个不同数字的数组,从0开始,例如0,1,2,…n。找到数组中缺了那个数字。例如输入nums=[0,1,3],输出2。
思路:高斯 从0到n的和为sum=(1+n)*n/2。只要计算数组的和与sum的差是多少就可以了。

   public int missingNumber(int[] nums) {int n = nums.length;int ssum = (1+n)*n/2;int sum = 0;for(int i=0;i<nums.length;i++){sum += nums[i];}return ssum-sum;} 

 收获:
1 据说可以用位运算。我想数数1的个数。思路不通。 根据 a^b^b=a,对同一个数进行两次异或,或留下原来的数。同时对数组的小标、数组中的元素的值进行异或。留下的元素就是丢失的元素。
public int missingNumber(int[] nums) {
int xor = nums.length;
for(int i=0;i

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;elseleft = mid +1;}return left;}

参考资料
1 问题
2 讨论

268 missing number相关推荐

  1. 268. Missing Number

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

  2. Leet Code OJ 268. Missing Number [Difficulty: Medium]

    题目: Given an array containing n distinct numbers taken from 0, 1, 2, -, n, find the one that is miss ...

  3. 268. Missing Number(缺失数字)

    题目地址:https://leetcode.com/problems/missing-number/description/ Given an array containing n distinct ...

  4. leetcode 268. Missing Number

    原地哈希.代码没怎么优化,思路清晰. int missingNumber(vector<int>& nums) {int size = nums.size();for (int i ...

  5. LeetCode 268. Missing Number--Python解法--数学题

    LeetCode 268. Missing Number–Python解法–数学题 LeetCode题解专栏:LeetCode题解 LeetCode 所有题目总结:LeetCode 所有题目总结 大部 ...

  6. PAT甲级1144 The Missing Number:[C++题解]哈希表

    文章目录 题目分析 题目链接 题目分析 来源:acwing 分析: 把所有的数放进一个hash表,然后从1开始遍历,看每个数是不是在hash表中,输出第一个不在表中的元素. ac代码 #include ...

  7. LeetCode之Missing Number

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

  8. 1144 The Missing Number (20 分)

    Given N integers, you are supposed to find the smallest positive integer that is NOT in the given li ...

  9. C#LeetCode刷题之#268-缺失数字(Missing Number)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/4056 访问. 给定一个包含 0, 1, 2, ..., n 中  ...

最新文章

  1. WCF加密操作(包括证书和证书+帐号密码)
  2. c++ 类中静态变量 static
  3. 【MySQL】数据库死锁查询及处理
  4. 电信/移动/联通同台秀肌肉 集体表态:云计算市场“不能丢”!
  5. Spotify模式并非“敏捷涅磐”
  6. atexit函数的执行顺序_Python模块学习:atexit
  7. 转换json_使用Java将JSON转换为POJO
  8. oracle lpad 字符集,oracle Lpad()函数和Rpad()函数的用法
  9. win10 x64中 windbg x64 安装配置符号库
  10. [转] MongoDB shell 操作 (查询)
  11. 毕业论文封面LaTeX模板
  12. 易居IPO后首份成绩单透露了什么秘密?|一点财经
  13. win7安装mysql后“应用程序无法启动因为应用程序的并行配置不正
  14. 对数函数log()用法
  15. matlab 绘制四棱锥,素描教程:怎么绘制四棱锥
  16. win10安装oracle客户端
  17. Linux编程和windows编程的区别
  18. 图像增强(空间域)——灰度变换
  19. Software Architecture Pattern(Mark Richards)笔记
  20. TCP第四次挥手后为什么要等待2MSL后才断开链接?等待时间为什么是2MSL?

热门文章

  1. Spark实战之读写HBase
  2. VMware 报错“Intel VT-x处于禁止状态”
  3. 博客园的博客终于开通了
  4. 添加组合索引时,做相等运算字段应该放在最前面
  5. 我的JavaScript for Visual Studio插件开源了
  6. .net Redis缓存优化提高加载速度和服务器性能(二)
  7. android+button+不可点击置灰,android:tint 给imagebutton着色 按钮灰色
  8. YII2 服务器验证码不显示
  9. 07 | 链表(下):如何轻松写出正确的链表代码?
  10. 万能点位图软件_红色网格衣+很多漂亮的花边像素图,绣手帕花边的十字绣图...