先将整数转换为对应的二进制的字符串
再转换为字符数组
再统计其中1的个数
public class Solution { public int NumberOf1(int n) { int index = 0; String binaryString = Integer.toBinaryString(n); char[] binaryChars = binaryString.toCharArray(); for (char binaryChar : binaryChars) { if (binaryChar == '1') { index++; } } return index; } }