Single Number III——LeetCode
Given an array of numbers nums, in which exactly two elements appear only once and all the other elements appear exactly twice. Find the two elements that appear only once.
For example:
Given nums = [1, 2, 1, 3, 2, 5], return [3, 5].
Note:
- The order of the result is not important. So in the above example,
[5, 3]is also correct. - Your algorithm should run in linear runtime complexity. Could you implement it using only constant space complexity?
题目大意:一个数组,有两个元素出现了一次,其余元素都出现两次,让用线性时间复杂度和常量空间复杂度找出这两个数。
解题思路:考虑位操作,对所有元素做异或操作,那么最后得到的值就是要求的两个元素异或得到的,那么找出其中为1的某一位,说明这一位两个数一个为0,一个为1,以这一位为标准,把数组的元素分为两组A、B,那么要求的两个元素肯定是一个在A组,一个在B组,而对这两组各自做异或操作,就可以得到两个数就是要求的。
public class SingleNumberIII {
public static void main(String[] args) {
int[] res = singleNumber(new int[]{1, 2, 1, 3, 5, 2});
System.out.println(Arrays.toString(res));
}
public static int[] singleNumber(int[] nums) {
int[] res = new int[2];
if (nums == null || nums.length == 0) {
return res;
}
for (int i = 0; i < nums.length; i++) {
res[0] ^= nums[i];
}
int pos = 0;
for (int i = 0; i < 32; i++) {
int offset = 1 << i;
if ((res[0] & (offset)) == offset) {
pos = i;
break;
}
}
res[0] = 0;
int mask = 1 << pos;
for (int i = 0; i < nums.length; i++) {
if ((mask & nums[i]) == mask) {
res[0] ^= nums[i];
} else {
res[1] ^= nums[i];
}
}
return res;
}
}
Single Number III——LeetCode的更多相关文章
- Single Number III leetcode java
问题描述: Given an array of numbers nums, in which exactly two elements appear only once and all the oth ...
- Single Number III - LeetCode
Given an array of numbers nums, in which exactly two elements appear only once and all the other ele ...
- leetcode 136 Single Number, 260 Single Number III
leetcode 136. Single Number Given an array of integers, every element appears twice except for one. ...
- LeetCode 260. Single Number III(只出现一次的数字 III)
LeetCode 260. Single Number III(只出现一次的数字 III)
- leetcode 136. Single Number 、 137. Single Number II 、 260. Single Number III(剑指offer40 数组中只出现一次的数字)
136. Single Number 除了一个数字,其他数字都出现了两遍. 用亦或解决,亦或的特点:1.相同的数结果为0,不同的数结果为1 2.与自己亦或为0,与0亦或为原来的数 class Solu ...
- Single Number III(LintCode)
Single Number III Given 2*n + 2 numbers, every numbers occurs twice except two, find them. Example G ...
- LeetCode136 Single Number, LeetCode137 Single Number II, LeetCode260 Single Number III
136. Single Number Given an array of integers, every element appears twice except for one. Find that ...
- 【刷题-LeeetCode】260. Single Number III
Single Number III Given an array of numbers nums, in which exactly two elements appear only once and ...
- [LeetCode] Single Number III 单独的数字之三
Given an array of numbers nums, in which exactly two elements appear only once and all the other ele ...
随机推荐
- Asp.net Mvc 第二回 UrlRouting
一.什么是UrlRouting 你可以使用UrlRouting来配置一些URL的映射,使用户可以按你的规则来访问网站. 使用UrlRouting,一定要规定URL模式,它包括一个位置标识,它将在你请求 ...
- lucene 抛出的异常(分享)
1) too many boolean clauses异常 例如: String keyword=".......";//(keyword的长度太长) Query indexQue ...
- MVC+EF 的增删改查操作
1. //创建EF映射对象数据集 static Models.db_JiaoYouEntities DbDeleteData = new Models.db_JiaoYouEntities(); 2. ...
- 内存储与外存储(IO流的应用)
存储分为内存储,外存储(Sd卡存储),其中还涉及IO流的应用. 我们先来看内存储,在布局中EditView中输入的内容,通过点击按钮,分别进行保存,读取,删除的操作,读取时显示在下面的TextView ...
- 写个接口的实现类,在方法的前面加了@Override居然报错
据说这是jdk的问题,@Override是JDK5就已经有了,但有个小小的Bug,就是不支持对接口的实现,认为这不是Override 而JDK6修正了这个Bug,无论是对父类的方法覆盖还是对接口的实现 ...
- Android学习笔记(广播机制)
1.Android的广播机制介绍 收听收音机也是一种广播,在收音机中有很多个广播电台,每个广播电台播放的内容都不相同.接受广播时广播(发送方)并不在意我们(接收方)接收到广播时如何处理.好比我们收听交 ...
- Android布局管理器(表格布局)
表格布局有TableLayout所代表,TableLayout继承了LinearLayout,因此他的本质依然是LinearLayout. 表格布局采用行.列的形式来进行管理,在使用的时候不需要声明多 ...
- 不能错过的XCode插件
以便自己记忆网上找的! 以下是搜集的一些有力的XCode插件. 全能搜索家CodePilot 2.0 你要找的是文件?是文件夹?是代码?Never Mind,CMD+SHIFT+X调出CodePilo ...
- Spring MVC中注解 @ModelAttribute
1.@ModelAttribute放在方法之上,在当前Control内的所有方法映射多个URL的请求,都会执行该方法 @ModelAttribute public void itemsCommon(H ...
- select控件变成可输入状态
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...