Skip to content

Commit f5d04e7

Browse files
committedOct 23, 2024
[Feat] Add some implementation
1 parent 689c328 commit f5d04e7

File tree

4 files changed

+51
-0
lines changed

4 files changed

+51
-0
lines changed
 

‎1-TwoSum.py

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class Solution:
2+
def twoSum(self, nums: List[int], target: int) -> List[int]:
3+
hashMap = {}
4+
for i, num in enumerate(nums):
5+
temp = target - num
6+
if temp in hashMap:
7+
return hashMap[temp], i
8+
hashMap[num] = i
9+
return

‎217-ContainsDuplicate.py

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
class Solution:
2+
def containsDuplicate(self, nums: List[int]) -> bool:
3+
dataSet = set()
4+
for num in nums:
5+
if num in dataSet:
6+
return True
7+
dataSet.add(num)
8+
return False

‎242-ValidAnagram.py

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution:
2+
def isAnagram(self, s: str, t: str) -> bool:
3+
if len(s) != len(t):
4+
return False
5+
6+
hashMapS = {}
7+
for num in s:
8+
if num not in hashMapS:
9+
hashMapS[num] = 1
10+
else:
11+
hashMapS[num] += 1
12+
13+
hashMapT = {}
14+
for num in t:
15+
if num not in hashMapT:
16+
hashMapT[num] = 1
17+
else:
18+
hashMapT[num] += 1
19+
20+
return hashMapS == hashMapT

‎49-GroupAnagrams.py

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution:
2+
def groupAnagrams(self, strs: List[str]) -> List[List[str]]:
3+
hashMap = {}
4+
result = []
5+
for str in strs:
6+
sortedString = "".join(sorted(str))
7+
if sortedString not in hashMap:
8+
hashMap[sortedString] = [str]
9+
else:
10+
hashMap[sortedString].append(str)
11+
12+
for val in hashMap.values():
13+
result.append(val)
14+
return result

0 commit comments

Comments
 (0)