Skip to content

Commit 627ea2c

Browse files
author
iamminji
committed
[20200719] july challenge
1 parent 25f9458 commit 627ea2c

File tree

4 files changed

+54
-0
lines changed

4 files changed

+54
-0
lines changed

README.md

+3
Original file line numberDiff line numberDiff line change
@@ -235,3 +235,6 @@
235235
| [Same Tree](https://leetcode.com/explore/challenge/card/july-leetcoding-challenge/545/week-2-july-8th-july-14th/3389/) | [python](challenge/2020/july/Same_Tree.py) | |
236236
| [3Sum](https://leetcode.com/explore/challenge/card/july-leetcoding-challenge/545/week-2-july-8th-july-14th/3384/) | [python](challenge/2020/july/3Sum.py) | [solutions](challenge/2020/july/3Sum.md)|
237237
| [Add Binary](https://leetcode.com/explore/challenge/card/july-leetcoding-challenge/546/week-3-july-15th-july-21st/3395/) | [python](challenge/2020/july/Add_Binary.py) | [solution](challenge/2020/july/Add_Binary.md) |
238+
| [Subsets](https://leetcode.com/explore/challenge/card/july-leetcoding-challenge/545/week-2-july-8th-july-14th/3387/) | [python](challenge/2020/july/Subsets.py) | |
239+
| [Reverse Words in a String](https://leetcode.com/explore/challenge/card/july-leetcoding-challenge/546/week-3-july-15th-july-21st/3391/) | [python](challenge/2020/july/Reverse_Words_in_a_String.py) | |
240+
| [Top K Frequent Elements](https://leetcode.com/explore/challenge/card/july-leetcoding-challenge/546/week-3-july-15th-july-21st/3393/) | [python](challenge/2020/july/Top_K_Frequent_Elements.py) | |
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
class Solution:
2+
def reverseWords(self, s: str) -> str:
3+
return " ".join(reversed(s.split()))
4+

challenge/2020/july/Subsets.py

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
from typing import List
2+
from copy import deepcopy
3+
4+
5+
class Solution:
6+
7+
def dfs(self, nums, i, inner, result):
8+
result.append(deepcopy(inner))
9+
for j in range(i, len(nums)):
10+
inner.append(nums[j])
11+
self.dfs(nums, j + 1, inner, result)
12+
inner.pop()
13+
return
14+
15+
def subsets(self, nums: List[int]) -> List[List[int]]:
16+
result = []
17+
self.dfs(nums, 0, [], result)
18+
return result
19+
20+
21+
if __name__ == '__main__':
22+
nums = [1, 2, 3]
23+
sol = Solution()
24+
print(sol.subsets(nums))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
from typing import List
2+
from collections import Counter
3+
4+
5+
class Solution:
6+
def topKFrequent(self, nums: List[int], k: int) -> List[int]:
7+
counter = Counter(nums)
8+
counter = sorted(counter.items(), key=lambda x: -x[1])
9+
result = []
10+
cnt = 0
11+
for item in counter:
12+
if cnt == k:
13+
break
14+
result.append(item[0])
15+
cnt += 1
16+
return result
17+
18+
19+
if __name__ == '__main__':
20+
nums = [1, 1, 1, 2, 2, 3]
21+
k = 2
22+
sol = Solution()
23+
print(sol.topKFrequent(nums, k))

0 commit comments

Comments
 (0)