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

Diff for: 3sum.py

+28
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

Diff for: accounts-merge.py

+37
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

Diff for: add-binary.py

+1
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:

Diff for: add-two-numbers.py

+1
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.

Diff for: alien-dictionary.py

+3
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

Diff for: backspace-string-compare.py

+1
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:

Diff for: binary-search-tree-iterator.py

+1
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.

Diff for: combination-sum-ii.py

+1
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:

Diff for: combination-sum.py

+18
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

Diff for: confusing-number.py

+1
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:

Diff for: design-phone-directory.py

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
# 379. Design Phone Directory
12
# https://leetcode.com/problems/design-phone-directory/
23

34
class PhoneDirectory:

Diff for: diameter-of-binary-tree.py

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
# 543. Diameter of Binary Tree
12
# https://leetcode.com/problems/diameter-of-binary-tree/
23

34
# Definition for a binary tree node.

Diff for: find-all-anagrams-in-a-string.py

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# 438. Find All Anagrams in a String
2+
# https://leetcode.com/problems/find-all-anagrams-in-a-string/
3+
4+
class Solution:
5+
def findAnagrams(self, s: str, p: str) -> List[int]:
6+
def equals(w1, w2, n):
7+
for i in range(26):
8+
if w1[i] != w2[i]:
9+
return False
10+
return True
11+
12+
if not s or len(s) < len(p):
13+
return []
14+
15+
ns, np = len(s), len(p)
16+
w1, w2 = [0]*26, [0]*26
17+
ans = []
18+
idx = lambda x: ord(x) - ord('a')
19+
20+
for i in range(np):
21+
w1[idx(s[i])] += 1
22+
w2[idx(p[i])] += 1
23+
24+
for i in range(ns-np+1):
25+
if equals(w1, w2, np):
26+
ans.append(i)
27+
w1[idx(s[i])] -= 1
28+
if i+np < ns:
29+
w1[idx(s[i+np])] += 1
30+
31+
return ans

Diff for: find-and-replace-in-string.py

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
# 833. Find And Replace in String
12
# https://leetcode.com/problems/find-and-replace-in-string/
23

34
class Solution:

Diff for: first-bad-version.py

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
# 278. First Bad Version
12
# https://leetcode.com/problems/first-bad-version/
23

34
# The isBadVersion API is already defined for you.

Diff for: flip-equivalent-binary-trees.py

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
# 951. Flip Equivalent Binary Trees
12
# https://leetcode.com/problems/flip-equivalent-binary-trees/
23

34
# Definition for a binary tree node.

Diff for: guess-number-higher-or-lower.py

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
# 374. Guess Number Higher or Lower
12
# https://leetcode.com/problems/guess-number-higher-or-lower/
23

34
# The guess API is already defined for you.

Diff for: integer-to-english-words.py

+91
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
# 273. Integer to English Words
2+
# https://leetcode.com/problems/integer-to-english-words/
3+
4+
class Solution:
5+
def numberToWords(self, num: int) -> str:
6+
def ones_n(n):
7+
f = {
8+
1 : 'One',
9+
2 : 'Two',
10+
3 : 'Three',
11+
4 : 'Four',
12+
5 : 'Five',
13+
6 : 'Six',
14+
7 : 'Seven',
15+
8 : 'Eight',
16+
9 : 'Nine'
17+
}
18+
return f[n]
19+
20+
def tens_less_twenty_n(n):
21+
f = {
22+
10 : 'Ten',
23+
11 : 'Eleven',
24+
12 : 'Twelve',
25+
13 : 'Thirteen',
26+
14 : 'Fourteen',
27+
15 : 'Fifteen',
28+
16 : 'Sixteen',
29+
17 : 'Seventeen',
30+
18 : 'Eighteen',
31+
19 : 'Nineteen'
32+
}
33+
return f[n]
34+
35+
def tens_n(n):
36+
f = {
37+
2 : 'Twenty',
38+
3 : 'Thirty',
39+
4 : 'Forty',
40+
5 : 'Fifty',
41+
6 : 'Sixty',
42+
7 : 'Seventy',
43+
8 : 'Eighty',
44+
9 : 'Ninety'
45+
}
46+
return f[n]
47+
48+
def tens(n):
49+
if n == 0:
50+
return ''
51+
if n < 10:
52+
return ones_n(n)
53+
elif n > 9 and n < 20:
54+
return tens_less_twenty_n(n)
55+
else:
56+
aux_tens = n // 10
57+
rest = n % 10
58+
return tens_n(aux_tens) + ('' if rest == 0 else ' ' + ones_n(rest))
59+
60+
def hundreds(n):
61+
if n == 0:
62+
return ''
63+
s = ''
64+
aux_hundreds = n // 100
65+
if aux_hundreds != 0:
66+
s += ones_n(aux_hundreds) + ' Hundred'
67+
tens_aux = n - (aux_hundreds * 100)
68+
if tens_aux != 0:
69+
s += ' ' if aux_hundreds != 0 else ''
70+
s += tens(tens_aux)
71+
return s
72+
73+
n = num
74+
billion = n // int(1e9)
75+
million = (n - (billion * int(1e9))) // int(1e6)
76+
thousand = (n - (billion * int(1e9)) - (million * int(1e6))) // int(1e3)
77+
rest = n - (billion * int(1e9)) - (million * int(1e6)) - (thousand * int(1e3))
78+
79+
s = ''
80+
if billion != 0:
81+
s += hundreds(billion) + ' Billion'
82+
if million != 0:
83+
s += ' ' if s else ''
84+
s += hundreds(million) + ' Million'
85+
if thousand != 0:
86+
s += ' ' if s else ''
87+
s += hundreds(thousand) + ' Thousand'
88+
if rest != 0:
89+
s += ' ' if s else ''
90+
s += hundreds(rest)
91+
return 'Zero' if n == 0 else s

