LeetCode 461. Hamming Distance

개발공부를해보자·2025년 2월 26일

LeetCode

목록 보기
71/95

파이썬 알고리즘 인터뷰 71번(리트코드 461번) Hamming Distance
https://leetcode.com/problems/hamming-distance/

나의 풀이

class Solution:
    def hammingDistance(self, x: int, y: int) -> int:
        bit = x ^ y
        result = 0
        while bit > 0:
            result += bit % 2
            bit = bit // 2
        return result

다른 풀이

class Solution:
    def hammingDistance(self, x: int, y: int) -> int:
        return bin(x ^ y).count('1')
  • bin 을 자주 써보지 않아서 생각나지 않았다.
  • 마찬가지로 문자열에서 .count 메서드가 있는 것을 머리로는 알고 있는데, 자주 써보지 않았다.
profile
개발 공부하는 30대 비전공자 직장인

0개의 댓글