小黑代码(小黑独立做出来)
class Solution:
def fourSum(self, nums: List[int], target: int) -> List[List[int]]:
# 数组长度
n = len(nums)
if n < 4:
return []
# 排序
nums.sort()
def three_sum(target_, start, end):
if end - start < 2:
return []
# 结果数组
res = []
for i in range(start, end):
if i != start and nums[i-1] == nums[i]:
continue
# 定义双指针
head = i + 1
tail = end
while head < tail:
if nums[head] + nums[tail] < target_-nums[i]:
head += 1
elif nums[head] + nums[tail] > target_-nums[i]:
tail -= 1
else:
res.append([nums[i], nums[head], nums[tail]])
while tail-1 != head and nums[tail] == nums[tail-1]:
tail -= 1
tail -= 1
return res
results = []
for i in range(n):
if i != 0 and nums[i-1] == nums[i]:
continue
res = three_sum(target-nums[i], i+1, n-1)
for l in res:
l.append(nums[i])
results.append(l)
return results
排序 + 双指针
class Solution:
def fourSum(self, nums: List[int], target: int) -> List[List[int]]:
# 数组长度
n = len(nums)
if n < 4:
return []
# 排序数组
nums.sort()
# 结果数组
results = []
# 开始固定起点,寻找3数之和
for i in range(n-3):
# 去除相同元素的情况
if i != 0 and nums[i-1] == nums[i]:
continue
# 剪枝情况
if nums[i]+nums[i+1]+nums[i+2]+nums[i+3] > target:
break
if nums[i]+nums[n-3]+nums[n-2]+nums[n-1] < target:
continue
# 进行三数之和判断
for j in range(i+1, n-2):
# 去除相同元素的情况
if j != i+1 and nums[j] == nums[j-1]:
continue
# 剪枝情况
if nums[j]+nums[j+1]+nums[j+2]+nums[i] > target:
break
if nums[j]+nums[n-2]+nums[n-1]+nums[i] < target:
continue
# 定义双指针
left = j+1
right = n-1
while left < right:
if nums[left]+nums[right]+nums[i]+nums[j] < target:
left += 1
elif nums[left]+nums[right]+nums[i]+nums[j] > target:
right -= 1
else:
results.append([nums[left], nums[right], nums[i], nums[j]])
while left < right and nums[left] == nums[left+1]:
left += 1
left += 1
return results
数据库练习
620. 有趣的电影
# Write your MySQL query statement below
SELECT
id,
movie,
description,
rating
FROM
cinema
WHERE
description != 'boring' AND id % 2 = 1
ORDER BY
rating DESC
Pandas练习
620. 有趣的电影
import pandas as pd
def not_boring_movies(cinema: pd.DataFrame) -> pd.DataFrame:
# 过滤无聊与id
data = cinema[(cinema.description!='boring') & (cinema.id%2!=0)]
data.sort_values('rating', ascending=False, inplace=True)
return data
小黑生活
午餐干饭
跟我们办公室主任互拍的一天哈哈哈
午休期间看看动漫《强风吹拂》
随身wifi购买力三年的100G月包,为了方便以后在单位用视频网站学习放松哈哈哈哈,买三年送两年,还可以~
怀柔全程马拉松证书下发,全马100多名在2000多人,这成绩还可以,开心最重要啦,嗨起来~
晚上喝跑团小伙伴龙潭中湖公园溜溜弯,看到了可爱的小刺猬,顺便刷一刷里程到12300km发朋友圈打卡
回家喝个小酒放松一下
投个简历玩玩,看来python开发行业是真的饱和了,简历都没过