Home Hamming Distance
Post
Cancel

Hamming Distance

Leetcode Problem

Hamming Distance

주어진 두 정수의 이진수에서 서로 다른 bit의 개수를 구하는 문제입니다.

1
2
3
4
5
6
7
class Solution:
    def hammingDistance(self, x: int, y: int) -> int:
        cnt = 0
        for i in range(31):
            if format(x, '031b')[i] != format(y, '031b')[i]:
                cnt += 1
        return cnt

정수의 범위는 2의 31승까지이기 때문에 format 함수를 통해 x와 y를 31자리까지 0으로 채우는 수로 나타낸 후에 이진수를 차례로 순회하면서 서로 다른 bit의 수를 구합니다.

1
2
3
4
5
6
7
8
9
10
class Solution:
    def hammingDistance(self, x: int, y: int) -> int:
        z = x ^ y
        i = z
        count = 0
        while i > 0:
            count += i % 2
            i = i // 2
        
        return count

solution의 풀이입니다. ^, xor 연산을 활용하면 풀이가 편하다는 것을 배웠습니다.

1
2
3
class Solution:
    def hammingDistance(self, x: int, y: int) -> int:
        return format(x ^ y, 'b').count('1')

제 풀이와 solution의 풀이를 응용해서 한 줄 풀이로 만들 수 있었습니다.





참고

This post is licensed under CC BY 4.0 by the author.