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

本文共 834 字,大约阅读时间需要 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.

链接:

3/27/2017

1 public class Solution { 2     public int hammingDistance(int x, int y) { 3         int z = x ^ y; 4         int c = 1, count = 0; 5         while (z != 0) { 6             if ((z & c) != 0) count++; 7             z >>= 1; 8         } 9         return count;10     }11 }

其他人作法:

1. 使用自带函数

1 public class Solution {2     public int hammingDistance(int x, int y) {3         return Integer.bitCount(x ^ y);4     }5 }

更多讨论:

转载于:https://www.cnblogs.com/panini/p/6629756.html

你可能感兴趣的文章
Cygwin下vim按方向键出现ABCD;
查看>>
android用shape画虚线,怎么也不显示
查看>>
javascript小白学习指南0---1
查看>>
【求助】怎样实如今并肩看中增加代码啊
查看>>
创业路(VC Pipeline),创业需要融资的阅读
查看>>
Effective C++:条款37:绝不又一次定义继承而来的缺省參数值
查看>>
linux find命令强大之处
查看>>
开宝箱怎么设计才算好?大脑说了算!
查看>>
oracle 中模糊查询对like的代替insrt()函数 可以做到效率节约一倍以上
查看>>
linux添加私有的ip
查看>>
mysql
查看>>
python学习中遇到的错误及解决办法
查看>>
爱的十个秘密--5.友谊的力量
查看>>
什么是程序集?
查看>>
电子书下载:Microsoft Silverlight 4 and SharePoint 2010 Integration
查看>>
C#反射
查看>>
Unity 深度冲突的解决方法
查看>>
IOS 7 UI 的适配
查看>>
变量的引用类型和非引用类型的区别
查看>>
drawable以及Bitmap的基本操作
查看>>