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-两个排序数组 ...
随机推荐
- 第一章:Activity的生命周期和启动模式
Activity是Android中四大组件之首,所以需要重视,尤其是启动方式,在AndroidManifest.xml中的注册信息 (一)Activity的生命周期 1.1.1 正常情况下的生命周期 ...
- Python Ethical Hacking - BeEF Framework(2)
Basic BeEF commands: Login the BeEF Control Panel, and go to Commands page. Create Alert Dialog: Run ...
- python 列表的创建以及基本操作
<python编程从入门到实践> 第三章 列表简介 用"[]"来建立列表,例如:letter = [a,b,c]; 用"[]"来提取列表元素,例如l ...
- 回文树(回文自动机)(PAM)
第一个能看懂的论文:国家集训队2017论文集 这是我第一个自己理解的自动机(AC自动机不懂KMP硬背,SAM看不懂一堆引理定理硬背) 参考文献:2017国家集训队论文集 回文树及其应用 翁文涛 参考博 ...
- P1852 跳跳棋 [LCA思想+二分答案]
题目描述 跳跳棋是在一条数轴上进行的.棋子只能摆在整点上.每个点不能摆超过一个棋子. 我们用跳跳棋来做一个简单的游戏:棋盘上有\(3\)颗棋子,分别在\(a,b,c\)这三个位置.我们要通过最少的跳动 ...
- queue stack for STL
前不久发现自己vector有些不会了,于是想起了queue和stack. 有一个小故事,,,某天我跟自己打赌我queue没有写博园,结果打开一看竟然不知什么时候写过了,而且(QAQ)还有一定的浏览量了 ...
- 动态规划入门(dp)
dp的基本思想,是把大问题转化成一个个小问题,然后递归解决. 所以本质思想的话还是递归. dp最重要的是要找到状态转移方程,也就是把大问题化解的过程. 举个例子 一个数字金字塔 在上面的数字三角形中寻 ...
- Python字符串更新
Python字符串更新:截取字符串的某一部分 和 其他字符串进行拼接. 注:可以修改字符串的值,但修改的不是内存中的值,而是创建新的字符串. 1.使用字符串常量进行更新: # 使用字符串常量 strs ...
- Django学习路5_更新和删除数据库表中元素
查找所有的元素 Student.objects.all() 查找单个元素 Student.objects.get(主键=值) 主键 pk = xxx 更新数据库数据后进行保存 stu.save() 删 ...
- PHP sqrt() 函数
实例 返回不同数的平方根: <?phpecho(sqrt(0) . "<br>");echo(sqrt(1) . "<br>"); ...