//我的作法code
public class Solution {it
public int NumberOf1(int n) { String a= Integer.toBinaryString(n); char b[]=a.toCharArray(); int num1=0; for(int i=0;i<b.length;i++){ if(b[i]=='1') num1++; } return num1; }
}io
//大佬的作法1class
public class Solution {時間
public int NumberOf1(int n) { return Integer.bitCount(n); }while
}co
//大佬作法2 這個運行的時間更短return
public class Solution {
public int NumberOf1(int n) {
int count = 0;
while(n!= 0){ count++; n = n & (n - 1); } return count; }
}