advisory


  • -1
    L

    public class Solution {
    // you need to treat n as an unsigned value
    public int hammingWeight(int n) {
    if(n==0)
    {
    return 0;
    }
    int count = 0;//记录1的位数
    int m = 0;
    while(n>0){
    m = n&1;
    if(m==1)
    count++;
    n = n>>1;
    }
    return count;
    }
    }

    Why is my answer wrong??


  • 0
    2

    use ``` to format code. e.g.

    public ...{
        int x=0;
        ...
    }
    

Log in to reply
 

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