File tree 2 files changed +27
-0
lines changed
2 files changed +27
-0
lines changed Original file line number Diff line number Diff line change 382
382
474|[ Ones and Zeroes] ( ./0474-ones-and-zeroes.js ) |Medium|
383
383
475|[ Heaters] ( ./0475-heaters.js ) |Medium|
384
384
476|[ Number Complement] ( ./0476-number-complement.js ) |Easy|
385
+ 477|[ Total Hamming Distance] ( ./0477-total-hamming-distance.js ) |Medium|
385
386
482|[ License Key Formatting] ( ./0482-license-key-formatting.js ) |Easy|
386
387
485|[ Max Consecutive Ones] ( ./0485-max-consecutive-ones.js ) |Easy|
387
388
486|[ Predict the Winner] ( ./0486-predict-the-winner.js ) |Medium|
Original file line number Diff line number Diff line change
1
+ /**
2
+ * 477. Total Hamming Distance
3
+ * https://leetcode.com/problems/total-hamming-distance/
4
+ * Difficulty: Medium
5
+ *
6
+ * The Hamming distance between two integers is the number of positions at which the
7
+ * corresponding bits are different.
8
+ *
9
+ * Given an integer array nums, return the sum of Hamming distances between all the
10
+ * pairs of the integers in nums.
11
+ */
12
+
13
+ /**
14
+ * @param {number[] } nums
15
+ * @return {number }
16
+ */
17
+ var totalHammingDistance = function ( nums ) {
18
+ let result = 0 ;
19
+
20
+ for ( let bit = 0 ; bit < 32 ; bit ++ ) {
21
+ const ones = nums . reduce ( ( count , n ) => count + ( ( n >> bit ) & 1 ) , 0 ) ;
22
+ result += ones * ( nums . length - ones ) ;
23
+ }
24
+
25
+ return result ;
26
+ } ;
You can’t perform that action at this time.
0 commit comments