Lintcode: Single Number III

Given 2*n + 2 numbers, every numbers occurs twice except two, find them.

Example
Given [1,2,2,3,4,4,5,3] return 1 and 5

Challenge
O(n) time, O(1) extra space.

利用bitwise XOR的特点,n个数(0或1),如果1的个数为奇数,则n个数bitwise XOR结果为1,否则为0

先将所有的数异或,得到的将是x和y以后之后的值n。 找到这个数n的为1的某一位(为了方便就取最右边为1的一位, n & ~(n-1),再将这一位为1的数异或,其余的数异或,得到的就是x和y的值。

 1 public class Solution {
 2     /**
 3      * @param A : An integer array
 4      * @return : Two integers
 5      */
 6     public List<Integer> singleNumberIII(int[] A) {
 7         // write your code here
 8         ArrayList<Integer> res = new ArrayList<Integer>();
 9         res.add(0);
10         res.add(0);
11         int n = 0;
12         for (int elem : A) {
13             n ^= elem;
14         }
15         n = n & (~(n-1));
16         for (int elem : A) {
17             if ((elem & n) != 0) {
18                 res.set(0, res.get(0)^elem);
19             }
20             else res.set(1, res.get(1)^elem);
21         }
22         return res;
23     }
24 }
时间: 2024-08-25 00:01:16

Lintcode: Single Number III的相关文章

260. Single Number III

260. Single Number III DescriptionHintsSubmissionsDiscussSolution DiscussPick One 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 o

leetcode Single Number III

题目连接 https://leetcode.com/problems/single-number-iii/ Single Number III Description 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

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 single one. (Easy) Note:Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory? 分析: 第一问属于技巧题,做过就会,

[LeetCode][JavaScript]Single Number III

Single Number III 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]

136. Single Number &amp;&amp; 137. Single Number II &amp;&amp; 260. Single Number III

136. Single Number Given an array of integers, every element appears twice except for one. Find that single one. Note:Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory? Subscribe to see which co

[LintCode] Single Number 单独的数字

Given 2*n + 1 numbers, every numbers occurs twice except one, find it. Have you met this question in a real interview? Yes Example Given [1,2,2,1,3,4,3], return 4 Challenge One-pass, constant extra space. LeetCode上的原题,请参见我之前的博客Single Number. class So

260. Single Number III [medium] (Python)

题目链接 https://leetcode.com/problems/single-number-iii/ 题目原文 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

【一天一道LeetCode】#260. Single Number III

一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 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 a

LeetCode OJ 之 Single Number III (唯一的数字-三)

题目: 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 or