位运算(4)——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]return2.Note:
Your algorithm should run in linear runtime complexity. Could you implement it using only constant extra space complexity?
排序后再找时间复杂度不合题意。
0……n异或,然后nums[i]异或:
public class Solution {
public int missingNumber(int[] nums) {
int n = nums.length;
int res = 0;
for(int i=1; i<=n; i++) {
res = res ^ i;
}
for(int i=0; i<n; i++) {
res = res ^ nums[i];
}
return res;
}
}
用异或解决的题目还有 “有几个数,其中只有一个数有奇数个,其余的数有偶数个,找出奇数个的那个数”
位运算(4)——Missing Number的更多相关文章
- 【leetcode】Single Number && Single Number II(ORZ 位运算)
题目描述: Single Number Given an array of integers, every element appears twice except for one. Find tha ...
- 136.137.260. Single Number && 位运算
136. Single Number 意思就是给你一堆数,每个数都出现了两次,只有一个数只出现了一次,找出这个数 位运算(和c艹一样) &:按位与 |:按位或 ^:异或(一样为0,不一样为1) ...
- HDU 3006 The Number of set(位运算 状态压缩)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3006 题目大意:给定n个集合,每个集合都是由大于等于1小于等于m的数字组成,m最大为14.由给出的集合 ...
- LeetCode - 136. Single Number - ( C++ ) - 解题报告 - 位运算思路 xor
1.题目大意 Given an array of integers, every element appears twice except for one. Find that single one. ...
- The number of set(位运算+状态dp)一道十分美妙的题目哦!!!!!!
Given you n sets.All positive integers in sets are not less than 1 and not greater than m.If use the ...
- leetcode - 位运算题目汇总(上)
最近在看位运算的知识,十分感叹于位运算的博大精深,正好leetcode有 Bit Manipulation 的专题,正好拿来练练手. Subsets 给出一个由不同的数字组成的数组,枚举它的子数组(子 ...
- LeetCode解题中位运算的运用
位运算是我最近才开始重视的东西,因为在LeetCode上面刷题的时候发现很多题目使用位运算会快很多.位运算的使用包含着许多技巧(详细可以参考http://blog.csdn.net/zmazon/ar ...
- <LeetCode OJ> 268. Missing Number
268. Missing Number Total Accepted: 31740 Total Submissions: 83547 Difficulty: Medium Given an array ...
- 【LeetCode】位运算 bit manipulation(共32题)
[78]Subsets 给了一个 distinct 的数组,返回它所有的子集. Example: Input: nums = [,,] Output: [ [], [], [], [,,], [,], ...
- C#LeetCode刷题-位运算
位运算篇 # 题名 刷题 通过率 难度 78 子集 67.2% 中等 136 只出现一次的数字 C#LeetCode刷题之#136-只出现一次的数字(Single Number) 53.5% 简单 ...
随机推荐
- 【bzoj4836】二元运算 分治FFT
Description 定义二元运算 opt 满足 现在给定一个长为 n 的数列 a 和一个长为 m 的数列 b ,接下来有 q 次询问.每次询问给定一个数字 c 你需要求出有多少对 (i, j) 使 ...
- 查看SELinux状态及关闭SELinux
查看SELinux状态: 输入:/usr/sbin/sestatus -v SELinux status: enabled ##开启状态 关闭SELinux 修改vi /etc/s ...
- java基础_01
一.java中的数据类型 1.基本数据类型:四类八种 byte(1),boolean(1),short(2),char(2),int(4),float(4),long(8),double(8); 2. ...
- elasticsearch安装bigdest插件
bigdest下载地址:https://github.com/hlstudio/bigdesk 对于bigdest插件来说,安装是非常简单的. 只要三个步骤就可以了,下载——解压——进入到_side文 ...
- [DEBUG]-[org.mybatis.spring.SqlSessionFactoryBean.buildSqlSessionFactory(SqlSessionFactoryBean.java:431)] 一直在创建sqlsession工厂原因
今天在做开发项目的时候出现一直在控台输出创建sqlsessionfactory'这个代码, tomcat一直在控制台上输出这个内容无法停止下来,那么到底是什么原因呢, 我们可以在输出信息上看到有个wa ...
- Idea 软件Project项目的jar依赖关系设置方法
1.查看所依赖的jar文件 (1)File--->Project Structure (2)Modules--->project01---->dependencies,可见所缺少的j ...
- LeetCode6. Z字形变换
描述 The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows li ...
- 应大数据时代而写了个磁力搜索的网页- WWW.MOVIH.COM 磁力
应大数据时代而写了个磁力搜索的网页- 索马里搜索磁力 http://www.,movih.com/ BT磁力示例网站:WWW.MOVIH.COM 采用分布式架构: JAVA系统:JBOSS7+EJB3 ...
- HDU - 3038 带权并查集
这道题我拖了有8个月... 今天放假拉出来研究一下带权的正确性,还有半开半闭的处理还有ab指向的一系列细节问题 #include<iostream> #include<algorit ...
- Linux批量杀掉挂掉的进程
$ `ps aux | grep test | grep -v grep | awk '{print $2}'` 杀掉含有test且不含有grep的进程,后面的 awk '{print $2}' 是进 ...