问题

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/4056 访问。

给定一个包含 0, 1, 2, ..., n 中 n 个数的序列,找出 0 .. n 中没有出现在序列中的那个数。

输入: [3,0,1]

输出: 2

输入: [9,6,4,2,3,5,7,0,1]

输出: 8

说明:

你的算法应具有线性时间复杂度。你能否仅使用额外常数空间来实现?


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

Input: [3,0,1]

Output: 2

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

Output: 8

Note:

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


示例

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/4056 访问。

public class Program {

    public static void Main(string[] args) {
int[] nums = null; nums = new int[] { 9, 6, 4, 2, 3, 5, 7, 0, 1 };
var res = MissingNumber(nums);
Console.WriteLine(res); res = MissingNumber2(nums);
Console.WriteLine(res); Console.ReadKey();
} private static int MissingNumber(int[] nums) {
//求和法
if(!nums.Contains(0)) return 0;
var sum = 0;
foreach(var num in nums) {
sum += num;
}
int max = nums.Length;
return (1 + max) * max / 2 - sum;
} private static int MissingNumber2(int[] nums) {
//异或法
//异或满足交换律,我们有以下公式
//A ^ B = B ^ A
//(A ^ B) ^ C = A ^ (B ^ C)
//A ^ C ^ B ^ A ^ C = A ^ A ^ C ^ C ^ B = B
//对于数组 [2, 3, 4, 0]
//res初始值为 0
//异或 0 ^ (2 ^ 1) ^ (3 ^ 2) ^ (4 ^ 3) ^ (0 ^ 4)
//= (0 ^ 0) ^ (1) ^ (2 ^ 2) ^ (3 ^ 3) ^ (4 ^ 4) = 1
int res = 0;
for(int i = 0; i < nums.Length; i++)
res ^= nums[i] ^ (i + 1);
return res;
} }

以上给出2种算法实现,以下是这个案例的输出结果:

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/4056 访问。

8
8

分析:

显而易见,2种算法的时间复杂度均为:  。

C#LeetCode刷题之#268-缺失数字(Missing Number)的更多相关文章

  1. C#LeetCode刷题之#788-旋转数字(Rotated Digits)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3967 访问. 我们称一个数 X 为好数, 如果它的每位数字逐个地 ...

  2. C#LeetCode刷题之#13-罗马数字转整数(Roman to Integer)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3842 访问. 罗马数字包含以下七种字符: I, V, X, L, ...

  3. C#LeetCode刷题之#191-位1的个数(Number of 1 Bits)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/4052 访问. 编写一个函数,输入是一个无符号整数,返回其二进制表 ...

  4. 【leetcode刷题笔记】Excel Sheet Column Number

    Related to question Excel Sheet Column Title Given a column title as appear in an Excel sheet, retur ...

  5. C#LeetCode刷题之#263-丑数(Ugly Number)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3862 访问. 编写一个程序判断给定的数是否为丑数.丑数就是只包含 ...

  6. [Swift]LeetCode268. 缺失数字 | Missing Number

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

  7. C#LeetCode刷题之#136-只出现一次的数字(Single Number)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/4046 访问. 给定一个非空整数数组,除了某个元素只出现一次以外, ...

  8. C#LeetCode刷题-位运算

    位运算篇 # 题名 刷题 通过率 难度 78 子集   67.2% 中等 136 只出现一次的数字 C#LeetCode刷题之#136-只出现一次的数字(Single Number) 53.5% 简单 ...

  9. C#LeetCode刷题-数组

    数组篇 # 题名 刷题 通过率 难度 1 两数之和 C#LeetCode刷题之#1-两数之和(Two Sum) 43.1% 简单 4 两个排序数组的中位数 C#LeetCode刷题之#4-两个排序数组 ...

随机推荐

  1. html-webpack-plugin在html中插入数据

    html-webpack-plugin在html中插入数据 <!DOCTYPE html> <html> <head> <meta charset=" ...

  2. react实战 : react 与 canvas

    有一个需求是这样的. 一个组件里若干个区块.区块数量不定. 区块里面是一个正六边形组件,而这个用 SVG 和 canvas 都可以.我选择 canvas. 所以就变成了在 react 中使用 canv ...

  3. Redis的字符串底层是啥?为了速度和安全做了啥?

    面试场景 面试官:Redis有哪些数据类型? 我:String,List,set,zset,hash 面试官:没了? 我:哦哦哦,还有HyperLogLog,bitMap,GeoHash,BloomF ...

  4. 设计模式:command模式

    目的:将命令设计成类的形式,并可以组织成队列 优点: 在需要的情况下,可以比较容易地将命令记入日志 可以容易的实现对请求的撤销和重做 由于新的具体命令类不影响其他的命令类,因此增加新的具体命令类很容易 ...

  5. coding如何绑定二次验证码_虚拟MFA_两步验证_身份验证?

    Coding.net 是一个面向开发者的云端开发平台,提供 Git/SVN 代码托管.任务管理.在线 WebIDE.Cloud Studio.开发协作.文件管理.Wiki 管理.提供个人服务及企业管理 ...

  6. python爬虫入门(2)----- lxml

    lxml 简介 lxml使用xpath对xml进行解析,XPath 是一门在 XML 文档中查找信息的语言.XPath 可用来在 XML 文档中对元素和属性进行遍历. 参考官方文档:https://l ...

  7. Monster Audio 使用教程(三)多音轨录音、播放

    在工作站音轨上,把需要进行录音的音轨的录音按钮点亮,然后点击液晶屏旁边的[录音]按钮,开始录音  导出干声 如果希望录音后,导出干声(干声为录下的原始声音,不受效果器的作用),用其他宿主软件进行处理, ...

  8. React native项目后期调整UI总结

    字体 fontSize: 14, marginLeft: 10, marginTop: 2, fontFamily: 'ABBvoiceCNSG-Regular', 布局 paddingHorizon ...

  9. django-模板之标签

    目录 模板 模版是纯文本文件,可以生成任何基于文本的文件格式,比如HTML,XML,CSV等.Django模版语言致力于在性能和简单性上取得平衡.Django的模版系统并不是简单的将Python嵌入到 ...

  10. 最近建了一个.net源码共享群,群共享有大量网友分享的.net(C#)商业源码

    .net源码共享群 324087998. 本群创建于2013/6/21: 群里都是.net(C#)程序开发人员,群共享有大量网友分享的.net(C#)商业源码.比如:DTCMS旗舰版,hishop微分 ...