mirror of
https://github.com/halfrost/LeetCode-Go.git
synced 2025-07-07 01:44:56 +08:00
添加 problem 215
This commit is contained in:
@ -0,0 +1,34 @@
|
||||
package leetcode
|
||||
|
||||
import "sort"
|
||||
|
||||
// 排序的方法反而速度是最快的
|
||||
func findKthLargest_(nums []int, k int) int {
|
||||
sort.Ints(nums)
|
||||
return nums[len(nums)-k]
|
||||
}
|
||||
|
||||
// 这个方法的理论依据是 partition 得到的点的下标就是最终排序之后的下标,根据这个下标,我们可以判断第 K 大的数在哪里
|
||||
func findKthLargest(nums []int, k int) int {
|
||||
if len(nums) == 0 {
|
||||
return 0
|
||||
}
|
||||
return selection(nums, 0, len(nums)-1, len(nums)-k)
|
||||
}
|
||||
|
||||
func selection(arr []int, l, r, k int) int {
|
||||
|
||||
if l == r {
|
||||
return arr[l]
|
||||
}
|
||||
|
||||
p := partition__(arr, l, r)
|
||||
|
||||
if k == p {
|
||||
return arr[p]
|
||||
} else if k < p {
|
||||
return selection(arr, l, p-1, k)
|
||||
} else {
|
||||
return selection(arr, p+1, r, k)
|
||||
}
|
||||
}
|
@ -0,0 +1,63 @@
|
||||
package leetcode
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
)
|
||||
|
||||
type question215 struct {
|
||||
para215
|
||||
ans215
|
||||
}
|
||||
|
||||
// para 是参数
|
||||
// one 代表第一个参数
|
||||
type para215 struct {
|
||||
one []int
|
||||
two int
|
||||
}
|
||||
|
||||
// ans 是答案
|
||||
// one 代表第一个答案
|
||||
type ans215 struct {
|
||||
one int
|
||||
}
|
||||
|
||||
func Test_Problem215(t *testing.T) {
|
||||
|
||||
qs := []question215{
|
||||
|
||||
question215{
|
||||
para215{[]int{3, 2, 1, 5, 6, 4}, 2},
|
||||
ans215{5},
|
||||
},
|
||||
|
||||
question215{
|
||||
para215{[]int{3, 2, 3, 1, 2, 4, 5, 5, 6}, 4},
|
||||
ans215{4},
|
||||
},
|
||||
|
||||
question215{
|
||||
para215{[]int{0, 0, 0, 0, 0}, 2},
|
||||
ans215{0},
|
||||
},
|
||||
|
||||
question215{
|
||||
para215{[]int{1}, 1},
|
||||
ans215{1},
|
||||
},
|
||||
|
||||
question215{
|
||||
para215{[]int{3, 2, 3, 1, 2, 4, 5, 5, 6, 7, 7, 8, 2, 3, 1, 1, 1, 10, 11, 5, 6, 2, 4, 7, 8, 5, 6}, 20},
|
||||
ans215{10},
|
||||
},
|
||||
}
|
||||
|
||||
fmt.Printf("------------------------Leetcode Problem 215------------------------\n")
|
||||
|
||||
for _, q := range qs {
|
||||
_, p := q.ans215, q.para215
|
||||
fmt.Printf("【input】:%v 【output】:%v\n", p.one, findKthLargest(p.one, p.two))
|
||||
}
|
||||
fmt.Printf("\n\n\n")
|
||||
}
|
31
Algorithms/215. Kth Largest Element in an Array/README.md
Normal file
31
Algorithms/215. Kth Largest Element in an Array/README.md
Normal file
@ -0,0 +1,31 @@
|
||||
# [215. Kth Largest Element in an Array](https://leetcode.com/problems/kth-largest-element-in-an-array/)
|
||||
|
||||
## 题目
|
||||
|
||||
Find the kth largest element in an unsorted array. Note that it is the kth largest element in the sorted order, not the kth distinct element.
|
||||
|
||||
Example 1:
|
||||
|
||||
```c
|
||||
Input: [3,2,1,5,6,4] and k = 2
|
||||
Output: 5
|
||||
```
|
||||
|
||||
Example 2:
|
||||
|
||||
```c
|
||||
Input: [3,2,3,1,2,4,5,5,6] and k = 4
|
||||
Output: 4
|
||||
```
|
||||
|
||||
Note:
|
||||
|
||||
You may assume k is always valid, 1 ≤ k ≤ array's length.
|
||||
|
||||
|
||||
## 题目大意
|
||||
|
||||
找出数组中第 K 大的元素。这一题非常经典。可以用 O(n) 的时间复杂度实现。
|
||||
|
||||
在快拍的 partition 操作中,每次 partition 操作结束都会返回一个点,这个标定点的下标和最终排序之后有序数组中这个元素所在的下标是一致的。利用这个特性,我们可以不断的划分数组区间,最终找到第 K 大的元素。执行一次 partition 操作以后,如果这个元素的下标比 K 小,那么接着就在后边的区间继续执行 partition 操作;如果这个元素的下标比 K 大,那么就在左边的区间继续执行 partition 操作;如果相等就直接输出这个下标对应的数组元素即可。
|
||||
|
Reference in New Issue
Block a user