Skip to content

Commit

Permalink
Merge pull request #95 from saysimple0828/saysimple-week4
Browse files Browse the repository at this point in the history
Saysimple week4
  • Loading branch information
DaleSeo authored May 29, 2024
2 parents 0fe554a + bddde12 commit 9ee7b25
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 0 deletions.
11 changes: 11 additions & 0 deletions counting-bits/saysimple.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# TC: O(n), SC: O(n)

class Solution:
def countBits(self, n: int) -> List[int]:
answer = []

for i in range(n+1):
b = bin(i)
answer.append(int(b.count("1")))

return answer
14 changes: 14 additions & 0 deletions group-anagrams/saysimple.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# TC: O(n), SC: O(n)

from collections import defaultdict

class Solution:
def groupAnagrams(self, strs: List[str]) -> List[List[str]]:
d = defaultdict(list)
for s in strs:
if not s:
d["0"].append("")
else:
d["".join(sorted(s))].append(s)

return [d[s] for s in d]
7 changes: 7 additions & 0 deletions missing-number/saysimple.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# TC: O(n), SC: O(1)

class Solution:
def missingNumber(self, nums: List[int]) -> int:
for i in range(len(nums)+1):
if i not in nums:
return i
5 changes: 5 additions & 0 deletions number-of-1-bits/saysimple.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# TC: O(1), SC: O(1)

class Solution:
def hammingWeight(self, n: int) -> int:
return bin(n).count("1")
5 changes: 5 additions & 0 deletions reverse-bits/saysimple.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# TC: O(1), SC: O(1)

class Solution:
def reverseBits(self, n: int) -> int:
return int(f"{bin(n)[2:]:0>32}"[::-1], 2)

0 comments on commit 9ee7b25

Please sign in to comment.