File tree Expand file tree Collapse file tree 3 files changed +39
-0
lines changed Expand file tree Collapse file tree 3 files changed +39
-0
lines changed Original file line number Diff line number Diff line change 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/ )
Original file line number Diff line number Diff line change 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
Original file line number Diff line number Diff line change 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
You can’t perform that action at this time.
0 commit comments