Diff for: intersection-of-two-arrays-ii.py

+3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
# 350. Intersection of Two Arrays II
2+
# https://leetcode.com/problems/intersection-of-two-arrays-ii/
3+
14
class Solution:
25
def intersect(self, nums1: List[int], nums2: List[int]) -> List[int]:
36
nums1.sort()

Diff for: intersection-of-two-arrays.py

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# 349. Intersection of Two Arrays
2+
# https://leetcode.com/problems/intersection-of-two-arrays/
3+
4+
class Solution:
5+
def intersection(self, nums1: List[int], nums2: List[int]) -> List[int]:
6+
nums1.sort()
7+
nums2.sort()
8+
ans = set()
9+
n1, n2 = len(nums1), len(nums2)
10+
i, j = 0, 0
11+
while i < n1 and j < n2:
12+
if nums1[i] < nums2[j]:
13+
i += 1
14+
elif nums1[i] > nums2[j]:
15+
j += 1
16+
else:
17+
ans.add(nums1[i])
18+
i += 1
19+
j += 1
20+
return ans

Diff for: is-graph-bipartite.py

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# 785. Is Graph Bipartite?
2+
# https://leetcode.com/problems/is-graph-bipartite/
3+
4+
class Solution:
5+
def isBipartite(self, graph: List[List[int]]) -> bool:
6+
n = len(graph)
7+
NOT_EXPLORED, RED, BLUE = -1, 0, 1
8+
color = [NOT_EXPLORED]*n
9+
for u in range(n):
10+
if color[u] == NOT_EXPLORED:
11+
color[u] = RED
12+
stack = []
13+
stack.append(u)
14+
while stack:
15+
curr = stack.pop()
16+
for v in graph[curr]:
17+
if color[v] == NOT_EXPLORED:
18+
color[v] = BLUE if color[curr] == RED else RED
19+
stack.append(v)
20+
elif color[curr] == color[v]:
21+
return False
22+
return True

Diff for: isomorphic-strings.py

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
# 205. Isomorphic Strings
12
# https://leetcode.com/problems/isomorphic-strings/
23

34
class Solution:

Diff for: k-closest-points-to-origin.py

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
# 973. K Closest Points to Origin
12
# https://leetcode.com/problems/k-closest-points-to-origin/
23

34
class Solution:

Diff for: letter-combinations-of-a-phone-number.py

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
# 17. Letter Combinations of a Phone Number
12
# https://leetcode.com/problems/letter-combinations-of-a-phone-number/
23

34
class Solution:

Diff for: longest-palindromic-substring.py

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
# 5. Longest Palindromic Substring
12
# https://leetcode.com/problems/longest-palindromic-substring/
23

34
class Solution:

Diff for: longest-substring-without-repeating-characters.py

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
# 3. Longest Substring Without Repeating Characters
12
# https://leetcode.com/problems/longest-substring-without-repeating-characters/
23

34
class Solution:

Diff for: merge-intervals.py

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
# 56. Merge Intervals
12
# https://leetcode.com/problems/merge-intervals/
23

34
class Solution:

Diff for: merge-k-sorted-lists.py

+3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
# 23. Merge k Sorted Lists
2+
# https://leetcode.com/problems/merge-k-sorted-lists/
3+
14
# Definition for singly-linked list.
25
# class ListNode:
36
# def __init__(self, x):

Diff for: merge-sorted-array.py

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
# 88. Merge Sorted Array
12
# https://leetcode.com/problems/merge-sorted-array/
23

34
class Solution:

Diff for: minimum-depth-of-binary-tree.py

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
# 111. Minimum Depth of Binary Tree
12
# https://leetcode.com/problems/minimum-depth-of-binary-tree/
23

34
# Definition for a binary tree node.

Diff for: move-zeroes.py

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
# 283. Move Zeroes
12
# https://leetcode.com/problems/move-zeroes/
23

34
class Solution:

Diff for: next-closest-time.py

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
# 681. Next Closest Time
12
# https://leetcode.com/problems/next-closest-time/
23

34
class Solution:

0 commit comments

Comments
 (0)