Swift   发布时间:2022-03-31  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了[Swift]LeetCode215. 数组中的第K个最大元素 | 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: Input: and k = 2 Output: 5 [3,2,1,5,6,4] Exam

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:

Input: and k = 2
Output: 5
[3,2,1,5,6,4]

Example 2:

Input: and k = 4
Output: 4[3,3,4,6]

Note: 
You may assume k is always valid,1 ≤ k ≤ array‘s length.

在未排序的数组中找到第 k 个最大的元素。请注意,你需要找的是数组排序后的第 k 个最大的元素,而不是第 k 个不同的元素。

示例 1:

输入:  k = 2
输出: 5
[3,4] 和

示例 2:

输入:  k = 4
输出: 4[3,6]

说明:

你可以假设 k 总是有效的,且 1 ≤ k ≤ 数组的长度。

24ms

1 class Solution {
2     func findKthLargest(_ nums: [Int],_ k: int) -> Int {
3         return nums.sorted(by: >)[k - 1]
4     }
5 }

56ms

 1 class Solution {
 2     func findKthLargest(_ nums: [Int],_ k: int) -> Int {
 3       var newNums = nums
 4         buildMaxHead(&newNums)//建最大堆
 5         for i in ((newNums.count - k - 1)..<newNums.count).reversed() {
 6             swap(&newNums,0,i)
 7             if i == newNums.count - k {
 8                 return newNums[i]
 9             }
10             headify(&newNums,i)
11         }
12         return 0
13     }
14     //@H_37_97@mARK: 堆排序
15     func headSort(_ nums:inout [Int]) {
16         buildMaxHead(&nums)
17         for i in (1..<nums.count).reversed() {
18             swap(&nums,i,0)
19             headify(&nums,i)
20         }
21     }
22     //建最大堆
23     func buildMaxHead(_ nums:inout [Int]) {
24         for i in (0...nums.count/2).reversed() {
25             headify(&nums,nums.count)
26         }
27     }
28     //堆调整
29     func headify(_ nums:inout [Int],_ index: Int,_ length: int) {
30         let leftChild = index * 2 + 1 // index节点的左孩子节点
31         let rightChild = index * 2 + 2 //index节点的右孩子节点
32         var largerTindex = index //节点与左右孩子的最大值
33         if leftChild < length && nums[leftChild] > nums[largerTindex] {
34             largerTindex = leftChild
35         }
36         if rightChild < length && nums[rightChild] > nums[largerTindex] {
37             largerTindex = rightChild
38         }
39         //如果交换了
40         if largerTindex != index {
41             swap(&nums,largerTindex,indeX)//交换节点值
42             headify(&nums,length)//调整变动的节点
43         }
44     }
45         func swap(_ nums:inout [Int],_ i:Int,_ j: int) {
46         let temp = nums[i]
47         nums[i] = nums[j]
48         nums[j] = temp
49     }
50 }

56ms

 1 class Solution {
 2     func findKthLargest(_ nums: [Int],_ k: int) -> Int {
 3         var nums = nums
 4         return find(&nums,0,nums.count - 1,k)
 5     }
 6 
 7     func find(_ nums: inout [Int],_ start: Int,_ end: Int,_ k: int) -> Int {
 8         let pivot = partition(&nums,start,end)
 9         let j = nums.count - pivot
10         if j > k {
11             return find(&nums,pivot + 1,end,k)
12         }
13         if j == k {
14             return nums[pivot]
15         }
16         return find(&nums,pivot - 1,k)
17     }
18 
19     func partition(_ nums: inout [Int],_ end: int) -> Int {
20         guard start < end else {
21             return start
22         }
23         let pivot = Int.random(in: start...end)
24         nums.swapAt(pivot,end)
25         var index = start
26         for i in start..<end where nums[i] < nums[end] {
27             nums.swapAt(i,indeX)
28             index += 1
29         }
30         nums.swapAt(index,end)
31         return index
32     }
33 }

60ms

 1 class Solution {
 2     func findKthLargest(_ nums: [Int],_ k: int) -> Int {
 3         var arr = nums
 4         return quickSELEct(arr: &arr,start: 0,end: arr.count - 1,k: arr.count - k)
 5     }
 6    
 7     private func quickSELEct(arr: inout[Int],start: Int,end: Int,k: int) -> Int {
 8         let pivot = partition(arr: &arr,start: start,end: end)
 9         if pivot > k { return quickSELEct(arr: &arr,end: pivot - 1,k: k) }
10         if pivot == k { return arr[pivot] }
11         return quickSELEct(arr: &arr,start: pivot + 1,end: end,k: k)
12     }
13 
14     private func partition(arr: inout[Int],end: int) -> Int {
15         guard start < end else { return start }
16         
17         var i = start
18         let pivoTindex = Int.random(in: start ... end)
19         let pivot  = arr[pivoTindex]
20         (arr[pivoTindex],arr[end]) = (arr[end],arr[pivoTindex])
21         
22         for j in start ..< end {
23             if arr[j] < pivot {
24                 (arr[i],arr[j]) = (arr[j],arr[i])
25                 i += 1
26             }
27         }
28         
29         (arr[i],arr[i])
30         return i
31     }
32     
33 }

84ms

 1 class Solution {
 2     private var heapSize = 0
 3     
 4     func findKthLargest(_ nums: [Int],_ k: int) -> Int {
 5         guard nums.count > 0 else {
 6             return 0
 7         }
 8         var nums = nums
 9         buildMaxHeap(&nums)
10         var i = 0
11         while i < k - 1 {
12             swap(&nums,heapSize - 1)
13             heapSize -= 1
14             maxHeapAdjust(&nums,0)
15             i += 1
16         }
17         return nums[0]
18     }
19     
20     func buildMaxHeap(_ nums: inout [Int]) {
21         heapSize = nums.count
22         var i = heapSize >> 1 - 1
23         while i >= 0 {
24             maxHeapAdjust(&nums,i)
25             i -= 1
26         }
27     }
28     
29     func maxHeapAdjust(_ nums: inout [Int],_ index: int) {
30         var largest = index
31         var l = index << 1 + 1
32         var r = index << 1 + 2
33         if l < heapSize && nums[l] > nums[largest] {
34             largest = l
35         }
36         if r < heapSize && nums[r] > nums[largest] {
37             largest = r
38         }
39         if largest != index {
40             swap(&nums,largest,indeX)
41             maxHeapAdjust(&nums,largest)
42         }
43     }
44     
45     func swap(_ nums: inout [Int],_ i: Int,_ j: int) {
46         let temp = nums[i]
47         nums[i] = nums[j]
48         nums[j] = temp
49     }
50 }

大佬总结

以上是大佬教程为你收集整理的[Swift]LeetCode215. 数组中的第K个最大元素 | Kth Largest Element in an Array全部内容,希望文章能够帮你解决[Swift]LeetCode215. 数组中的第K个最大元素 | Kth Largest Element in an Array所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。