Hamming Distance
https://leetcode.com/problems/hamming-distance/description/
TheHamming distancebetween two integers is the number of positions at which the corresponding bits are different.
Given two integers
x
andy
, calculate the Hamming distance.
Thoughts
找不同bit想到XOR. 然后再数XOR后有多少个1. 通过把32位不断右移后与1相与.
Code
Analysis
时间复杂度O(1).
Last updated
Was this helpful?