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. 金蝶KIS客户端修改IP连接服务器的方法

    问题现象:服务器IP变更后,金蝶KIS客户端打开时提示多个错误,并会自动关闭,无法联网登录 1. 到下面位置修改注册表 Windows Registry Editor Version 5.00 [HK ...

  2. spark- PySparkSQL之PySpark解析Json集合数据

    PySparkSQL之PySpark解析Json集合数据 数据样本 12341234123412342|asefr-3423|[{"}] 正菜: #-*- coding:utf-8 –*- ...

  3. python常用函数 F

    filter(callable, list/tuple) 接收一个函数和一个序列,完成元素过滤. 例子: fnmatch(str,str) 使用底层操作系统的大小写敏感规则来匹配模式. 例子: fnm ...

  4. [CodeForces - 1225E]Rock Is Push 【dp】【前缀和】

    [CodeForces - 1225E]Rock Is Push [dp][前缀和] 标签:题解 codeforces题解 dp 前缀和 题目描述 Time limit 2000 ms Memory ...

  5. CPU性能优化

    原创转载请注明出处:https://www.cnblogs.com/agilestyle/p/11521331.html CPU性能指标 根据指标找工具 根据工具查指标 top.vmstat 和 pi ...

  6. 转-调用HTMLTestRunner生成的报告中不能显示用例中print函数的输出

    官方原生的HTMLTestRunner.py支持python2.0版本,python3.0版本的使用需要做一些修改: Python3调用HTMLTestRunner执行用例生成测试报告中,不能正常显示 ...

  7. 如何高效地学好R语言?

    如何高效地学好R语言? 学R语言主要在于5点三阶段: 第一阶段有一点:基础的文件操作(read.*, write.*).数据结构知识,认识什么是数据框(data.frame).列表(list).矩阵( ...

  8. 关于tomcat中的三个端口的作用及其相关细节

    [一]端口内容 tomcat的端口号相关信息: Tomcat admin port——管理端口,允许你远程配置tomcat HTTP——正常的http协议 AJP——Apache JServ Prot ...

  9. 爬取拉勾网所有python职位并保存到excel表格 对象方式

    # 1.把之间案例,使用bs4,正则,xpath,进行数据提取. # 2.爬取拉钩网上的所有python职位. from urllib import request,parse import json ...

  10. js比较日期时间的大小

    var myDate = new Date(); var timed = myDate.toLocaleDateString(); var oDate1 = new Date(item.express ...