NumberOf1Bits(leetcode191)
生活随笔
收集整理的這篇文章主要介紹了
NumberOf1Bits(leetcode191)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
2019獨角獸企業重金招聘Python工程師標準>>>
rite a function that takes an unsigned integer and returns the number of '1'?bits it has (also known as the?Hamming weight).
Example 1:
Input: 11 Output: 3 Explanation: Integer 11 has binary representation 00000000000000000000000000001011Example 2:
Input: 128 Output: 1 Explanation: Integer 128 has binary representation 00000000000000000000000010000000使用API
public static int hammingWeight(int n) {return Integer.bitCount(n); }API里面寫的沒看懂,自己寫寫
//在java中不能定義unsign int 用long類型代替 測試2147483648 public static int hammingWeight2(long n) {int num = 0;while(n > 0) {num += n & 1;n = n >>> 1;}return num; }轉載于:https://my.oschina.net/u/2277632/blog/2933888
總結
以上是生活随笔為你收集整理的NumberOf1Bits(leetcode191)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Spring Boot中对自然语言处理工
- 下一篇: 提升方法---提升树