- Notifications
You must be signed in to change notification settings - Fork0
add new solution#1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Merged
Changes fromall commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
2 changes: 1 addition & 1 deletion动态规划/1143. 最长公共子序列.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions双指针/0.双指针总结.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -34,3 +34,4 @@ | ||
- [ ] 28 实现 strStr() :KMP算法 | ||
- [ ] 31 下一个排列 | ||
- [ ] 75 颜色分类:双指针/三指针swap | ||
- [ ] 264 丑数II |
31 changes: 13 additions & 18 deletions双指针/264.丑数II.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
2 changes: 2 additions & 0 deletions排序/0.排序总结.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -138,6 +138,8 @@ def merge_sort(nums): | ||
- [ ] 215 数组中的第K个最大元素 | ||
- [ ] 347 前K个高频元素 | ||
- [ ] 703 数据流中的第K大元素 | ||
- [ ] 40剑指offer. 最小的k个数 | ||
69 changes: 69 additions & 0 deletions排序/347 前K个高频元素.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
``` | ||
# 给你一个整数数组 nums 和一个整数 k ,请你返回其中出现频率前 k 高的元素。你可以按 任意顺序 返回答案。 | ||
# | ||
# | ||
# | ||
# 示例 1: | ||
# | ||
# | ||
# 输入: nums = [1,1,1,2,2,3], k = 2 | ||
# 输出: [1,2] | ||
# | ||
# | ||
# 示例 2: | ||
# | ||
# | ||
# 输入: nums = [1], k = 1 | ||
# 输出: [1] | ||
# | ||
# | ||
# | ||
# 提示: | ||
# | ||
# | ||
# 1 <= nums.length <= 105 | ||
# k 的取值范围是 [1, 数组中不相同的元素的个数] | ||
# 题目数据保证答案唯一,换句话说,数组中前 k 个高频元素的集合是唯一的 | ||
# | ||
# | ||
# | ||
# | ||
# 进阶:你所设计算法的时间复杂度 必须 优于 O(n log n) ,其中 n 是数组大小。 | ||
# Related Topics 数组 哈希表 分治 桶排序 计数 快速选择 排序 堆(优先队列) | ||
# 👍 1067 👎 0 | ||
# leetcode submit region begin(Prohibit modification and deletion) | ||
class Solution: | ||
def topKFrequent(self, nums: List[int], k: int) -> List[int]: | ||
counter = collections.Counter(nums) | ||
counter = list(counter.items()) | ||
def topK(nums, k, left, right): | ||
def partition(nums, left, right): | ||
key = left | ||
while left < right: | ||
while left < right and nums[right][1] >= nums[key][1]: | ||
right -= 1 | ||
while left < right and nums[left][1] <= nums[key][1]: | ||
left += 1 | ||
nums[right], nums[left] = nums[left], nums[right] | ||
nums[key], nums[left] = nums[left], nums[key] | ||
return left | ||
if left >= right: | ||
return | ||
mid = partition(nums, left, right) | ||
if mid == k: | ||
return | ||
elif mid > k: | ||
topK(nums, k, left, mid-1) | ||
else: | ||
topK(nums, k, mid+1, right) | ||
l = len(counter) | ||
topK(counter, l-k, 0, len(counter) - 1) | ||
return [i[0] for i in counter[-k:]] | ||
# leetcode submit region end(Prohibit modification and deletion) | ||
``` | ||
快速跑徐 |
2 changes: 1 addition & 1 deletion树/0.总结.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.