Problem: 461. 汉明距离
文章目录
- 题目描述
- 思路
- 复杂度
- Code
题目描述
思路
Problem: 力扣191. 位1的个数(位运算)
该题只需要在上题的基础上先对两个数进行一次异或操作即可
复杂度
时间复杂度:
O ( 1 ) O(1) O(1)
空间复杂度:
O ( 1 ) O(1) O(1)
Code
class Solution {
public:/*** Bit operation* @param x Given number x* @param y Given number y* @return int*/int hammingDistance(int x, int y) {int temp = x ^ y;int mask = 1;int count = 0;for (int i = 0; i < 32; ++i) {if ((temp & mask) != 0) {count++;}mask <<= 1;}return count;}
};