博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
461. Hamming Distance
阅读量:7067 次
发布时间:2019-06-28

本文共 699 字,大约阅读时间需要 2 分钟。

题目描述:

The  between two integers is the number of positions at which the corresponding bits are different.

Given two integers x and y, calculate the Hamming distance.

Note:

0 ≤ xy < 231.

Example:

Input: x = 1, y = 4Output: 2Explanation:1   (0 0 0 1)4   (0 1 0 0)       ↑   ↑The above arrows point to positions where the corresponding bits are different. 解题思路: 题目不难,直接上代码。 代码:
1 class Solution { 2 public: 3     int hammingDistance(int x, int y) { 4         int num = 0; 5         int tmp = x xor y; 6         while (tmp != 0) { 7             if (tmp % 2) 8                 num ++; 9             tmp /= 2;10         }11         return num;12     }13 };

 

转载于:https://www.cnblogs.com/gsz-/p/9383442.html

你可能感兴趣的文章
简单的转义字符
查看>>
RabbitMQ入门-Topic模式
查看>>
poj 2777 Count Color(线段树区间更新)
查看>>
Java数据结构与算法(5) - ch05链表(LinkList)
查看>>
CLR Via CSharp读书笔记(21):自动内存管理(垃圾回收)
查看>>
刚刚接触python的感想
查看>>
modelsim使用常见问题及解决办法集锦 ②
查看>>
常用的第三方库
查看>>
java 操作elasticsearch之搭建测试项目环境
查看>>
iOS-图文表并茂,手把手教你GCD
查看>>
python之logging模块
查看>>
让Android Studio支持系统签名
查看>>
quick 状态机StateMachine
查看>>
Node学习5-events模块
查看>>
3.5 Templates -- Binding Element Attributes(绑定元素属性)
查看>>
jquery常用技巧及常用方法列表集合
查看>>
Microsoft Dynamics AX 2012 正式版虚拟机
查看>>
iOS-CALayer图片淡入淡出动画
查看>>
Python学习笔记
查看>>
Oracle
查看>>