11.二进制码

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

两个api的运用:

Integer.toBinaryString(n)

public class Solution {
public int NumberOf1(int n) {
char[] chars = Integer.toBinaryString(n).toCharArray();

  int count = 0;
  for(int i= 0;i < chars.length; i++) {
    if(chars[i] == '1') {
      count++;
    }
  }


  return count;
  }
}

猜你喜欢

转载自www.cnblogs.com/wzQingtTian/p/10661841.html