[C++][Single Number II] short simple code


#1
int Solution::singleNumber(const vector<int> &A) {
    vector<long> vec(32, 0);
    long temp = 0;
    for(int i=0; i<31; i++) {
        for(auto it : A)
            vec[i] += it&(1<<i);
        if(vec[i]%3)
            temp += 1<<i;
    }
    return temp;
}

#2

Hey! I tried this in python,

class Solution:
    # @param A : tuple of integers
    # @return an integer
    def singleNumber(self, A):
        A = list(A)
        count = 0
        temp = 0
        for i in range(32):
            for item in A:
                count += item & (1<<i)
                count = count%3
            if(count%3):
                temp += 1<<1
            count = 0
        return temp

but facing wrong answer, Please tell where does it go wrong?


#3

Hi,

In your code block you have used
temp += 1<<1
instead of
temp += 1<<i

Otherwise, the code works perfectly fine.

class Solution:
	# @param A : tuple of integers
	# @return an integer
	def singleNumber(self, A):
        A = list(A)
        temp = 0
        for i in range(31):
            count = 0
            for item in A:
                count += item & (1<<i)
            if(count%3):
                temp += 1<<i
        return temp

PS:
Markdown has a code view format.
Please use it, at-least, for python code.
Indentations are important. : )

Cheers.


#4

I am ashamed :disappointed_relieved:. Maybe I should get my eyes checked up. Sorry, I didn’t know there was a feature. I’m a newbie here. Changed it now.
Thanks for the reply!
cheers!