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 2c9ace2

Browse files
D. J.:
- Added the leetcode problem and solution for 477
1 parent 4a132e3 commit 2c9ace2

File tree

3 files changed

+39
-0
lines changed

3 files changed

+39
-0
lines changed

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,7 @@
246246
- [452 Minimum Number of Arrows to Burst Balloons](https://leetcode.com/problems/minimum-number-of-arrows-to-burst-balloons/description/)
247247
- [461 Hamming Distance](https://leetcode.com/problems/hamming-distance/description/)
248248
- [474 Ones and Zeroes](https://leetcode.com/problems/ones-and-zeroes/description/)
249+
- [477 Total Hamming Distance](https://leetcode.com/problems/total-hamming-distance/description/)
249250
- [502 IPO](https://leetcode.com/problems/ipo/description/)
250251
- [509 Fibonacci Number](https://leetcode.com/problems/fibonacci-number/description/)
251252
- [516 Longest Palindromic Subsequence](https://leetcode.com/problems/longest-palindromic-subsequence/description/)
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
from typing import List
2+
3+
4+
class Solution:
5+
"""Base class for all LeetCode Problems."""
6+
7+
def totalHammingDistance(self, nums: List[int]) -> int:
8+
"""
9+
The Hamming distance between two integers is the number of positions at which
10+
the corresponding bits are different.
11+
12+
Given an integer array nums, return the sum of Hamming distances between all the
13+
pairs of the integers in nums.
14+
"""
15+
result = 0
16+
for bit in range(32):
17+
ones = sum((num >> bit) & 1 for num in nums)
18+
zeros = len(nums) - ones
19+
result += ones * zeros
20+
return result
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
from typing import List
2+
3+
import pytest
4+
5+
from awesome_python_leetcode._477_total_hamming_distance import Solution
6+
7+
8+
@pytest.mark.parametrize(
9+
argnames=["nums", "expected"],
10+
argvalues=[
11+
([4, 14, 2], 6),
12+
([4, 14, 4], 4),
13+
],
14+
)
15+
def test_func(nums: List[int], expected: int):
16+
"""Tests the solution of a LeetCode problem."""
17+
total_hamming_distance = Solution().totalHammingDistance(nums)
18+
assert total_hamming_distance == expected

0 commit comments

Comments
(0)

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