Description

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

Example 1:

  1. Input: [,,]
  2. Output:

Example 2:

  1. Input: [,,,,,,,,]
  2. Output:

Note:
Your algorithm should run in linear runtime complexity. Could you implement it using only constant extra space complexity?

问题描述,一个数组包含n个不重复的数从0,1,2,3,...n 找到那个不在数组中的数

我的思路:先将数组排序再进行比较.但是这样子效率很低,时间复杂度在O=n+排序所需时间;

  1. public int MissingNumber(int[] nums) {
  2. Array.Sort(nums);
  3.  
  4. for(int i=; i < nums.Length; i++){
  5. if(i != nums[i])
  6. return i;
  7. }
  8. return nums.Length;
  9. }

第二种思路: 从0到n的和为(0+n)(n+1)/2 再计算数组的和。两和的差就是结果。该算法时间复杂度为O(n)

  1. public int MissingNumber(int[] nums) {
  2. int n = nums.Length;
  3. int sum = n*(n+)/;
  4. int sums = nums.Sum();
  5. return sum-sums;
  6. }

LeetCode Array Easy 268. Missing Number的更多相关文章

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

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

  2. [LeetCode&Python] Problem 268. Missing Number

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

  3. 【LeetCode】268. Missing Number

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

  4. LeetCode - 268. Missing Number - stable_sort应用实例 - ( C++ ) - 解题报告

    1.题目大意 Given an array nums, write a function to move all 0's to the end of it while maintaining the ...

  5. 268. Missing Number@python

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

  6. LeetCode Array Easy 485. Max Consecutive Ones

    Description Given a binary array, find the maximum number of consecutive 1s in this array. Example 1 ...

  7. LeetCode Array Easy 283. Move Zeroes

    Description Given an array nums, write a function to move all 0's to the end of it while maintaining ...

  8. Java [Leetcode 268]Missing Number

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

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

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

随机推荐

  1. sq - 压缩一个排过序的单词列表 unsq - 解压一个排过序的单词列表

    总览 (SYNOPSIS) sq < infile > outfile unsq < infile > outfile 描述 (DESCRIPTION) sq 压缩 一个 排过 ...

  2. 93-基于ATOM E3825的3U PXIe 主板控制器

    基于ATOM E3825的3U PXIe 主板控制器 一.板卡概述: 本主板采用intel ATOM 处理器 E3825 设计主板控制器,是一种低成本.低功耗解决方案.板卡采用Intel Bay Tr ...

  3. openstack创建一个虚拟机的过程

      为什要用云? 一.简单的说就是对资源更加合理的分配,使用,比如硬件的数量,带宽等等这些,因为你不能机器买来不需要了再卖掉(当然也可以),带宽跟机房签合同得来一年的,中间不够了也不能加,超了也不退钱 ...

  4. LOJ3119. 「CTS2019 | CTSC2019」随机立方体 二项式反演

    题目传送门 https://loj.ac/problem/3119 现在 BZOJ 的管理员已经不干活了吗,CTS(C)2019 和 NOI2019 的题目到现在还没与传上去. 果然还是 LOJ 好. ...

  5. Java字符串流学习

    字符串流 定义:字符串流,以一个字符为数据源,来构造一个字符流. 作用:在Web开发中,我们经常要从服务器上获取数据,数据返回的格式通过一个字符串(XML.JSON),我们需要把这个字符串构造为一个字 ...

  6. Ubunu12.04 vncserver xstartup配置文件

    以下有效: #!/bin/sh # Uncomment the following two lines for normal desktop: unset SESSION_MANAGER #exec ...

  7. Docker常规操作

    原创转载请注明出处:https://www.cnblogs.com/agilestyle/p/11601853.html Docker 常⽤命令 镜像相关 • docker pull <imag ...

  8. springMVC带参数请求重定向

    SpirngMVC返回逻辑视图名 可以分下面几种情况: 1. servlet进行请求转发,返回到jsp页面,如  return "index.jsp" ; 2. servlet 返 ...

  9. UIView响应事件的两个方法

    参考自:https://blog.csdn.net/mushaofeng1990/article/details/62434349 用户触摸屏幕后的事件传递过程: //方法A-(UIView *)hi ...

  10. S1.2 Python开发规范指南

    参考链接 Python风格规范 分号 不要在行尾加分号, 也不要用分号将两条命令放在同一行. 行长度 每行不超过80个字符 例外: 长的导入模块语句 注释里的URL 不要使用反斜杠连接行. Pytho ...