LeetCode题解之Hamming Distance

1、题目描述

2、问题分析

使用C++ 标准库中的 bitset 类,将整数转换为二进制形式,然后再将其转换为字符串,最后比较字符串。

3、代码

 1 int hammingDistance(int x, int y) {
 2
 3         bitset<32> a(x);
 4         bitset<32> b(y);
 5
 6         string a_s = a.to_string();
 7         string b_s = b.to_string();
 8
 9         string::iterator it1 = a_s.begin() ;
10         string::iterator it2 = b_s.begin() ;
11         int re = 0;
12         while( it1 != a_s.end() && it2 != b_s.end() ){
13             if( *it1 != *it2 ){
14                 re++;
15             }
16             it1++;
17             it2++;
18         }
19         return re;
20     }

原文地址:https://www.cnblogs.com/wangxiaoyong/p/9295612.html

时间: 2024-10-19 11:06:12

LeetCode题解之Hamming Distance的相关文章

[LeetCode] 477. Total Hamming Distance(位操作)

传送门 Description The Hamming distance between two integers is the number of positions at which the corresponding bits are different. Now your job is to find the total Hamming distance between all pairs of the given numbers. Example: Input: 4, 14, 2 Ou

LeetCode之461. Hamming Distance

------------------------------------------------------------------ AC代码: public class Solution { public int hammingDistance(int x, int y) { return Integer.toString(x^y,2).replaceAll("0","").length(); } } 题目来源: https://leetcode.com/prob

LeetCode 477: Total Hamming Distance

Note: 1. Very smart way of calculating how many difference from bits: https://en.wikipedia.org/wiki/Hamming_distance#Algorithm_example 2. Another way is counting how many 1s and 0s per bits. Then the contribution of that bit will bt C(1, k) * C(1, n

[LeetCode] 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. Example: Input: x = 1, y = 4 Output: 2 Explanation: 1

461. Hamming Distance(leetcode)

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 < 2^31. Example: Input: x = 1, y = 4 Output: 2 Explanation:

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. Example: Input: x = 1, y = 4 Output: 2 Explanation: 1

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. Example: Input: x = 1, y = 4 Output: 2 Explanation: 1

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. Example: Input: x = 1, y = 4 Output: 2 Explanation: 1

Leetcode 461. Hamming Distance JAVA语言

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. PS:求海明距离. 思路:就是求x和y二进制的异或中的1的个数 public class Solution {     public int ham