【LeetCode从零单刷】Single Number III

来源:互联网 发布:高中网络课程 编辑:程序博客网 时间:2024/05/19 00:42

题目:

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?

解答:

如果只有一个落单的数,是不是很好处理?因为相同的数位异或之后答案是0,每个数都进行异或操作之后,是不是就剩下那个单一的数。

 那么,如果我将这一题,分为两个组,每个部分仅包含一个落单的数,其他都是成双成对出现的话,是不是就可以了。

如何区分呢?首先,我们将每个数进行异或操作,得到一个答案 tmp,本质就是那两个落单的数异或后的结果

这里利用一个位运算技巧:如果两个数的异或结果为tmp,则 tmp&(~(tmp - 1)) 的结果就是这两个数最低不相同的那一位。依靠与这一位相与,可以将这两个数区分开来。

对于这两个数之外的其他数呢?相与的结果无所谓,只要保证他们成对出现即可,两个相同的数与这一位相与之后的结果肯定是相同的,必然会分到同一组。

class Solution {public:    vector<int> singleNumber(vector<int>& nums) {        int len = nums.size();                int tmp = 0;        for(int i = 0; i < len; i++)        {            tmp = tmp ^ nums[i];        }        int lastbit = tmp & (~(tmp - 1));        int ansA = 0, ansB = 0;        for(int i = 0; i < len; i++)        {            if(nums[i] & lastbit)            {                ansA ^= nums[i];            }            else{                ansB ^= nums[i];            }        }        vector<int> ans = {ansA, ansB};        return ans;    }};

这里有一个小编程技巧,利用花括号将某些确定的数组成一个vector:vector<int> ans = {ansA, ansB};

0 0
原创粉丝点击