LeetCode——461. Hamming Distance(C++,模拟)
来源:互联网 发布:徐老师淘宝店叫什么 编辑:程序博客网 时间:2024/06/05 17:55
题目链接
The Hamming distance 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 ≤ x
, y
< 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.
题解:
逐位比较。
class Solution {public: int hammingDistance(int x, int y) { int res=0; int maxv=x>y?x:y; int minv=x-maxv+y; while(maxv) { if(maxv%2!=minv%2) res++; maxv/=2; minv/=2; } return res; }};
阅读全文
0 0
- LeetCode——461. Hamming Distance(C++,模拟)
- LeetCode——Hamming Distance
- LeetCode——Hamming Distance
- LeetCode——Hamming Distance
- leetcode 461. Hamming Distance(C语言)
- LeetCode 461. Hamming Distance
- 【Leetcode】461. Hamming Distance
- LeetCode 461. Hamming Distance
- LeetCode - 461. Hamming Distance
- LeetCode-461. Hamming Distance
- 【LeetCode】 461. Hamming Distance
- Leetcode-461. Hamming Distance
- Leetcode 461. Hamming Distance
- LeetCode--461. Hamming Distance
- 461. Hamming Distance [LeetCode]
- [LeetCode]461. Hamming Distance
- leetcode 461. Hamming Distance
- Leetcode 461. Hamming Distance
- 广田集团——建安装饰行业BI标杆客户
- Linux命令date日期时间和Unix时间戳互转
- JS中({})和(){}的区别
- POI之You can define up to 4000 styles in a .xls workbook-yellowcong
- jQuery(全选/反选)与每个选项的联动
- LeetCode——461. Hamming Distance(C++,模拟)
- centos6实现LNMP
- 2017年8月1日 爬虫重新开始学习
- 创建Maven项目失败的解决方法
- bzoj1835基站选址(dp+线段树)
- JS中声明一个数组
- GitLab安装说明
- iOS应用之间的跳转,进入指定页面
- ANDROID GRADLE命令详解与导入第三方包