본문 바로가기
LeetCode/Top Interview Q. - Easy

LeetCode.Hamming Distance [Bit]

by 벤진[Benzene] 2020. 6. 18.

Problem:

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 (0 0 0 1) 4 (0 1 0 0) ↑ ↑ The above arrows point to positions where the corresponding bits are different.

 

-Summary-

Used bin() method and XOR operation

1. Calculate the XOR operation between two numbers (XOR operation will calculate '1' only if two bits are different at the given position)

2. Count 1 of binary number from step 1.

모든 문제에 대한 저작권은 LeetCode 회사에 있습니다. [Copyright © 2020 LeetCode]

'LeetCode > Top Interview Q. - Easy' 카테고리의 다른 글

LeetCode. Pascal's Triangle  (0) 2020.06.21
LeetCode. Missing Number [Bit]  (0) 2020.06.19
LeetCode.Number of 1 Bits [Bit]  (0) 2020.06.17
LeetCode.Roman to Integer [Math]  (0) 2020.06.16
LeetCode. Count Primes [Math]  (0) 2020.06.15

댓글