由于数组已经按照升序排列,利用双指针,i指针指向数组头,j指针指向数组尾,如果两数之和大于目标数则j前移,如果两数之和小鱼目标数则i后移,直到找到两个值。
class Solution(object):
def twoSum(self, numbers, target):
"""
:type numbers: List[int]
:type target: int
:rtype: List[int]
"""
index = []
i = 1
j = len(numbers)
while i<j:
if numbers[i-1] + numbers[j-1] > target:
j -= 1
elif numbers[i-1] + numbers[j-1] < target:
i += 1
else:
index.append(i)
index.append(j)
break
return index
首先利用快速排序降序排好数组,然后输出第k个元素。递归时,如果一次排序后左边数组left的长度大于等于k,则不需要对右边数组right进行排序,因为第k个最大元素一定在左边数组中。这个细节可以使运行时间由120ms降低到84ms.
class Solution(object):
def findKthLargest(self, nums, k):
"""
:type nums: List[int]
:type k: int
:rtype: int
"""
return self.QuickSort(nums, k)[k-1]
def QuickSort(self, A, k):
if len(A) > 1:
key = A[int(len(A)/2)]
left, right = [], []
A.remove(key)
for a in A:
if a >= key:
left.append(a)
elif a <= key:
right.append(a)
if len(left) >= k:
return self.QuickSort(left, k)
else:
return self.QuickSort(left, k) + [key] + self.QuickSort(right, k)
else:
return A
首先建立一个键为元素,值为频率的键值对列表,然后按频率进行堆排序,维护一个最大堆,取前k个键值对的键作为输出。
class Solution(object):
def topKFrequent(self, nums, k):
"""
:type nums: List[int]
:type k: int
:rtype: List[int]
"""
# 建立元素-频率的键值对列表
dic = {}
for num in nums:
dic[num] = dic.get(num, 0) + 1
lis = []
for key, value in dic.items():
lis.append((key, value))
# 建堆
heapsize = len(lis)
i = int(len(lis)/2) - 1
while i >= 0:
self.MaxHeapify(lis, i, heapsize)
i -= 1
# 取前k个键值对的键作为输出
j = len(lis) - 1
topk = []
for _ in range(k):
lis[0], lis[j] = lis[j], lis[0]
topk.append(lis[j][0])
heapsize -= 1
self.MaxHeapify(lis, 0, heapsize)
j -= 1
return topk
# 维护最大堆
def MaxHeapify(self, A, i, heapsize):
l = 2*i+1
r = 2*i+2
if l < heapsize and A[l][1] > A[i][1]:
largest = l
else:
largest = i
if r < heapsize and A[r][1] > A[largest][1]:
largest = r
if largest != i:
A[i], A[largest] = A[largest], A[i]
self.MaxHeapify(A, largest, heapsize)
第一种方法:计数排序,遍历一次数组计算0,1,2的频数,然后改写数组,时间复杂度为O(n),需要额外的计数字典空间。
class Solution(object):
def sortColors(self, nums):
"""
:type nums: List[int]
:rtype: None Do not return anything, modify nums in-place instead.
"""
dic = {0:0,
1:0,
2:0}
for num in nums:
if num == 0:
dic[0] += 1
elif num == 1:
dic[1] += 1
elif num == 2:
dic[2] += 1
nums[:dic[0]] = [0] * dic[0]
nums[dic[0]:dic[0]+dic[1]] = [1] * dic[1]
nums[dic[0]+dic[1]:] = [2] * dic[2]
第二种方法:三指针,左指针指向红色(0)的右边界,右指针指向蓝色(2)的左边界,当前指针指向遍历数组时的交换元素位置,当前元素为0时与左指针指向的元素交换,并且左指针与当前指针右移,当前元素为1时,当前指针右移,当前元素为2时与右指针指向的元素交换,并且右指针左移,当前指针不移动。时间复杂度为O(n),空间复杂度为O(1)。
class Solution(object):
def sortColors(self, nums):
"""
:type nums: List[int]
:rtype: None Do not return anything, modify nums in-place instead.
"""
i = 0
j = len(nums) - 1
k = 0
while k <= j:
if nums[k] == 0:
nums[k], nums[i] = nums[i], nums[k]
i += 1
k += 1
elif nums[k] == 1:
k += 1
elif nums[k] == 2:
nums[k], nums[j] = nums[j], nums[k]
j -= 1
首先对胃口数组和饼干数组按降序排序,不知道为什么自己写的快排比内置排序函数慢很多,然后根据贪心思想,每次用最大的饼干去满足最大的胃口,如果最大的饼干都满足不了,跳过这个胃口,直到饼干发完或小孩都考虑完。
class Solution(object):
def findContentChildren(self, g, s):
"""
:type g: List[int]
:type s: List[int]
:rtype: int
"""
g.sort(reverse=True)
s.sort(reverse=True)
i, j = 0, 0
count = 0
while i<len(g) and j<len(s):
if g[i] <= s[j]:
i += 1
j += 1
count += 1
elif g[i] > s[j]:
i += 1
return count