Java [Leetcode 268]Missing Number
题目描述:
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?
解题思路:
题目与Single Number题目本质上是一样的。就是一堆数字里面,只有一个数字是单的,其他都是成双的,找出那个单着的数字。运用异或就行了。
代码如下:
public class Solution {
public int missingNumber(int[] nums) {
int length = nums.length;
int res = 0;
for(int i = 1; i <= length; i++)
res ^= i;
for(int i = 0; i < length; i++)
res ^= nums[i];
return res;
}
}
Java [Leetcode 268]Missing Number的更多相关文章
- LeetCode 268. Missing Number缺失数字 (C++/Java)
题目: Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is mi ...
- LeetCode 268. Missing Number (缺失的数字)
Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missin ...
- [LeetCode] 268. Missing Number 缺失的数字
Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missin ...
- [LeetCode] 268. Missing Number ☆(丢失的数字)
转载:http://www.cnblogs.com/grandyang/p/4756677.html Given an array containing n distinct numbers take ...
- 33. leetcode 268. Missing Number
Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missin ...
- LeetCode - 268. Missing Number - stable_sort应用实例 - ( C++ ) - 解题报告
1.题目大意 Given an array nums, write a function to move all 0's to the end of it while maintaining the ...
- leetcode 268 Missing Number(异或运算的应用)
Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missin ...
- Leetcode 268 Missing Number 位运算
题意:先将0, 1, 2, ..., n放入数组,然后去掉其中一个值,找到那个值. 这题与singe number 是一个类型,变形的地方就是首先需要将0, 1, 2, ..., n再次放入这个数组, ...
- <LeetCode OJ> 268. Missing Number
268. Missing Number Total Accepted: 31740 Total Submissions: 83547 Difficulty: Medium Given an array ...
随机推荐
- vc6命令行编译工程方法
查vc++ 6.0 的 msdn找到下面的命令: msdev FileName [/MAKE "ProjectName – ConfigName | ALL"] [/REBUILD ...
- POJ 1906
#include <iostream> #include <string> #define MAXN 150 using namespace std; int _m[MAXN] ...
- hdu 4038 Stone
思路: 如果负数的个数为偶数则不必改变,为奇数就将最大负数变为正: 对于正数,尽量将1,2变为3即可. 代码如下: #include<cstring> #include<iostre ...
- python之self (转)
首先明确的是self只有在类的方法中才会有,独立的函数或方法是不必带有self的.self在定义类的方法时是必须有的,虽然在调用时不必传入相应的参数. self名称不是必须的,在python中self ...
- NET 使用HtmlAgilityPack抓取网页数据
刚刚学习了XPath路径表达式,主要是对XML文档中的节点进行搜索,通过XPath表达式可以对XML文档中的节点位置进行快速定位和访问,html也是也是一种类似于xml的标记语言,但是语法没有那么严谨 ...
- MAC OS JAVA环境变量配置
在 /etc/profile 中 加上这些 #临时提权 sudo su #输入密码 vi /etc/profile #配置JAVA_HOME,此处路径根据自己的版本填写 JAVA_HOME=&quo ...
- springmvc环境的搭建
最近应公司要求,用了2天时间学了springmvc的搭建,就简单总结一下: springmvc和struts2的比较,因为我是学过struts的,它们都是基于mvc模式而设计的web层框架 它们最大的 ...
- JAVA XMLConfiguration解决StringXML及多节点
import org.apache.commons.configuration.ConfigurationException; import org.apache.commons.configurat ...
- PCB走线角度选择 — PCB Layout 跳坑指南
现在但凡打开SoC原厂的PCB Layout Guide,都会提及到高速信号的走线的拐角角度问题,都会说高速信号不要以直角走线,要以45度角走线,并且会说走圆弧会比45度拐角更好.狮屎是不是这样?PC ...
- Android:文件夹显示红色叹号
有感叹号,说明有的文件损坏或丢失了 解决方法: 右击工程,Build Path..->Configure Build Path...->Java Build Path 可以看到引用的jar ...