问题

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 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. OSCP Learning Notes - Buffer Overflows(5)

    Generating Shellcode & Gaining Root 1.Generate the shellcode on Kali Linux. LHOST is the IP of K ...

  2. P5198 [USACO19JAN]Icy Perimeter S (洛谷) (水搜索)

    同样是因为洛谷作业不会写…… 写(水)博客啦. 直接放题目吧,感觉放在代码框里好看点 Farmer John要开始他的冰激凌生意了!他制造了一台可以生产冰激凌球的机器,然而不幸的是形状不太规则,所以他 ...

  3. k8s(00)入门知识介绍

    系列文章说明 本系列文章,可以基本算是 老男孩2019年王硕的K8S周末班课程 笔记,根据视频来看本笔记最好,否则有些地方会看不明白 需要视频可以联系我 k8s概念入门 目录 系列文章说明 k8s概念 ...

  4. STL源码剖析:序

    STL源码包含哪些内容 容器:STL的核心 适配器:容器都是在一种最底层的基础容器上使用适配器实现 空间配置器:提供内存的管理 迭代器:由于遍历容器中的数据 算法:由于操作容器中的数据,如排序,拷贝, ...

  5. 题解 SP1841 【PPATH - Prime Path】

    模拟赛考到了这个题,但我傻傻的用了\(DFS\),于是爆了零 后来才想明白,因为搜索树的分支很多,但答案的深度却又没有那么深,所以在这里\(BFS\),而\(DFS\)一路搜到底的做法则会稳稳地\(T ...

  6. 【IDEA】HTML通过servlet3.0注解名提交表单到servlet类找不到页面的问题

    IDEA一时爽,摸不着头的BUG火葬场 这个问题困扰我整整一天一夜,先是代码检查路径设置找不出问题,后面换tomcat版不行,抱着侥幸心理换IDEA版本意料之中还是没解决问题. 都快想秃了最后终于完美 ...

  7. MyBatis--动态插入多条数据

    MySQL支持的一种插入多行数据的INSERT语句写法是 INSERT INTO 表名 (字段名1,字段名2,字段名3) VALUES (值1,值2,值3,...),(值1,值2,值3,...)... ...

  8. 虚拟机 NAT 网络静态ip配置记录

    环境 VMware 15.5.5 build-16285975 centos7 虚拟网络编辑器配置 位置 编辑 --> 虚拟网络编辑器 示例 以下以 192.168.18.0 网段ip为例,实际 ...

  9. python学习笔记1 -- 函数式编程之高阶函数 filter

    filter 函数用于过滤序列,与map 和reduce函数类似,作为高阶函数,他们也是同样的使用方法,filter(参数1, 参数2),参数1是一个函数,而参数2是一个序列. filter的作用是根 ...

  10. PHP trigger_error() 函数

    定义和用法 trigger_error() 函数创建用户自定义的错误消息. trigger_error() 函数用于在用户指定的条件下触发一个错误消息.它可以与内建的错误处理程序一起使用,或者与由 s ...