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 ...
随机推荐
- 【CodeForces】【#286】Div.2
T_T越来越水了,这次只做出A+B. A题为了代码简单直接枚举(插入位置和插入字符) //CF #286 Div.2 A #include<vector> #include<stri ...
- [设计模式] 12 代理模式 proxy
在GOF的<设计模式:可复用面向对象软件的基础>一书中对代理模式是这样说的:为其他对象提供一种代理以控制对这个对象的访问.结合上面的游戏代理的例子和下面的图,我们来进行分析一下.以前你是这 ...
- RFID开发笔记 Alien阅读器文档
1. 开机使用serial connect,完成boot后使用TCP/IP协议与主机通信 2.TagList,是一个活跃标签的列表,这里活跃的含义是在一个间隔里被监听到.如果一个标签之前没有被监听到, ...
- 【C++基础】 指针&字符串&数组
先贴代码,总结以后再写,和5中内存分配方式密切相关 PS: str 返回整个字符串,str[0],*str返回首字符h char *strA(){ char str[]="hello!&qu ...
- HDU 1829 A Bug's Life(种类并查集)
思路:见代码吧. #include <stdio.h> #include <string.h> #include <set> #include <vector ...
- Rsyslog配置
http://blog.itpub.net/30015039/viewspace-1457009/
- 为Android Studio 项目手动下载gradle
在http://developer.android.com/samples/index.html上下载的例子,导入Android Studio的时候,第一件事就是下载项目对应版本的gradle.gra ...
- Mac 如果一定要写个锁屏程序的话就这样
package test; import java.io.BufferedReader; import java.io.File; import java.io.IOException; import ...
- 搜索之BM25和BM25F模型
www.netfoucs.com/article/wdxin1322/94603.html#
- Hibernate逍遥游记-第13章 映射实体关联关系-002用主键映射一对一(<one-to-one constrained="true">、<generator class="foreign">)
1. <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hi ...