What's wrong with this problem??


  • 0
    M

    I thought the input should be valid int, but actually it's not. Is there any problem with this algorithm?

        public int hammingWeight(int n) {
        if(n==1||n==0)
        return n;
        int temp=n;
        int count=0;
        int res=1;
        while(res!=0){
            res=temp/2;
            int mod=temp%2;
            if(mod==1)
            count++;
            temp=res;
        }
        return count;
    }

  • 0
    S

    The code definition is

    class Solution {
    public:
        int hammingWeight(uint32_t n) {
            
        }
    };
    

    not

    class Solution {
    public:
        int hammingWeight(int n) {
            
        }
    };

  • 0
    M

    But in java it's int...For java, there is no definition for unsigned int


Log in to reply
 

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