日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

LeetCode之Hamming Distance

發布時間:2023/12/4 编程问答 50 豆豆
生活随笔 收集整理的這篇文章主要介紹了 LeetCode之Hamming Distance 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1、題目

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.

題意:

給定兩個整數x,y,計算x和y的漢明距離。漢明距離是指x、y的二進制表示中,相同位置上數字不相同的所有情況數。


2、代碼實現

public class Solution {public int hammingDistance(int x, int y) {int notSame = x ^ y;int count = 0;while (notSame != 0) {if (notSame % 2 == 1)++count;notSame /= 2;}return count;} } 注意記得用 異或
0 ^ 1 = 1 1 ^ 0 = 1 1 ^ 1 = 0 0 ^ 0 = 0 然后就是我們平時10進制的話,每次去掉末尾的數字是 / 10,二進制的話就是/2,切記。

總結

以上是生活随笔為你收集整理的LeetCode之Hamming Distance的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。