Skip to content

Commit 2520282

Browse files
committed
:squirrel:
1 parent 9510f5d commit 2520282

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+442
-1
lines changed

3sum.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# 15. 3Sum
2+
# https://leetcode.com/problems/3sum/
3+
4+
class Solution:
5+
def threeSum(self, nums: List[int]) -> List[List[int]]:
6+
nums.sort()
7+
n = len(nums)
8+
ans = []
9+
for i in range(n-2):
10+
if i > 0 and nums[i] == nums[i-1]:
11+
continue
12+
j = i+1
13+
k = n-1
14+
while j < k:
15+
s = nums[i] + nums[j] + nums[k]
16+
if s == 0:
17+
ans.append([nums[i], nums[j], nums[k]])
18+
while j < k and nums[j] == nums[j+1]:
19+
j += 1
20+
while j < k and nums[k] == nums[k-1]:
21+
k -= 1
22+
j += 1
23+
k -= 1
24+
elif s > 0:
25+
k -= 1
26+
else:
27+
j += 1
28+
return ans

accounts-merge.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# 721. Accounts Merge
2+
# https://leetcode.com/problems/accounts-merge/
3+
4+
from collections import defaultdict
5+
class Solution:
6+
def accountsMerge(self, accounts: List[List[str]]) -> List[List[str]]:
7+
def dfs(stack, emails, explored, graph):
8+
while stack:
9+
u = stack.pop()
10+
emails.append(u)
11+
for v in graph[u]:
12+
if v not in explored:
13+
explored.add(v)
14+
stack.append(v)
15+
16+
graph = defaultdict(set)
17+
email_to_name = dict()
18+
for account in accounts:
19+
name = account[0]
20+
for i in range(1, len(account)):
21+
email_to_name[account[i]] = name
22+
if i == 1:
23+
continue
24+
graph[account[i]].add(account[i-1])
25+
graph[account[i-1]].add(account[i])
26+
27+
explored = set()
28+
ans = []
29+
for email in email_to_name.keys():
30+
if email not in explored:
31+
explored.add(email)
32+
stack = [email]
33+
emails = []
34+
dfs(stack, emails, explored, graph)
35+
emails.sort()
36+
ans.append([email_to_name[email]] + emails)
37+
return ans

add-binary.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
# 67. Add Binary
12
# https://leetcode.com/problems/add-binary/
23

34
class Solution:

add-two-numbers.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
# 2. Add Two Numbers
12
# https://leetcode.com/problems/add-two-numbers/
23

34
# Definition for singly-linked list.

alien-dictionary.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
# 269. Alien Dictionary
2+
# https://leetcode.com/problems/alien-dictionary/
3+
14
class Solution:
25
def alienOrder(self, words: List[str]) -> str:
36
from collections import defaultdict

backspace-string-compare.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
# 844. Backspace String Compare
12
# https://leetcode.com/problems/backspace-string-compare/
23

34
class Solution:

binary-search-tree-iterator.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
# 173. Binary Search Tree Iterators
12
# https://leetcode.com/problems/binary-search-tree-iterator/
23

34
# Definition for a binary tree node.

combination-sum-ii.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
# 40. Combination Sum II
12
# https://leetcode.com/problems/combination-sum-ii/
23

34
class Solution:

combination-sum.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# 39. Combination Sum
2+
# https://leetcode.com/problems/combination-sum/
3+
4+
class Solution:
5+
def combinationSum(self, candidates: List[int], target: int) -> List[List[int]]:
6+
def backtrack(l, k, s, idx, n, aux, ans):
7+
if s == k:
8+
ans.append(aux[:])
9+
elif s > k:
10+
return
11+
for i in range(idx, n):
12+
aux.append(l[i])
13+
backtrack(l, k, s+l[i], i, n, aux, ans)
14+
aux.pop()
15+
16+
ans = []
17+
backtrack(candidates, target, 0, 0, len(candidates), [], ans)
18+
return ans

confusing-number.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
# 1056. Confusing Number
12
# https://leetcode.com/problems/confusing-number/
23

34
class Solution:

0 commit comments

Comments
 (0)