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

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程语言 > java >内容正文

java

leetcode 461. 汉明距离(Java版)

發(fā)布時(shí)間:2024/2/28 java 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 leetcode 461. 汉明距离(Java版) 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

題目

https://leetcode-cn.com/problems/hamming-distance/

題解

使用 Java 中的按位異或 ^ 運(yùn)算符:https://www.baeldung.com/java-xor-operator

For example, let’s consider two integers 1 and 3, whose binary representations are 00000001 and 000000011, respectively. Then, using the XOR operator between them will result in the integer 2.

Only the second bit is different in those two numbers, therefore the result of the XOR operator on this bit will be 1. All other bits are identical, thus their bitwise XOR result is 0, giving us a final value of 00000010 — the binary representation of the integer 2.

代碼

一行搞定:return Integer.bitCount(x ^ y);

public class Solution {public static int hammingDistance(int x, int y) {System.out.println(Integer.toBinaryString(x)); // 001System.out.println(Integer.toBinaryString(y)); // 100System.out.println(Integer.toBinaryString(x ^ y)); // 101return Integer.bitCount(x ^ y);}// for testpublic static void main(String[] args) {System.out.println(hammingDistance(1, 4));} }

總結(jié)

以上是生活随笔為你收集整理的leetcode 461. 汉明距离(Java版)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。