File tree Expand file tree Collapse file tree 2 files changed +16
-0
lines changed Expand file tree Collapse file tree 2 files changed +16
-0
lines changed Original file line number Diff line number Diff line change 45
45
| 0160 | [ getIntersectionNode] ( ./code/0160_getIntersectionNode ) | ⭐ | Linked List | 1️⃣✅ |
46
46
| 0162 | [ findPeakElement] ( ./code/0162_findPeakElement ) | ⭐⭐ | Array, Binary Search | |
47
47
| 0189 | [ rotate] ( ./code/0189_rotate ) | ⭐⭐ | Array | |
48
+ | 0191 | [ hammingWeight] ( ./code/0191_hammingWeight ) | ⭐ | Bit | 1️⃣ |
48
49
| 0204 | [ countPrimes] ( ./code/0204_countPrimes ) | ⭐ | Hash Table | |
49
50
| 0206 | [ reverseList] ( ./code/0206_reverseList ) | ⭐ | Linked List | |
50
51
| 0222 | [ countNodes] ( ./code/0222_countNodes ) | ⭐⭐ | Tree, Binary Search | |
Original file line number Diff line number Diff line change
1
+ /**
2
+ * @param {number } n - a positive integer
3
+ * @return {number }
4
+ */
5
+ var hammingWeight = function ( n ) {
6
+ let ans = 0 ;
7
+ while ( n ) {
8
+ n = n & ( n - 1 ) ;
9
+ ans ++ ;
10
+ }
11
+ return ans ;
12
+ } ;
13
+
14
+ // 时间复杂度 O(logN)
15
+ // 空间复杂度 O(1)
You can’t perform that action at this time.
0 commit comments