剑指offer【12】- 二进制中1的个数

输入一个整数,输出该数二进制表示中1的个数。其中负数用补码表示。

 1 public class Solution {
 2     public int NumberOf1(int n) {
 3         String str = Integer.toBinaryString(n);
 4         
 5         char [] c = str.toCharArray();
 6         
 7         int t = 0;
 8         for(int i = 0; i < c.length; i++){
 9             if(c[i] == '1'){
10                 t++;
11             }
12         }
13         return t;
14     }
15 }
原文地址:https://www.cnblogs.com/linliquan/p/11305043.html