最近群聊里传了一个面试题 实现统计1的个数(汉明权重 hammingWeight),使用popcnt的算法对硬件不友好,有无绕过的思路 显然这个哥们的第一个实现是 int hammingWeight_popcnt ns 334434 BM_hammingWeight/0 80.7 ns 80.7 ns 8689750 BM_hammingWeight 3646 ns 195882 BM_hammingWeight/512 8099 ns 8097 ns 85508 BM_hammingWeight ns 517551 BM_hammingWeight/0 124 ns 124 ns 5638895 BM_hammingWeight 5511 ns 123293 BM_hammingWeight/512 12036 ns 12036 ns 58336 BM_hammingWeight
://gitee.com/nateshao/leetcode/blob/main/algo-notes/src/main/java/com/nateshao/sword_offer/topic_12_hammingWeight package com.nateshao.sword_offer.topic_12_hammingWeight; /** * @date Created by 邵桐杰 on 2021/11/20 12 = hammingWeight(11); System.out.println("hammingWeight = " + hammingWeight); } public static int hammingWeight(int n) { int res = 0; while (n ! 代码: public class Solution { public static int hammingWeight2(int n) { int res = 0;
代码如下: package main import "fmt" func main() { n := 5 if true { ret := hammingWeight1 (n) fmt.Println(ret) } if true { ret := hammingWeight2(uint32(n)) fmt.Println func hammingWeight1(n int) int { bits := 0 rightOne := 0 for n ! 0 { bits++ rightOne = n & (-n) n ^= rightOne } return bits } func hammingWeight2
Java Code: public int hammingWeight(int n) { int bits = 0; int mask = 1; for (int i = 0; 这里我们可以通过无符号右移运算符来优化一下循环次数: Java Code: public int hammingWeight(int n) { int count = 0; while Java Code: public int hammingWeight(int n) { int count = 0; while (n ! = 0) { n &= (n - 1); count++; } return count; } JavaScript Code: function hammingWeight 三、其他解法 3.1 平衡算法 Java Code: public int hammingWeight(int n) { n = (n & 0x55555555) + ((n >>> 1) &
代码如下: package main import "fmt" func main() { n := 5 if true { ret := hammingWeight1 (n) fmt.Println(ret) } if true { ret := hammingWeight2(uint32(n)) fmt.Println func hammingWeight1(n int) int { bits := 0 rightOne := 0 for n ! 0 { bits++ rightOne = n & (-n) n ^= rightOne } return bits } func hammingWeight2
(0) → 结果 0 → arr = [0] i = 1 → 调用 hammingWeight(1) → 结果 1 → arr = [0, 1] i = 2 → 调用 hammingWeight(2) → 结果 1 → arr = [0, 1, 1] i = 3 → 调用 hammingWeight(3) → 结果 2 → arr = [0, 1, 1, 2] i = 4 → 调用 hammingWeight 调用 hammingWeight 时间复杂度为 O(32),即 O(1)。 调用 hammingWeight(5): 5 的二进制表示为:0101。 可优化方向: 优化 hammingWeight 函数,利用 n & (n - 1) 方法快速清除最低位的 1,减少迭代次数 优化后的辅助函数 int hammingWeight(int n) {
import java.util.Arrays; class Solution { static public int hammingWeight(int n) { int return num; } public static void main(String[] args) { System.out.println(hammingWeight import java.util.Arrays; class Solution { static public int hammingWeight(int n) { int return num; } public static void main(String[] args) { System.out.println(hammingWeight
class Solution { public: int hammingWeight(uint32_t n) { int res = 0; while(n) in understanding the better functions. // It uses 24 arithmetic operations (shift, add, and). int hammingWeight known implementation on machines with slow multiplication. // It uses 17 arithmetic operations. int hammingWeight machines with fast multiplication. // It uses 12 arithmetic operations, one of which is a multiply. int hammingWeight
注意:本题与主站(力扣) 191 题相同:力扣 代码 python代码1——bin()函数 class Solution: def hammingWeight(self, n: int) -> python代码2——使用n&(n−1) class Solution: def hammingWeight(self, n: int) -> int: res = 0 class Solution: def hammingWeight(self, n: int) -> int: res = 0 while n: == 0 java代码: public class Solution { // you need to treat n as an unsigned value public int hammingWeight
Solution Version 1 class Solution { public: int hammingWeight(uint32_t n) { int count = 0 n >>= 1; } return count; } }; Version 2 class Solution { public: int hammingWeight
Python实现: class Solution(object): def hammingWeight(self, n): """ :type n: int """ return sum([int(x) for x in bin(n)[2:] if x == '1']) a = 11 b = Solution() print(b.hammingWeight
解题思路 class Solution: def hammingWeight(self, n: int) -> int: strI = bin(n)#转成二进制 1") if __name__ == '__main__': nums = 11111111111111111111111111111101 result = Solution().hammingWeight
Solution191 solution191 = new Solution191(); int n = 11; System.out.println(solution191.hammingWeight * 用Integer.bitCount函数统计参数n转成2进制后有多少个1 * * @param n * @return */ public int hammingWeight * * @param n * @return */ public int hammingWeight_2(int n) { int count = /** * 用flag来与n的每位做位于运算,来判断1的个数 * * @param n * @return */ public int hammingWeight
numbers with remaining length and available digits func numberRest(offset, status int) int { c := hammingWeight = 10 } return ans } // Returns the number of set bits (1s) in a binary representation func hammingWeight int len); int process(int offset, int status, int n); int numberRest(int offset, int status); int hammingWeight int len); int process(int offset, int status, int n); int numberRest(int offset, int status); int hammingWeight ^ (1 << first), n); } return ans; } int numberRest(int offset, int status) { int c = hammingWeight
上述解法的 java 实现如下: public class Solution { public int hammingWeight(int n) { int ans = 0; 该方法的 java 实现如下: public class Solution { public int hammingWeight(int n) { int ans = 0; 基于上述方法的 java 实现如下: public class Solution { public int hammingWeight(int n) { int res = 0;
and deletion) public class Solution { // you need to treat n as an unsigned value public int hammingWeight deletion) 提交 public class Solution { // you need to treat n as an unsigned value public int hammingWeight 0x3f; } 解法2 public class Solution { // you need to treat n as an unsigned value public int hammingWeight
: 1 Explanation: Integer 128 has binary representation 00000000000000000000000010000000 要完成的函数: int hammingWeight 代码如下:(附详解) int hammingWeight(uint32_t n) { int res=0; for(int i=0;i<32;i++)
-1-bits 很神奇的位运算,当然不是我想的,我看的答案~ n = n&(n-1),每次都会清空末尾的一个1,全清空之后就为 0; class Solution { public: int hammingWeight n-1; count++; } return count; } }; 前面的人比较狠 class Solution { public: int hammingWeight
num & 1 后判断为 1, 则计数增加 1, 然后将右移一位 num >>= 1, 以此类推, 直到移位 32 次. public class Solution { public int hammingWeight --- 0000 由于每次都把最低位的 1 变为 0 了, 直到为 0, 那么这种个编程变了几次就说明有多少个 1. public class Solution { public int hammingWeight
er-jin-zhi-zhong-1de-ge-shu-lcof /** * @param {number} n - a positive integer * @return {number} */ // var hammingWeight // 时间复杂度 O(k),k=32, 空间复杂度 O(1) // var hammingWeight = function(n) { // let num=0 // let arr = ==0){ // num++; // } // } // return num; // }; //位运算优化 var hammingWeight