File tree Expand file tree Collapse file tree 5 files changed +42
-0
lines changed Expand file tree Collapse file tree 5 files changed +42
-0
lines changed Original file line number Diff line number Diff line change
1
+ # TC: O(n), SC: O(n)
2
+
3
+ class Solution :
4
+ def countBits (self , n : int ) -> List [int ]:
5
+ answer = []
6
+
7
+ for i in range (n + 1 ):
8
+ b = bin (i )
9
+ answer .append (int (b .count ("1" )))
10
+
11
+ return answer
Original file line number Diff line number Diff line change
1
+ # TC: O(n), SC: O(n)
2
+
3
+ from collections import defaultdict
4
+
5
+ class Solution :
6
+ def groupAnagrams (self , strs : List [str ]) -> List [List [str ]]:
7
+ d = defaultdict (list )
8
+ for s in strs :
9
+ if not s :
10
+ d ["0" ].append ("" )
11
+ else :
12
+ d ["" .join (sorted (s ))].append (s )
13
+
14
+ return [d [s ] for s in d ]
Original file line number Diff line number Diff line change
1
+ # TC: O(n), SC: O(1)
2
+
3
+ class Solution :
4
+ def missingNumber (self , nums : List [int ]) -> int :
5
+ for i in range (len (nums )+ 1 ):
6
+ if i not in nums :
7
+ return i
Original file line number Diff line number Diff line change
1
+ # TC: O(1), SC: O(1)
2
+
3
+ class Solution :
4
+ def hammingWeight (self , n : int ) -> int :
5
+ return bin (n ).count ("1" )
Original file line number Diff line number Diff line change
1
+ # TC: O(1), SC: O(1)
2
+
3
+ class Solution :
4
+ def reverseBits (self , n : int ) -> int :
5
+ return int (f"{ bin (n )[2 :]:0>32} " [::- 1 ], 2 )
You can’t perform that action at this time.
0 commit comments