LeetCode215 数组中的第K个最大元素
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.
思路一:调用python库
class Solution(object):
def findKthLargest(self, nums, k):
"""
:type nums: List[int]
:type k: int
:rtype: int
"""
return sorted(nums, reverse = True)[k-1]
思路二:堆排序
class Solution(object):
def findKthLargest(self, nums, k):
"""
:type nums: List[int]
:type k: int
:rtype: int
"""
from heapq import *
heap = []
heapify(heap)
for num in nums:
heappush(heap, num)
if len(heap) > k:
heappop(heap)
return heap[0]
思路三:快排 + partition
class Solution(object):
def findKthLargest(self, nums, k):
"""
:type nums: List[int]
:type k: int
:rtype: int
"""
left, right, target = 0, len(nums) - 1, k - 1
while True:
pos = self.partition(nums, left, right)
if pos == target:
return nums[pos]
elif pos > k: #要往左找
right = pos - 1
elif pos < k: #要往右找
left = pos + 1
def partition(self, nums, left, right):
import random
k = random.randint(left, right)
pivot = nums[k]
nums[left], nums[k] = nums[k], nums[left]
index = left
for i in range(left + 1, right + 1):
if nums[i] > pivot:
index += 1
nums[i], nums[index] = nums[index], nums[i]
nums[left], nums[index] = nums[index], nums[left]
return index #此时所有index左侧的值都比nums[index]大, 所有右侧的值都比nums[index]小
代码出自LeetCode-Python-215. 数组中的第K个最大元素,详细解释也可以参考这篇博客。
LeetCode215 数组中的第K个最大元素官方解决方案
LeetCode217 存在重复元素
Given an array of integers, find if the array contains any duplicates.
Your function should return true if any value appears at least twice in the array,
and it should return false if every element is distinct.
思路一:排序
先对数组进行排序;
然后遍历数组,前后比较元素值是否相等:
相等,返回 True;
数组遍历完成后,元素均不相同,返回 False。
class Solution:
def containsDuplicate(self, nums: List[int]) -> bool:
# 先对数组排序
nums.sort()
# 遍历数组,比较前后元素值
for i in range(len(nums)-1):
if nums[i] == nums[i+1]:
return True
return False
思路二:哈希表
遍历数组,将数组中的元素添加到哈希表中。
如果元素已经存在于哈希表中,那么表示由重复 ,返回 True;否则返回 False。
class Solution:
def containsDuplicate(self, nums: List[int]) -> bool:
# 声明集合
s = set()
# 遍历数组,将元素添加到集合中
for num in nums:
# 若元素存在于集合中,表示存在重复元素
if num in s:
return True
s.add(num)
return False
思路一和二详细解释和代码均出自LeetCode 217. 存在重复元素 | Python。
思路三:字典(详细解释参见leetcode 217 python 存在重复元素)。
遍历列表,已元素为键,出现次数为值,如果出现次数有大于1次的,说明存在重复元素。
class Solution:
def containsDuplicate(self, nums):
"""
:type nums: List[int]
:rtype: bool
"""
d = dict()
for i in nums:
n = d.get(i, 0)
n += 1
d[i] = n
if n > 1:
return True
return False
LeetCode230 二叉搜索树中第K小的元素
Given a binary search tree,
write a function kthSmallest to find the kth smallest element in it.
思路一:递归
通过构造 BST 的中序遍历序列,则第 k-1 个元素就是第 k 小的元素。
class Solution:
def kthSmallest(self, root, k):
def inorder(r):
return inorder(r.left) + [r.val] + inorder(r.right) if r else []
return inorder(root)[k - 1]
思路二:迭代
在栈的帮助下,可以将方法一的递归转换为迭代。
class Solution:
def kthSmallest(self, root, k):
stack = []
while True:
while root:
stack.append(root)
root = root.left
root = stack.pop()
k -= 1
if not k:
return root.val
root = root.right
代码和详细的解释,以及进阶版的解答都可以看原博客LeetCode——230. 二叉搜索树中第K小的元素。
LeetCode230 二叉搜索树中第K小的元素官方解决方案
任务链接:
team-learning-program/LeetCodeTencent/215 数组中的第K个最大元素.md
team-learning-program/LeetCodeTencent/217 存在重复元素.md
team-learning-program/LeetCodeTencent/230 二叉搜索树中第K小的元素.md
版权声明:本文为u012428169原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。