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:

Input: [,,]
Output:

Example 2:

Input: [,,,,,,,,]
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+排序所需时间;

public int MissingNumber(int[] nums) {
Array.Sort(nums); for(int i=; i < nums.Length; i++){
if(i != nums[i])
return i;
}
return nums.Length;
}

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

public int MissingNumber(int[] nums) {
int n = nums.Length;
int sum = n*(n+)/;
int sums = nums.Sum();
return sum-sums;
}

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. shell脚本实现ftp上传下载文件

    前段时间工作中需要将经过我司平台某些信息核验数据提取后上传到客户的FTP服务器上,以便于他们进行相关的信息比对核验.由于包含这些信息的主机只有4台,采取的策略是将生成的4个文件汇集到一个主机上,然后在 ...

  2. spring boot 不连接数据库启动

    Consider the following:    If you want an embedded database (H2, HSQL or Derby), please put it on th ...

  3. ORACLE 查询所有表、外键、主键等信息

    Select   a.Owner 外键拥有者, a.Table_Name 外键表, c.Column_Name 外键列, b.Owner 主键拥有者, b.Table_Name 主键表, d.Colu ...

  4. Sass-@each

    @each 循环就是去遍历一个列表,然后从列表中取出对应的值. @each 循环指令的形式: @each $var in <list> 如果你没有接触过列表,也不要紧,他也非常简单. 在下 ...

  5. bzoj4007 & loj2111 [JLOI2015]战争调度 复杂度分析+树上背包

    题目传送门 https://lydsy.com/JudgeOnline/problem.php?id=4007 https://loj.ac/problem/2111 题解 同 [NOI2006]网络 ...

  6. 推荐Html Table和Markown互转的网站Table Convert Online

    网站名称:https://tableconvert.com/ 进入网站可以看到可以Table 转为Markdown.JSON.XML.SQL 多种格式 Table(4×5)定义Table的行数和列数: ...

  7. java 根据excel模板导出文件

    <!--读取excel文件,配置POI框架的依赖--> <dependency> <groupId>org.apache.poi</groupId> & ...

  8. Sticks

    题目链接 题意:给你一组等长木棒,然后他随意砍断成n个木棒,木棒长度不一,但你知道分别是多少,要你求出原始木棒可能的最小长度. 思路:首先那个原始木棒的长度肯定是其总长度的约数,然后也肯定大于等于所有 ...

  9. Minimal Power of Prime

    题目链接 题意:输入n,求所有质因子幂的最小值.n奇大无比. 思路:先对n所有n开五次方根的质因子约完,然后如果没有除尽的话,因子最多也就4个了,所以幂数大于1的情况有p1^4,p1^3, p1^2  ...

  10. selenium问题之定位不到元素(NoSuchElementException)

    在使用selenium+爬虫的时候,经常会遇到一个问题,就是NoSuchElementException,定位不到元素的问题 一,打开了新页面,一般selenium点击新链接跳转打开了一个新页面,那么 ...