[LeetCode] 461. Hamming Distance
生活随笔
收集整理的這篇文章主要介紹了
[LeetCode] 461. Hamming Distance
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
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.
題意,兩個數字轉為二進制,然后異或一下,判斷有多少個1
直接異或判斷,簡單粗暴
class Solution {private int ham(int num) {int sum = 0;while (num != 0) {if (num%2 == 1)sum ++;num = num/2;}return sum;}public int hammingDistance(int x, int y) {int res = x^y;return ham(res);} }?
轉載于:https://www.cnblogs.com/Moriarty-cx/p/9769685.html
總結
以上是生活随笔為你收集整理的[LeetCode] 461. Hamming Distance的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【转】6 Reasons Why Jav
- 下一篇: 彻底弄懂 HTTP 缓存机制及原理 |