Я пытаюсь улучшить свои навыки кодирования, и я застрял на вопросе и не могу понять тестовый пример. I passed the first 5 test cases but got stuck at the last one:
Top K Frequent Elements
Given an integer array nums and an integer k, return the k most frequent elements within the array.
The test cases are generated such that the answer is always unique.
You may return the output in any order.
Example 1
Input: nums = [1,2,2,3,3,3], k = 2
[2,3]
Пример 2
Ввод: nums = [7,7], k = 1
output: [7]
Input: nums=[1,2] k=2
Output: [1,2]
< /code>
my code: < /h2>
class Solution:
def topKFrequent(self, nums: List[int], k: int) -> List[int]:
number_map={}
result=[]
for num in nums:
if num in number_map:
number_map[num] +=1
else:
number_map[num] = 1
for num in number_map:
if number_map[num] >= k:
result.append(num)
return result
< /code>
Я хочу знать, почему это не работает для < /p>
nums=[1,2] k=2
Я пытаюсь улучшить свои навыки кодирования, и я застрял на вопросе и не могу понять тестовый пример. I passed the first 5 test cases but got stuck at the last one:
Top K Frequent Elements Given an integer array nums and an integer k, return the k most frequent elements within the array. The test cases are generated such that the answer is always [b]unique[/b]. You may return the output in [b]any order[/b]. Example 1 [list] [*]Input: nums = [1,2,2,3,3,3], k = 2 [*][2,3] [/list] Пример 2 [list] [*] Ввод: nums = [7,7], k = 1 [*] output: [7] [/list]
Input: nums=[1,2] k=2
Output: [1,2] < /code>
my code: < /h2> class Solution: def topKFrequent(self, nums: List[int], k: int) -> List[int]: number_map={} result=[] for num in nums: if num in number_map: number_map[num] +=1 else: number_map[num] = 1 for num in number_map: if number_map[num] >= k: result.append(num)
return result < /code> Я хочу знать, почему это не работает для < /p> nums=[1,2] k=2
Я пытаюсь улучшить свои навыки кодирования, и я застрял на вопросе и не могу понять тестовый пример. I passed the first 5 test cases but got stuck at the last one:
Top K Frequent Elements
Given an integer array nums and an integer k, return the k...
Я пытаюсь улучшить свои навыки кодирования, и я застрял на вопросе и не могу понять тестовый пример. I passed the first 5 test cases but got stuck at the last one:
Top K Frequent Elements
Given an integer array nums and an integer k, return the k...
Я пытаюсь улучшить свои навыки кодирования, и я застрял на вопросе и не могу понять тестовый пример. I passed the first 5 test cases but got stuck at the last one:
Top K Frequent Elements
Given an integer array nums and an integer k, return the k...
Я не могу открыть свой эмулятор, пожалуйста, помогите решить эту проблему. Я застрял в течение 3 дней. Я попробовал несколько решений, но любое из решений не работает. Поэтому я прошу любого, кто сталкивается с этой проблемой или знает эту проблему,...
Я не могу понять разницу между методами с параметрами и без них, когда они возвращают значение, когда они их вызывают и т. д.
Я читал основы программирования и надеялся понять это , но я не могу понять даже эту часть программирования.