Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upload my tasks #46

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions Jacklin/contains_duplicate.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class Solution:
def containsDuplicate(self, nums: List[int]) -> bool:
seen = set()
for num in nums:
if num in seen:
return True
else:
seen.add(num)
return False
9 changes: 9 additions & 0 deletions Jacklin/groupanagram.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class Solution:
def groupAnagrams(self, strs: List[str]) -> List[List[str]]:
anagramGroups = defaultdict(list)

for word in strs:
sortedWord = ''.join(sorted(word))
anagramGroups[sortedWord].append(word)

return list(anagramGroups.values())
9 changes: 9 additions & 0 deletions Jacklin/plusone.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class Solution:
def plusOne(self, digits: List[int]) -> List[int]:
n = len(digits)
for i in range(n - 1, -1, -1):
if digits[i] < 9:
digits[i] += 1
return digits
digits[i] = 0
return [1] + digits
14 changes: 14 additions & 0 deletions Jacklin/product_exceptself.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class Solution:
def productExceptSelf(self, nums: List[int]) -> List[int]:
n = len(nums)
answer = [1] * n
prefix = 1
for i in range(n):
answer[i] = prefix
prefix *= nums[i]
suffix = 1
for i in range(n - 1, -1, -1):
answer[i] *= suffix
suffix *= nums[i]

return answer
15 changes: 15 additions & 0 deletions Jacklin/topk.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class Solution:
def topKFrequent(self, nums: List[int], k: int) -> List[int]:

frequency = Counter(nums)
buckets = [[] for _ in range(len(nums) + 1)]

for num, freq in frequency.items():
buckets[freq].append(num)

result = []
for i in range(len(buckets) - 1, 0, -1):
if buckets[i]:
result.extend(buckets[i])
if len(result) >= k:
return result[:k]
8 changes: 8 additions & 0 deletions Jacklin/twosum.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class Solution:
def twoSum(self, nums: List[int], target: int) -> List[int]:
h = {}
for i, n in enumerate(nums):
c = target - n
if c in h:
return [h[c], i]
h[n] = i
5 changes: 5 additions & 0 deletions Jacklin/validanagram.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class Solution:
def isAnagram(self, s: str, t: str) -> bool:
if len(s) != len(t):
return False
return Counter(s) == Counter(t)