Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 3abfbb7

Browse files
D. J.:
- Added the leetcode problem and solution for 461
1 parent ec03cc0 commit 3abfbb7

File tree

3 files changed

+36
-0
lines changed

3 files changed

+36
-0
lines changed

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,7 @@
235235
- [427 Construct Quad Tree](https://leetcode.com/problems/construct-quad-tree/description/)
236236
- [433 Minimum Genetic Mutation](https://leetcode.com/problems/minimum-genetic-mutation/description/)
237237
- [452 Minimum Number of Arrows to Burst Balloons](https://leetcode.com/problems/minimum-number-of-arrows-to-burst-balloons/description/)
238+
- [461 Hamming Distance](https://leetcode.com/problems/hamming-distance/description/)
238239
- [474 Ones and Zeroes](https://leetcode.com/problems/ones-and-zeroes/description/)
239240
- [502 IPO](https://leetcode.com/problems/ipo/description/)
240241
- [509 Fibonacci Number](https://leetcode.com/problems/fibonacci-number/description/)
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution:
2+
"""Base class for all LeetCode Problems."""
3+
4+
def hammingDistance(self, x: int, y: int) -> int:
5+
"""
6+
The Hamming distance between two integers is the number of positions at which
7+
the corresponding bits are different.
8+
9+
Given two integers x and y, return the Hamming distance between them.
10+
"""
11+
distance = 0
12+
while x > 0 or y > 0:
13+
# Get the last bit
14+
distance += (x & 1) ^ (y & 1)
15+
16+
# Shift to the right
17+
x >>= 1
18+
y >>= 1
19+
return distance

‎tests/test_461_hamming_distance.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import pytest
2+
3+
from awesome_python_leetcode._461_hamming_distance import Solution
4+
5+
6+
@pytest.mark.parametrize(
7+
argnames=["x", "y", "expected"],
8+
argvalues=[
9+
(1, 4, 2),
10+
(3, 1, 1),
11+
],
12+
)
13+
def test_func(x: int, y: int, expected: int):
14+
"""Tests the solution of a LeetCode problem."""
15+
hamming_distance = Solution().hammingDistance(x, y)
16+
assert hamming_distance == expected

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /