C#LeetCode刷题之#268-缺失数字(Missing Number)
问题
该文章的最新版本已迁移至个人博客【比特飞】,单击链接 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)的更多相关文章
- C#LeetCode刷题之#788-旋转数字(Rotated Digits)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3967 访问. 我们称一个数 X 为好数, 如果它的每位数字逐个地 ...
- C#LeetCode刷题之#13-罗马数字转整数(Roman to Integer)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3842 访问. 罗马数字包含以下七种字符: I, V, X, L, ...
- C#LeetCode刷题之#191-位1的个数(Number of 1 Bits)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/4052 访问. 编写一个函数,输入是一个无符号整数,返回其二进制表 ...
- 【leetcode刷题笔记】Excel Sheet Column Number
Related to question Excel Sheet Column Title Given a column title as appear in an Excel sheet, retur ...
- C#LeetCode刷题之#263-丑数(Ugly Number)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3862 访问. 编写一个程序判断给定的数是否为丑数.丑数就是只包含 ...
- [Swift]LeetCode268. 缺失数字 | Missing Number
Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missin ...
- C#LeetCode刷题之#136-只出现一次的数字(Single Number)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/4046 访问. 给定一个非空整数数组,除了某个元素只出现一次以外, ...
- C#LeetCode刷题-位运算
位运算篇 # 题名 刷题 通过率 难度 78 子集 67.2% 中等 136 只出现一次的数字 C#LeetCode刷题之#136-只出现一次的数字(Single Number) 53.5% 简单 ...
- C#LeetCode刷题-数组
数组篇 # 题名 刷题 通过率 难度 1 两数之和 C#LeetCode刷题之#1-两数之和(Two Sum) 43.1% 简单 4 两个排序数组的中位数 C#LeetCode刷题之#4-两个排序数组 ...
随机推荐
- 微信小程序开发部署
一.开发准备 1,想要开发微信小程序,必须要有一个AppId,如果没有可以去注册一个. https://mp.weixin.qq.com/进入注册页面,点击上方注册. 2,点击选择“小程序”出 ...
- Eclipse点击空格总是自动补全代码怎么办,如何自动补全代码,代码提示
Eclipse点击空格总是自动补全不想要的代码说明大家配置的时候出现了一点错误,下面的步骤将会解决它, 网上部分经验需要大家更改代码非常繁琐,下面是一个简单的步骤方法 步骤一:打开eclipse依次点 ...
- Makefile中的一个坑
问题描述:Makefile中,我想将一个变量的后缀全部进行替换,如将所有的.c后缀变成.d后缀 方法:$(CUR_SOURCE: .c = .d ) 说明:查阅相关资料,了解到上述这种语法就可以将所有 ...
- C++语法小记---类模板
类模板 类模板和函数模板类似,主要用于定义容器类 类模板可以偏特化,也可以全特化,使用的优先级和函数模板相同 类模板不能隐式推倒,只能显式调用 工程建议: 模板的声明和实现都在头文件中 成员函数的实现 ...
- Docker 基础知识 - 使用绑定挂载(bind mounts)管理应用程序数据
绑定挂载(bind mounts)在 Docker 的早期就已经出现了.与卷相比,绑定挂载的功能有限.当您使用绑定挂载时,主机上的文件或目录将挂载到容器中.文件或目录由其在主机上的完整或相对路径引用. ...
- django-rest-framework-源码解析001-整体框架
简介 Django Rest Framework是一个强大且灵活的工具包,主要用以构建RESTful风格的Web API. Django REST Framework(简称DRF)可以在Django的 ...
- Thymeleaf从入门到精通
什么是Thymeleaf 大家好,我是bigsai,今天我们来学习Thymeleaf,如果你对Thymeleaf比较陌生也不要紧,它很容易学习与理解,并有着自己鲜明的特色. 开始之前,我们依旧问一个问 ...
- iframe和DataForm
一.iframe使用 iframe在一个页面中,相当于整个window窗口的子窗口,可通过页面的元素结构查看. <div> <p>学习iframe</p> < ...
- express,node.js实现获取本地文件夹下面的全部图片文件
http://www.luyixian.cn/javascript_show_169354.aspx 按照网上的教程试了多次,处理了各种结果后还有报错, 最后的报错是cant find module ...
- MySQL数据库常见命令
数据库的操作 MySQL服务器的常见命令 开启:sudo service mysql start关闭:sudo service mysql stop重启:sudo service mysql rest ...