-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #95 from saysimple0828/saysimple-week4
Saysimple week4
- Loading branch information
Showing
5 changed files
with
42 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |