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

Example 1

Input: [3,0,1]
Output: 2

Example 2

Input: [9,6,4,2,3,5,7,0,1]
Output: 8

我的版本:

package leetCode;import java.util.Arrays;/*** Created by luoluo on 2018/1/27.先进行快排,然后在判断*/
public class MissingNumber {public static void main(String[] args) {int[] a = {3,0,1};missingNumber(a);System.out.println(Arrays.toString(a));}public static int missingNumber(int[] nums) {int n = nums.length;sort(nums, 0, n-1);if(nums[0] != n ){return n;}else if(nums[nums.length-1] !=0){return 0;}for (int i = 0; i < nums.length-1; i++) {if(nums[i]-nums[i+1]!=1){return nums[i]-1;}}return 0;}public static void sort(int[] nums ,int bg,int end){if (bg >= end) {return;}int partition = partition(nums, bg, end);sort(nums, bg, partition - 1);sort(nums, partition + 1, end);}public static int partition(int[] nums,int bg, int end){int l = nums[bg];int j = bg;for (int i = bg+1; i <= end; i++) {if(nums[i]>l){j++;swap(nums, i, j);}}swap(nums, j, bg);return j;}public static void swap(int[] nums ,int a ,int b) {int temp = nums[a];nums[a] = nums[b];nums[b] = temp;}
}

大神的版本:

public static int missingNumber(int[] nums) {int sum = nums.length;for (int i = 0; i < nums.length; i++)sum += i - nums[i];return sum;
}

转载于:https://www.cnblogs.com/luozhiyun/p/8367149.html

268. Missing Number相关推荐

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

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

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

  3. leetcode 268. Missing Number

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

  4. 268 missing number

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

  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. Science:领导决策的计算和神经生物学基础
  2. nginx响应高并发参数配置
  3. gentoo doc web site
  4. openssl https 单向认证连接成功示例
  5. opencv笔记(3):图像镜像
  6. IIFE(立即执行函数表达式)
  7. Tokyo cabinet HDB– 初始化
  8. java实现线性回归(简单明了,适合理解)
  9. Light OJ 1316 A Wedding Party 最短路+状态压缩DP
  10. 浅谈Object Pascal的指针
  11. 电机控制初学入门资料_电机控制如何入门
  12. 看完这篇解决你99%的运维安全陋习,快别踩坑了!
  13. linux系统Nginx下limit_req模块burst参数超详细解析
  14. 小颗粒积木步骤图纸_loz小颗粒钻石积木拼图图纸谁有
  15. 基本面量化与美林时钟模型
  16. 归一化mysql函数_数据归一化和两种常用的归一化方法
  17. Win10 内置 OpenSSH 使用密钥连不上的问题解决
  18. 查看电脑内存个数、主频(工作频率)、容量、位宽等的方法总结
  19. su [user] 和 su - [user]的区别
  20. Android开发最近新闻和功能都在这里了

热门文章

  1. 跨进程信息交互真个费事。
  2. CentOS-6.4安装配置Nginx
  3. Windows Phone 网络HttpWebRequest用法
  4. 创业互联网公司如何搭建自己的技术框架
  5. webapi 返回类型
  6. 在Linux安装配置Tomcat 并部署web应用 ( 三种方式 )
  7. 使用JMS实现请求/应答程序
  8. 下载并导出数据到execl中
  9. Android中mesure过程详解
  10. Ruby的资源站点和开发工具列表