问题描述:

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:

  1. The order of the result is not important. So in the above example, [5, 3] is also correct.
  2. Your algorithm should run in linear runtime complexity. Could you implement it using only constant space complexity?

提示:bit manipulation

一、位运算

//参照single number 的方法,将所有数异或运算,得到result为两个single number的异或;
//找到result中最低位为1的那一位,是两个single number不同的地方,根据这个bit,可以将nums数组中的数分为A、B两组;
//然后分别在A和B中寻找single number即可

public static int[] singleNumber_Bit(int[] nums){

    int result = 0;
for(int i = 0; i < nums.length; i++){
result = result ^ nums[i];
}
int[] res = new int[2];
//找到result二进制表示中最右侧的1
int pos = result & ( ~ (result - 1 )); //统计一个int型整数的二进制表示中有多少个1,可以采用 n = n & (n - 1);来从右到左逐个统计1的个数
for(int i = 0 ; i < nums.length; i++){ //将nums分为A B两组来分别求single number
if((pos & nums[i]) != 0){
res[0] = res[0] ^ nums[i];
} else {
res[1] = res[1] ^ nums[i];
}
}
return res;
}

Single Number III leetcode java的更多相关文章

  1. Single Number II leetcode java

    问题描述: Given an array of integers, every element appears three times except for one. Find that single ...

  2. Single Number III——LeetCode

    Given an array of numbers nums, in which exactly two elements appear only once and all the other ele ...

  3. Single Number III - LeetCode

    Given an array of numbers nums, in which exactly two elements appear only once and all the other ele ...

  4. leetcode 136 Single Number, 260 Single Number III

    leetcode 136. Single Number Given an array of integers, every element appears twice except for one. ...

  5. LeetCode 260. Single Number III(只出现一次的数字 III)

    LeetCode 260. Single Number III(只出现一次的数字 III)

  6. leetcode 136. Single Number 、 137. Single Number II 、 260. Single Number III(剑指offer40 数组中只出现一次的数字)

    136. Single Number 除了一个数字,其他数字都出现了两遍. 用亦或解决,亦或的特点:1.相同的数结果为0,不同的数结果为1 2.与自己亦或为0,与0亦或为原来的数 class Solu ...

  7. Single Number III(LintCode)

    Single Number III Given 2*n + 2 numbers, every numbers occurs twice except two, find them. Example G ...

  8. 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 ...

  9. 【刷题-LeeetCode】260. Single Number III

    Single Number III Given an array of numbers nums, in which exactly two elements appear only once and ...

随机推荐

  1. Shiro源码分析

    1.入口类:AbstractAuthenticator 用户输入的登录信息经过其authenticate方法: public final AuthenticationInfo authenticate ...

  2. Difference between ID and control.ClientID OR why use control.ClientID if I can access control through ID

     https://stackoverflow.com/questions/3743582/difference-between-id-and-control-clientid-or-why-use-c ...

  3. 【配置】log4j.properties 详解与配置步骤

    一.Log4j基本使用方法 Log4j由三个重要的组件构成:[日志信息的优先级],[日志信息的输出目的地],[日志信息的输出格式]. 日志信息的优先级从高到低有ERROR.WARN. INFO.DEB ...

  4. [学习一个] Matlab GUI 学习笔记 Ⅰ

    Matlab GUI 学习笔记 Ⅰ 1. Foreword Matlab 是严格意义上的编程语言吗?曾经有人告诉我他是通过 Matlab 学会了面对对象编程,我是不信的,但这依然不妨碍它在特殊领域的强 ...

  5. Lintcode449-Char to Integer-Naive

    Description Convert a char to an integer. You can assume the char is in ASCII code (See Definition, ...

  6. BZOJ 3673: 可持久化并查集(可持久化并查集+启发式合并)

    http://www.lydsy.com/JudgeOnline/problem.php?id=3673 题意: 思路: 可持久化数组可以用可持久化线段树来实现,并查集的查询操作和原来的一般并查集操作 ...

  7. CSDN-markdown编辑器语法——字体、字号与颜色

     Markdown是一种可以使用普通文本编辑器编写的标记语言,通过类似HTML的标记语法,它可以使普通文本内容具有一定的格式.但是它本身是不支持修改字体.字号与颜色等功能的!   CSDN-markd ...

  8. 关于js中splice方法返回的结果

    一.前言 刚刚在使用splice()方法,发现这个方法返回的是删除后的数组元素,如果要获取删除指定元素后的数组,直接调用原来的数组即可!因为splice()会改变原来数组!之前对splice()方法一 ...

  9. UESTC 1697 简单GCD问题(一) 筛法

    简单GCD问题(一) Time Limit: 1500/500MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others) 秦队长给你两 ...

  10. Spark之standalone模式

    standalone hdfs:namenode是主节点进程,datanode是从节点进程 yarn:resourcemanager是主节点进程,nodemanager是从节点进程 hdfs和yarn ...