My CPP Solution


  • 0
    H
    class Solution {
    public:
    	vector<int> singleNumber(vector<int>& nums) {
    		int res = nums[0];
    		vector<int> ans(2);
    		for (int i = 1; i < nums.size();++i)
    		{
    			res = res^nums[i];
    		}
    		int firstLowBit = 1;
    		while (!(res&firstLowBit))
    		{
    			firstLowBit = firstLowBit << 1;
    		}
    		for (auto num : nums)
    		{
    			if (num & firstLowBit)
    			{
    				ans[0] ^=  num;
    			}
    			else 
    			{
    				ans[1] ^=  num;
    			}
    		}
    		return ans;
    	}
    };

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.