leetCode(Using C)——461. Hamming Distance
Description
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.
Link:
If you want to solve the problem, you can visite the web site.click me
Solution:
#define MAXSIZE 32
int hammingDistance(int x, int y) {
int stack1[MAXSIZE],top1=-1;
int stack2[MAXSIZE],top2=-1;
int dis;
while(x>0){
stack1[++top1] = x%2;
x /= 2;
}
while(y>0){
stack2[++top2] = y%2;
y /= 2;
}
dis=0;
while(top1>top2){
if(stack1[top1--]==1){
dis++;
}
}
while(top2>top1){
if(stack2[top2--]==1){
dis++;
}
}
while(top1>-1&&top2>-1){
if(stack1[top1--]!=stack2[top2--]){
dis++;
}
}
return dis;
}
相關文章
- LeetCode 461. Hamming DistanceLeetCode
- 【LeetCode】461.Hamming Distance_EASY(一)LeetCode
- 漢明距離(Hamming distance)
- HDU 4712Hamming Distance(隨機函式運用)隨機函式
- LeetCode 461. 漢明距離LeetCode
- [Leetcode] Edit DistanceLeetCode
- Leetcode Edit DistanceLeetCode
- Leetcode-Edit DistanceLeetCode
- Edit Distance leetcode javaLeetCodeJava
- Leetcode-One Edit DistanceLeetCode
- LeetCode-Shortest Word DistanceLeetCode
- leetCode(Using C)——657. Judge Route CircleLeetCode
- 461.漢明距離(c++實現)C++
- [LeetCode] 243. Shortest Word DistanceLeetCode
- LeetCode-Shortest Word Distance IILeetCode
- LeetCode-Shortest Word Distance IIILeetCode
- [LeetCode] 2739. Total Distance TraveledLeetCode
- LeetCode之Shortest Distance to a Character(Kotlin)LeetCodeKotlin
- [LeetCode] 244. Shortest Word Distance IILeetCode
- leetCode(Using C)——760. Find Anagram MappingsLeetCodeAPP
- leetCode(Using C)——718. Maximum Length of Repeated SubarrayLeetCode
- LeetCode-Rearrange String k Distance ApartLeetCode
- LeetCode-Shortest Distance from All BuildingsLeetCodeUI
- Java解決LeetCode72題 Edit DistanceJavaLeetCode
- [LeetCode] 1385. Find the Distance Value Between Two ArraysLeetCode
- simhash&hamming distince
- [LeetCode] 317. Shortest Distance from All BuildingsLeetCodeUI
- Leetcode Implement Queue using StacksLeetCode
- 【leetcode】72. Edit Distance 編輯距離計算LeetCode
- Using MongoDB in C#MongoDBC#
- LeetCode-Implement Stack Using QueuesLeetCode
- LeetCode-Implement Queue using StacksLeetCode
- Matrix Distance
- LeetCode | 232 Implement Queue Using StacksLeetCode
- LeetCode 1334. Find the City With the Smallest Number of Neighbors at a Threshold Distance??LeetCode
- C#中using的使用C#
- 海明碼(Hamming Code)的知識點
- Oracle 12c: Recover tables using RMANOracle