算法系列——Missing Number
来源:互联网 发布:1hhhh.com域名升级访问 编辑:程序博客网 时间:2024/06/07 04:05
题目描述
Given an array containing n distinct numbers taken from 0, 1, 2, …, n, find the one that is missing from the array.
For example,
Given nums = [0, 1, 3] return 2.
Note:
Your algorithm should run in linear runtime complexity. Could you implement it using only constant extra space complexity?
解题思路
位操作
还是考察位操作。.那么思路是既然0到n之间少了一个数,我们将这个少了一个数的数组合0到n之间完整的数组异或一下,那么相同的数字都变为0了,剩下的就是少了的那个数字了。
求和
也可以利用求和公式,算出 0-n的和,然后将数组求和,做差即可。
代码不再给出。
程序实现
public class Solution { public int missingNumber(int[] nums) { int len=nums.length; int XOR=0; for(int i=0;i<=len;i++) XOR^=i; for(int val:nums) XOR^=val; return XOR; }}
阅读全文
0 0
- 算法系列——Missing Number
- 算法题——Missing Number(JAVA)
- Missing Number —— Leetcode
- LeetCode——Missing Number
- leetcode——Missing Number
- Algorithms—268.Missing Number
- 算法系列——Number of Boomerangs
- 算法系列——Palindrome Number
- 算法系列——Ugly Number
- 算法系列——Ugly Number II
- 算法系列——Letter Combinations of a Phone Number
- 算法系列——Number of 1 Bits
- 15算法课程 268. Missing Number
- leetcode——268——Missing Number
- leetcode刷题日记——Missing Number
- Missing number
- Missing number
- Missing number
- maven手动打包pom文件配置
- 莫名其妙的错误之BigDecimal.setsetScale()报错java.lang.ArithmeticException: Rounding necessary
- Open CV小细节
- Rebranding 找规律
- APDU命令集
- 算法系列——Missing Number
- iOS In-App Purchase 内购之问题总结
- 关于驱动隐藏那点事(不触发PG 支持win10)
- 高效Linux 1 概述
- etcd的简单使用
- Java中正则表达式的使用
- May Day Holiday
- iOS之healthKit
- SQlite数据库的C编程接口(四) 绑定参数(Bound Parameters) ——《Using SQlite》读书笔记