[leetcode-260-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 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.assume that A and B are the two elements which we want to find;

2.use XOR for all elements,the result is : r = A^B,we just need to distinguish A from B next step;

3.if we can find a bit ‘1‘ in r,then the bit in corresponding position in A and B must be different.We can use {last = r & (~(r-1))} to get the last bit 1 int r;

4.we use last to divide all numbers into two groups,then A and B must fall into the two distrinct groups.XOR elements in eash group,get the A and B.

vector<int> singleNumber1(vector<int>& nums) {
         int r = 0, n = nums.size(), i = 0, last = 0;
         vector<int> ret(2, 0);

         for (i = 0; i < n; ++i)
             r ^= nums[i];

         last = r & (~(r - 1));
         for (i = 0; i < n; ++i)
         {
             if ((last & nums[i]) != 0)
                 ret[0] ^= nums[i];
             else
                 ret[1] ^= nums[i];
         }

         return ret;
     }

参考:

https://discuss.leetcode.com/topic/34545/share-my-c-solution

时间: 2024-08-06 00:45:10

[leetcode-260-Single Number III]的相关文章

[LeetCode] 260. Single Number III 单独数 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. Example: Input: [1,2,1,3,2,5] Output: [3,5] Note: The order of the result is

Leetcode 260. Single Number III JAVA语言

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 

Leetcode 260 Single Number III 亦或

在一个数组中找出两个不同的仅出现一次的数(其他数字出现两次) 同样用亦或来解决(参考编程之美的1.5) 先去取出总亦或值 然后分类,在最后一位出现1的数位上分类成 ans[0]和ans[1] a&(-a)就是计算出这个数,可以参考树状数组. 最后就是注意(nums[i] & a) == 0要加().注意符号运算优先级 1 class Solution { 2 public: 3 vector<int> singleNumber(vector<int>& nu

[LeetCode] 260. 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 once. For example: Given nums = [1, 2, 1, 3, 2, 5], return [3, 5].

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

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

[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]

【一天一道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

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

问题描述: https://leetcode.com/problems/single-number-iii/ 在一个数组里面,只有两个元素仅出现过1次,其余都出现过两次.找出出现仅一次的那两个(a, b). 要求常量空间,线性时间. 问题解决: 这题用到“神奇的位运算”. 1.因为除了特殊的两个元素,其余两两出现,那么就想到了XOR(异或). 2.从头到尾XOR之后,会得到a xor b 的结果.接下来我们试着把这两个元素分离开来. 3.我们在a xor b 中找到任意一位XOR[diff_po