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 ...
随机推荐
- IE6兼容inline-block的方法
或许有朋友会对IE不支持 display:inline-block 属性,表示疑问或者反对.说:“我在 IE 中对 a 或者 span 等内联元素使用 display:inline-block 一直是 ...
- Web Server 和 HTTP 协议
https://toutiao.io/posts/xm2fr/preview 一直在找实习,有点什么东西直接就在evernote里面记了,也没时间来更新到这里.找实习真是个蛋疼的事,一直找的是困难模式 ...
- POJ 1745
#include <iostream> #define MAXN 10005 using namespace std; int _m[MAXN]; ]; int main() { //fr ...
- grunt下cssmin的配置参数
每个目标的具体设置,需要参考该模板的文档minify目标的参数具体含义如下: expand:如果设为true,就表示下面文件名的占位符(即*号)都要扩展成具体的文件名. cwd:需要处理的文件(inp ...
- Android Service 的一些笔记
绑定服务: 用于间接调用服务里面的方法.如果调用者Activity被销毁了,服务也跟着销毁了,服务也会跟着销毁. 开启服务: 不可以调用服务里面的方法.如果调用者的Activity退出了,服务还会长期 ...
- zend studio 10破解/汉化(转发)
转发:http://blog.csdn.net/qq1355541448/article/details/16807429 Zend Studio 10正式版破解及汉化 2013年03月12日 ⁄ P ...
- 李洪强iOS开发之零基础学习iOS开发】【02-C语言】01-概述
前面已经给大家介绍了iOS开发相关的一些基础知识,其实iOS开发就是开发iPhone\iPad上的软件,而要想开发一款软件,首先要学习程序设计语言.iOS开发需要学习的主要程序设计语言有:C语言.C+ ...
- Hibernate逍遥游记-第13章 映射实体关联关系-003单向多对多
0. 1. drop database if exists SAMPLEDB; create database SAMPLEDB; use SAMPLEDB; create table MONKEYS ...
- 计算XX年的某月某日是当年的第多少天?是星期几?
/** * 编写程序,计算2013年的某月某日是当年的第多少天?是星期几?已知2013年元旦是星期二. */ import java.util.Scanner; public class Date { ...
- Go语言博客
http://www.cnblogs.com/concurrency/p/4293613.html#3130523