This commit is contained in:
2026-06-05 22:18:39 +08:00
parent 0aed1528e2
commit 14d05011d5
7 changed files with 169 additions and 7 deletions
@@ -57,6 +57,12 @@ Write your approach here.
#+begin_src python
class Solution:
def twoSum(self, nums: List[int], target: int) -> List[int]:
sb = {}
for xi, x in enumerate(nums):
want = target - x;
if want in sb:
return sb[want], xi
sb[x] = xi
#+end_src
** TODO C++
@@ -40,9 +40,11 @@ Given an integer array ~nums~ and an integer ~k~, return /the/ ~k~ /most frequen
Write your approach here.
** TODO Python
#+begin_src python
#+begin_src python :lc-problem 347 :lc-lang python3
class Solution:
def topKFrequent(self, nums: List[int], k: int) -> List[int]:
from collections import Counter
return [n for n, _ in Counter(nums).most_common(k)]
#+end_src
** TODO C++
@@ -52,10 +52,40 @@ Write your approach here.
** TODO Python
#+begin_src python
from collections import defaultdict as dd
class Solution:
def stringMatching(self, words: List[str]) -> List[str]:
words = reversed(sorted(words))
def squash(word, trie):
lst = []
for c in word:
lst.append([trie, ''])
for i in range(len(lst)):
lst[i][0] = lst[i][0][c]
lst[i][1] += c
if not lst[i][0]['ads']:
lst[i][0]['ads'] = {lst[i][1]: [word]}
else:
lst[i][0]['ads'][lst[i][1]].append(word)
def in_trie(word, trie):
for c in word:
if c not in trie:
return []
trie = trie[c]
return [(word, bw) for bw in trie['ads'][word]]
trie_maker = lambda: dd(trie_maker)
trie = trie_maker()
ans = []
for s in words:
ans += in_trie(s, trie)
squash(s, trie)
return ans
#+end_src
**
** TODO C++
#+begin_src cpp
class Solution {