2ms Java code with %


  • 0
    Z
    public class Solution {
        public int hammingWeight(int n) {
            int t=0;
            if(n<0){
                n=-n-1;
                while(n!=0){
                    if(n%2==1) t++;
                    n=n/2;
                }
                return 32-t;
            }
            while(n!=0){
                if(n%2==1) t++;
                n=n/2;
            }
            return t;
        }
    }

Log in to reply
 

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