Skip to content

Commit 9ee7b25

Browse files
authored
Merge pull request #95 from saysimple0828/saysimple-week4
Saysimple week4
2 parents 0fe554a + bddde12 commit 9ee7b25

File tree

5 files changed

+42
-0
lines changed

5 files changed

+42
-0
lines changed

counting-bits/saysimple.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
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

group-anagrams/saysimple.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
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]

missing-number/saysimple.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
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

number-of-1-bits/saysimple.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# TC: O(1), SC: O(1)
2+
3+
class Solution:
4+
def hammingWeight(self, n: int) -> int:
5+
return bin(n).count("1")

reverse-bits/saysimple.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
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)

0 commit comments

Comments
 (0)